Anonymous user
Queue/Definition: Difference between revisions
{{out}}
(add UNIX Shell) |
({{out}}) |
||
Line 1:
{{task|Data Structures}}{{Data structure}}
[[File:Fifo.gif|frame|right|Illustration of FIFO behavior]]
Implement a FIFO queue.
Elements are added at one side and popped from the other in the order of insertion.
Operations:
Line 526 ⟶ 527:
</lang>
{{out}}
<pre>
empty? 1
push a
Line 535 ⟶ 537:
empty? 1
Popping from empty queue.
</pre>
=={{header|Batch File}}==
This solution uses an environment variable naming convention to implement a queue as a pseudo object containing a pseudo dynamic array and head and tail attributes, as well as an empty "method" that is a sort of macro.
The implementation depends on delayed expansion being enabled at the time of each call to a queue function.
More complex variations can be written that remove this limitation.
<lang dos>
Line 645 ⟶ 650:
ENDCASE
ENDPROC</lang>
{{out}}
<pre>
Push 3
Line 1,030 ⟶ 1,035:
console.log v
</lang>
{{out}}
<
> time coffee fifo.coffee
{ number: 1 }
Line 1,039 ⟶ 1,044:
user 0m2.089s
sys 0m0.265s
</
=={{header|Common Lisp}}==
Line 1,274 ⟶ 1,279:
=={{header|Erlang}}==
The standard way to manage fifo in functional programming is to use a pair of list for the fifo queue, one is the input, the other is the output.
When the output is empty just take the input list and reverse it.
<lang Erlang>-module(fifo).
-export([new/0, push/2, pop/1, empty/1]).
Line 1,571 ⟶ 1,577:
try { q.dequeue() } catch (NoSuchElementException e) { println e }</lang>
{{out}}
<pre>Queue:[Crosby, Stills, Nash, Young]
Queue:[Stills, Nash, Young]
Line 1,584 ⟶ 1,590:
=={{header|Haskell}}==
The standard way to manage fifo in functional programming is to use a pair of list for the fifo queue, one is the input, the other is the output.
When the output is empty just take the input list and reverse it.
<lang haskell>data Fifo a = F [a] [a]
Line 1,647 ⟶ 1,654:
</lang>
{{out}}
<pre>Popped value: 1
Popped value: 2
Line 2,093 ⟶ 2,100:
=={{header|OCaml}}==
The standard way to manage fifo in functional programming is to use a pair of list for the fifo queue, one is the input, the other is the output.
When the output is empty just take the input list and reverse it.
<lang ocaml>module FIFO : sig
Line 2,470 ⟶ 2,478:
(fifo 'Queue '(a b c)) # and a list (a b c)
Queue # Show the queue</lang>
{{out}}
<pre>->((a b c) 1 abc "abc" .)</pre>
Line 2,571 ⟶ 2,579:
Debug Pop()</lang>
{{out}}
<pre>
3
1
Line 2,577 ⟶ 2,586:
Pop(), out of range. Error at line 17
0
</pre>
=={{header|Python}}==
Line 2,896 ⟶ 2,906:
print ["Trying to pop an empty queue yields:" q/pop]</lang>
{{out}}
<pre>Queue is not empty.
a
Line 2,938 ⟶ 2,947:
else say 'There are' queued() 'elements in the queue'
return</lang>
{{out}}
<pre style="overflow: scroll;">
Queue is empty
Line 3,075 ⟶ 3,084:
println("dequeue = " + q.dequeue)
println("isEmpty = " + q.isEmpty)</lang>
{{out}}
<pre>isEmpty = true
dequeue(empty) failed.
Line 3,310 ⟶ 3,319:
print "peek: $(queue_peek foo)"; queue_pop foo</lang>
{{out}}
<pre>foo is empty
foo is not empty
|