Queue/Definition: Difference between revisions

m
(added Arturo)
 
(7 intermediate revisions by 6 users not shown)
Line 1,328:
this\items: remove.index this\items 0
return result
]</syntaxhighlight>
]
 
Q: to :queue []
 
push Q 1
push Q 2
push Q 3
 
print ["queue is empty?" empty? Q]
 
print ["popping:" pop Q]
print ["popping:" pop Q]
print ["popping:" pop Q]
 
print ["queue is empty?" empty? Q]</syntaxhighlight>
 
{{out}}
 
<pre>queue is empty? false
popping: 1
popping: 2
popping: 3
queue is empty? true</pre>
 
=={{header|ATS}}==
Line 1,876 ⟶ 1,854:
</pre>
 
=={{header|BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> FIFOSIZE = 1000
FOR n = 3 TO 5
PRINT "Push ";n : PROCenqueue(n)
NEXT
PRINT "Pop " ; FNdequeue
PRINT "Push 6" : PROCenqueue(6)
REPEAT
PRINT "Pop " ; FNdequeue
UNTIL FNisempty
PRINT "Pop " ; FNdequeue
END
DEF PROCenqueue(n) : LOCAL f%
DEF FNdequeue : LOCAL f% : f% = 1
DEF FNisempty : LOCAL f% : f% = 2
PRIVATE fifo(), rptr%, wptr%
DIM fifo(FIFOSIZE-1)
CASE f% OF
WHEN 0:
wptr% = (wptr% + 1) MOD FIFOSIZE
IF rptr% = wptr% ERROR 100, "Error: queue overflowed"
fifo(wptr%) = n
WHEN 1:
IF rptr% = wptr% ERROR 101, "Error: queue empty"
rptr% = (rptr% + 1) MOD FIFOSIZE
= fifo(rptr%)
WHEN 2:
= (rptr% = wptr%)
ENDCASE
ENDPROC</syntaxhighlight>
{{out}}
<pre>
Push 3
Push 4
Push 5
Pop 3
Push 6
Pop 4
Pop 5
Pop 6
Pop
Error: queue empty
</pre>
=={{header|Batch File}}==
 
Line 1,953 ⟶ 1,978:
exit /b 0
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> FIFOSIZE = 1000
FOR n = 3 TO 5
PRINT "Push ";n : PROCenqueue(n)
NEXT
PRINT "Pop " ; FNdequeue
PRINT "Push 6" : PROCenqueue(6)
REPEAT
PRINT "Pop " ; FNdequeue
UNTIL FNisempty
PRINT "Pop " ; FNdequeue
END
DEF PROCenqueue(n) : LOCAL f%
DEF FNdequeue : LOCAL f% : f% = 1
DEF FNisempty : LOCAL f% : f% = 2
PRIVATE fifo(), rptr%, wptr%
DIM fifo(FIFOSIZE-1)
CASE f% OF
WHEN 0:
wptr% = (wptr% + 1) MOD FIFOSIZE
IF rptr% = wptr% ERROR 100, "Error: queue overflowed"
fifo(wptr%) = n
WHEN 1:
IF rptr% = wptr% ERROR 101, "Error: queue empty"
rptr% = (rptr% + 1) MOD FIFOSIZE
= fifo(rptr%)
WHEN 2:
= (rptr% = wptr%)
ENDCASE
ENDPROC</syntaxhighlight>
{{out}}
<pre>
Push 3
Push 4
Push 5
Pop 3
Push 6
Pop 4
Pop 5
Pop 6
Pop
Error: queue empty
</pre>
 
=={{header|BQN}}==
Line 2,751 ⟶ 2,728:
return [reader, writer]
}</syntaxhighlight>
=={{header|EasyLang}}==
 
A double-linked list is used, which is implemented via an expandable array.
 
<syntaxhighlight>
prefix qu_
global q[] head tail .
#
proc enq n . .
if tail = 0
head = 1
else
q[tail + 2] = len q[] + 1
.
q[] &= n
q[] &= tail
q[] &= 0
tail = len q[] - 2
.
func deq .
if head = 0
return 0 / 0
.
r = q[head]
old = head
head = q[head + 2]
last = len q[]
prev = q[last - 1]
if prev <> 0
q[prev + 2] = old
.
next = q[last]
if next <> 0
q[next + 1] = old
else
tail = old
.
q[old] = q[last - 2]
q[old + 1] = q[last - 1]
q[old + 2] = q[last]
len q[] -3
if head = len q[] + 1
head = old
.
if head <> 0
q[head + 1] = 0
else
tail = 0
.
return r
.
func empty .
return if head = 0
.
prefix
#
qu_enq 2
qu_enq 5
qu_enq 7
while qu_empty = 0
print qu_deq
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
There is no native queue type in EchoLisp. make-Q implements queues in message passing style, using vector operations. Conversions from-to lists are also provided.
Line 2,792 ⟶ 2,833:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
templateclass queue<T>
{
T[] theArray;
Line 2,826 ⟶ 2,867:
{
if (theTale == theTop)
{ InvalidOperationException.new:("Queue is empty").raise() };
T item := theArray[theTop];
Line 4,806 ⟶ 4,847:
 
<syntaxhighlight lang="perl">use Carp;
sub mypushmy push :prototype(\@@) {my($list,@things)=@_; push @$list, @things}
sub mypopmaypop :prototype(\@) {my($list)=@_; @$list or croak "Empty"; shift @$list }
sub empty :prototype(@) {not @_}</syntaxhighlight>
 
Example:
Line 6,487 ⟶ 6,528:
{{libheader|Wren-queue}}
The above module contains a suitable Queue class.
<syntaxhighlight lang="ecmascriptwren">import "./queue" for Queue
 
var q = Queue.new()
1,980

edits