Queue/Usage: Difference between revisions
Content added Content deleted
(J) |
(add JavaScript) |
||
Line 9: | Line 9: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada> |
<lang ada>with FIFO; |
||
with FIFO; |
|||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 30: | Line 29: | ||
Pop (Queue, Value); |
Pop (Queue, Value); |
||
Put_Line ("Is_Empty " & Boolean'Image (Is_Empty (Queue))); |
Put_Line ("Is_Empty " & Boolean'Image (Is_Empty (Queue))); |
||
end Queue_Test; |
end Queue_Test;</lang> |
||
⚫ | |||
Sample output: |
Sample output: |
||
<pre> |
<pre>Is_Empty TRUE</pre> |
||
Is_Empty TRUE |
|||
⚫ | |||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=133 discussion] |
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=133 discussion] |
||
Line 78: | Line 75: | ||
exit(0); |
exit(0); |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Note that with C++'s standard queue, accessing the first element of the queue and removing it are two separate operations, <tt>front()</tt> and <tt>pop()</tt>. |
Note that with C++'s standard queue, accessing the first element of the queue and removing it are two separate operations, <tt>front()</tt> and <tt>pop()</tt>. |
||
<lang cpp> |
<lang cpp>#include <queue> |
||
#include <queue> |
|||
#include <cassert> // for run time assertions |
#include <cassert> // for run time assertions |
||
Line 126: | Line 123: | ||
q.pop(); |
q.pop(); |
||
assert( q.empty() ); |
assert( q.empty() ); |
||
⚫ | |||
} |
|||
</lang> |
|||
Note that the container used to store the queue elements can be specified explicitly; to use a linked linst instead of a deque (the latter is the default), just replace the definition of <tt>q</tt> to |
Note that the container used to store the queue elements can be specified explicitly; to use a linked linst instead of a deque (the latter is the default), just replace the definition of <tt>q</tt> to |
||
⚫ | |||
<lang cpp> |
|||
⚫ | |||
</lang> |
|||
(and add <tt>#include <list></tt>, of course). Also note that the containers can be used directly; in that case <tt>push</tt> and <tt>pop</tt> have to be replaced by <tt>push_back</tt> and <tt>pop_front</tt>. |
(and add <tt>#include <list></tt>, of course). Also note that the containers can be used directly; in that case <tt>push</tt> and <tt>pop</tt> have to be replaced by <tt>push_back</tt> and <tt>pop_front</tt>. |
||
Line 204: | Line 199: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
All functions, from the shell: |
All functions, from the shell: |
||
<lang Erlang> |
<lang Erlang>1> Q = fifo:new(). |
||
1> Q = fifo:new(). |
|||
{fifo,[],[]} |
{fifo,[],[]} |
||
2> fifo:empty(Q). |
2> fifo:empty(Q). |
||
Line 221: | Line 215: | ||
8> fifo:pop(fifo:new()). |
8> fifo:pop(fifo:new()). |
||
** exception error: 'empty fifo' |
** exception error: 'empty fifo' |
||
in function fifo:pop/1 |
in function fifo:pop/1</lang> |
||
</lang> |
|||
Crashing is the normal expected behavior in Erlang: let it crash, a supervisor will take responsibility of restarting processes, or the caller will take care of it. Only program for the successful cases. |
Crashing is the normal expected behavior in Erlang: let it crash, a supervisor will take responsibility of restarting processes, or the caller will take care of it. Only program for the successful cases. |
||
Line 262: | Line 255: | ||
end program FIFOTest</lang> |
end program FIFOTest</lang> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 268: | Line 260: | ||
This is an interactive J session: |
This is an interactive J session: |
||
<lang J> |
<lang J> queue=: conew 'fifo' |
||
queue=: conew 'fifo' |
|||
isEmpty__queue '' |
isEmpty__queue '' |
||
1 |
1 |
||
Line 288: | Line 279: | ||
isEmpty__queue '' |
isEmpty__queue '' |
||
1</lang> |
1</lang> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
LinkedList can always be used as a queue or stack, but not in conjunction with the Stack object provided by Java. To use a LinkedList as a stack, use the <tt>push</tt> and <tt>pop</tt> methods. A LinkedList can also be used as a double-ended queue (deque); LinkedList has implemented the Deque interface since Java 1.6+. |
LinkedList can always be used as a queue or stack, but not in conjunction with the Stack object provided by Java. To use a LinkedList as a stack, use the <tt>push</tt> and <tt>pop</tt> methods. A LinkedList can also be used as a double-ended queue (deque); LinkedList has implemented the Deque interface since Java 1.6+. |
||
<lang java> |
<lang java>import java.util.LinkedList; |
||
import java.util.LinkedList; |
|||
import java.util.Queue; |
import java.util.Queue; |
||
... |
... |
||
Line 306: | Line 295: | ||
System.out.println(queue.remove()); // 1 |
System.out.println(queue.remove()); // 1 |
||
System.out.println(queue); // [2, 3] |
System.out.println(queue); // [2, 3] |
||
System.out.println(queue.isEmpty()); // false |
System.out.println(queue.isEmpty()); // false</lang> |
||
</lang> |
|||
You can also use "offer" and "poll" methods instead of "add" and "remove", respectively. They indicate errors with the return value instead of throwing an exception. |
You can also use "offer" and "poll" methods instead of "add" and "remove", respectively. They indicate errors with the return value instead of throwing an exception. |
||
{{works with|Java|1.4}} |
{{works with|Java|1.4}} |
||
<lang java> |
<lang java>import java.util.LinkedList; |
||
import java.util.LinkedList; |
|||
... |
... |
||
LinkedList queue = new LinkedList(); |
LinkedList queue = new LinkedList(); |
||
Line 323: | Line 310: | ||
System.out.println(queue.removeFirst()); // 1 |
System.out.println(queue.removeFirst()); // 1 |
||
System.out.println(queue); // [2, 3] |
System.out.println(queue); // [2, 3] |
||
System.out.println(queue.isEmpty()); // false |
System.out.println(queue.isEmpty()); // false</lang> |
||
</lang> |
|||
=={{header|JavaScript}}== |
|||
<lang javascript>var f = new FIFO(); |
|||
print(f.empty()); |
|||
f.push(1); |
|||
f.push(2); |
|||
f.pop(); |
|||
print(f.empty()); |
|||
print(f.dequeue()) |
|||
print(f.empty()); |
|||
print(f.dequeue());</lang> |
|||
outputs: |
|||
⚫ | |||
false |
|||
2 |
|||
true |
|||
undefined</pre> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 330: | Line 334: | ||
UCB Logo comes with a protocol for treating lists as queues. |
UCB Logo comes with a protocol for treating lists as queues. |
||
<lang logo> make "fifo [] |
|||
print empty? :fifo ; true |
print empty? :fifo ; true |
||
queue "fifo 1 |
queue "fifo 1 |
||
Line 338: | Line 342: | ||
print dequeue "fifo ; 1 |
print dequeue "fifo ; 1 |
||
show :fifo ; [2 3] |
show :fifo ; [2 3] |
||
print empty? :fifo ; false |
print empty? :fifo ; false</lang> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 380: | Line 384: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<lang python> |
<lang python>import Queue |
||
import Queue |
|||
my_queue = Queue.Queue() |
my_queue = Queue.Queue() |
||
my_queue.put("foo") |
my_queue.put("foo") |
||
Line 388: | Line 391: | ||
print my_queue.get() # foo |
print my_queue.get() # foo |
||
print my_queue.get() # bar |
print my_queue.get() # bar |
||
print my_queue.get() # baz |
print my_queue.get() # baz</lang> |
||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |