Queue/Usage: Difference between revisions

add JavaScript
(J)
(add JavaScript)
Line 9:
 
=={{header|Ada}}==
<lang ada>with FIFO;
with FIFO;
with Ada.Text_Io; use Ada.Text_Io;
Line 30 ⟶ 29:
Pop (Queue, Value);
Put_Line ("Is_Empty " & Boolean'Image (Is_Empty (Queue)));
end Queue_Test;</lang>
</lang>
Sample output:
<pre>Is_Empty TRUE</pre>
 
Is_Empty TRUE
</pre>
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=133 discussion]
Line 78 ⟶ 75:
exit(0);
}</lang>
 
=={{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>.
<lang cpp>#include <queue>
#include <queue>
#include <cassert> // for run time assertions
 
Line 126 ⟶ 123:
q.pop();
assert( q.empty() );
}</lang>
}
</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
<lang cpp> std::queue<int, std::list<int> ></lang>
<lang cpp>
 
std::queue<int, std::list<int> >
</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>.
 
Line 204 ⟶ 199:
=={{header|Erlang}}==
All functions, from the shell:
<lang Erlang>1> Q = fifo:new().
1> Q = fifo:new().
{fifo,[],[]}
2> fifo:empty(Q).
Line 221 ⟶ 215:
8> fifo:pop(fifo:new()).
** exception error: 'empty fifo'
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.
 
Line 262 ⟶ 255:
 
end program FIFOTest</lang>
 
 
=={{header|J}}==
Line 268 ⟶ 260:
This is an interactive J session:
 
<lang J> queue=: conew 'fifo'
queue=: conew 'fifo'
isEmpty__queue ''
1
Line 288 ⟶ 279:
isEmpty__queue ''
1</lang>
 
 
=={{header|Java}}==
{{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+.
<lang java>import java.util.LinkedList;
import java.util.LinkedList;
import java.util.Queue;
...
Line 306 ⟶ 295:
System.out.println(queue.remove()); // 1
System.out.println(queue); // [2, 3]
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.
 
{{works with|Java|1.4}}
<lang java>import java.util.LinkedList;
import java.util.LinkedList;
...
LinkedList queue = new LinkedList();
Line 323 ⟶ 310:
System.out.println(queue.removeFirst()); // 1
System.out.println(queue); // [2, 3]
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:
</pre>true
false
2
true
undefined</pre>
 
=={{header|Logo}}==
Line 330 ⟶ 334:
UCB Logo comes with a protocol for treating lists as queues.
 
<lang logo> make "fifo []
print empty? :fifo ; true
queue "fifo 1
Line 338 ⟶ 342:
print dequeue "fifo ; 1
show :fifo ; [2 3]
print empty? :fifo ; false</lang>
 
=={{header|OCaml}}==
Line 380 ⟶ 384:
 
=={{header|Python}}==
<lang python>import Queue
import Queue
my_queue = Queue.Queue()
my_queue.put("foo")
Line 388 ⟶ 391:
print my_queue.get() # foo
print my_queue.get() # bar
print my_queue.get() # baz</lang>
</lang>
 
=={{header|Ruby}}==
Anonymous user