Jump to content

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.
Elements are added at one side and popped from the other in the order of insertion.
 
Operations:
Line 526 ⟶ 527:
</lang>
 
{{out}}
Example output:
<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.
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}}
'''Output:'''
<pre>
Push 3
Line 1,030 ⟶ 1,035:
console.log v
</lang>
{{out}}
output
<langpre>
> time coffee fifo.coffee
{ number: 1 }
Line 1,039 ⟶ 1,044:
user 0m2.089s
sys 0m0.265s
</langpre>
 
=={{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.
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}}
Output:
<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.
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}}
Output:
<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.
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}}
Output:
<pre>->((a b c) 1 abc "abc" .)</pre>
 
Line 2,571 ⟶ 2,579:
Debug Pop()</lang>
 
{{out}}
'''Outputs
<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}}
Output:
 
<pre>Queue is not empty.
a
Line 2,938 ⟶ 2,947:
else say 'There are' queued() 'elements in the queue'
return</lang>
{{out}}
'''output'''
<pre style="overflow: scroll;">
Queue is empty
Line 3,075 ⟶ 3,084:
println("dequeue = " + q.dequeue)
println("isEmpty = " + q.isEmpty)</lang>
{{out}}
Output:
<pre>isEmpty = true
dequeue(empty) failed.
Line 3,310 ⟶ 3,319:
print "peek: $(queue_peek foo)"; queue_pop foo</lang>
 
{{out}}
Output:
<pre>foo is empty
foo is not empty
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.