Queue/Definition: Difference between revisions

Content added Content deleted
(add UNIX Shell)
({{out}})
Line 1: Line 1:
{{task|Data Structures}}{{Data structure}}
{{task|Data Structures}}{{Data structure}}
[[File:Fifo.gif|frame|right|Illustration of FIFO behavior]]
[[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.
Implement a FIFO queue.
Elements are added at one side and popped from the other in the order of insertion.


Operations:
Operations:
Line 526: Line 527:
</lang>
</lang>


{{out}}
Example output:
<pre>
empty? 1
empty? 1
push a
push a
Line 535: Line 537:
empty? 1
empty? 1
Popping from empty queue.
Popping from empty queue.
</pre>


=={{header|Batch File}}==
=={{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.
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>
<lang dos>
Line 645: Line 650:
ENDCASE
ENDCASE
ENDPROC</lang>
ENDPROC</lang>
{{out}}
'''Output:'''
<pre>
<pre>
Push 3
Push 3
Line 1,030: Line 1,035:
console.log v
console.log v
</lang>
</lang>
{{out}}
output
<lang>
<pre>
> time coffee fifo.coffee
> time coffee fifo.coffee
{ number: 1 }
{ number: 1 }
Line 1,039: Line 1,044:
user 0m2.089s
user 0m2.089s
sys 0m0.265s
sys 0m0.265s
</lang>
</pre>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 1,274: Line 1,279:


=={{header|Erlang}}==
=={{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.
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).
<lang Erlang>-module(fifo).
-export([new/0, push/2, pop/1, empty/1]).
-export([new/0, push/2, pop/1, empty/1]).
Line 1,571: Line 1,577:
try { q.dequeue() } catch (NoSuchElementException e) { println e }</lang>
try { q.dequeue() } catch (NoSuchElementException e) { println e }</lang>


{{out}}
Output:
<pre>Queue:[Crosby, Stills, Nash, Young]
<pre>Queue:[Crosby, Stills, Nash, Young]
Queue:[Stills, Nash, Young]
Queue:[Stills, Nash, Young]
Line 1,584: Line 1,590:
=={{header|Haskell}}==
=={{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.
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]
<lang haskell>data Fifo a = F [a] [a]
Line 1,647: Line 1,654:
</lang>
</lang>


{{out}}
Output:
<pre>Popped value: 1
<pre>Popped value: 1
Popped value: 2
Popped value: 2
Line 2,093: Line 2,100:
=={{header|OCaml}}==
=={{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.
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
<lang ocaml>module FIFO : sig
Line 2,470: Line 2,478:
(fifo 'Queue '(a b c)) # and a list (a b c)
(fifo 'Queue '(a b c)) # and a list (a b c)
Queue # Show the queue</lang>
Queue # Show the queue</lang>
{{out}}
Output:
<pre>->((a b c) 1 abc "abc" .)</pre>
<pre>->((a b c) 1 abc "abc" .)</pre>


Line 2,571: Line 2,579:
Debug Pop()</lang>
Debug Pop()</lang>


{{out}}
'''Outputs
<pre>
3
3
1
1
Line 2,577: Line 2,586:
Pop(), out of range. Error at line 17
Pop(), out of range. Error at line 17
0
0
</pre>


=={{header|Python}}==
=={{header|Python}}==
Line 2,896: Line 2,906:
print ["Trying to pop an empty queue yields:" q/pop]</lang>
print ["Trying to pop an empty queue yields:" q/pop]</lang>


{{out}}
Output:

<pre>Queue is not empty.
<pre>Queue is not empty.
a
a
Line 2,938: Line 2,947:
else say 'There are' queued() 'elements in the queue'
else say 'There are' queued() 'elements in the queue'
return</lang>
return</lang>
{{out}}
'''output'''
<pre style="overflow: scroll;">
<pre style="overflow: scroll;">
Queue is empty
Queue is empty
Line 3,075: Line 3,084:
println("dequeue = " + q.dequeue)
println("dequeue = " + q.dequeue)
println("isEmpty = " + q.isEmpty)</lang>
println("isEmpty = " + q.isEmpty)</lang>
{{out}}
Output:
<pre>isEmpty = true
<pre>isEmpty = true
dequeue(empty) failed.
dequeue(empty) failed.
Line 3,310: Line 3,319:
print "peek: $(queue_peek foo)"; queue_pop foo</lang>
print "peek: $(queue_peek foo)"; queue_pop foo</lang>


{{out}}
Output:
<pre>foo is empty
<pre>foo is empty
foo is not empty
foo is not empty