Queue/Definition: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(15 intermediate revisions by 10 users not shown)
Line 1,305:
 
Error detected !!!!.
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">define :queue [][
init: [
this\items: []
]
]
 
empty?: function [this :queue][
zero? this\items
]
 
push: function [this :queue, item][
this\items: this\items ++ item
]
 
pop: function [this :queue][
ensure -> not? empty? this
result: this\items\0
this\items: remove.index this\items 0
return result
]</syntaxhighlight>
 
=={{header|ATS}}==
Line 1,830 ⟶ 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,907 ⟶ 1,978:
exit /b 0
</syntaxhighlight>
 
=={{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,704 ⟶ 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,745 ⟶ 2,833:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
templateclass queue<T>
{
T[] theArray;
Line 2,779 ⟶ 2,867:
{
if (theTale == theTop)
{ InvalidOperationException.new:("Queue is empty").raise() };
T item := theArray[theTop];
Line 3,761 ⟶ 3,849:
 
=={{header|jq}}==
Note that sinceSince jq is a purely functional language, the entityentities chosen to represent queues
representingmust a queue mustsomehow be presented asto anthe inputfunctions tothat operate anyon functionthem.
The approach taken here is to use a JSON object with a key named "queue"
that is to operate on it.
to hold the contents of the queue. This allows us to "pop" a queue by modifying
.queue while returning the popped item in the same object under a different key.
The definition of pop as given below is idiomatic in jq but implies
that popping an empty queue yields [null, []] rather than an error. An
alternative definition, pop_or_error, is also given to illustrate
how an error condition can be generated.
<syntaxhighlight lang="jq"># An empty queue:
def fifo: [];
 
There are three possibilities for defining `pop` on an empty queue:
def push(e): [e] + .;
 
# Do not make a special case of it, which in our case would mean that `{queue: []} | pop` would emit `{queue: [], item: null}`
def pop: [.[0], .[1:]];
# Raise an error
# Emit nothing
 
Here (1), is questionable as the queue might contain null, so here we define
def pop_or_error: if length == 0 then error("pop_or_error") else pop end;
`pop_or_error`, which raises an error when given an empty queue, and `pop`, which
emits the empty stream when given an empty queue.
In order to facilitate observing the evolving states of queues during processing,
we use the same `observe` function defined at [[Stack]].
 
<syntaxhighlight lang="jq">
def empty: length == 0;</syntaxhighlight>
# Input: an object
'''Examples''':
# Output: the updated object with .emit filled in from `update|emit`.
<syntaxhighlight lang="jq">fifo | pop # produces [null,[]]
# `emit` may produce a stream of values, which need not be strings.
def observe(update; emit):
def s(stream): reduce stream as $_ (null;
if $_ == null then .
elif . == null then "\($_)"
else . + "\n\($_)"
end);
.emit as $x
| update
| .emit = s($x // null, emit);
 
fifo
| push(42) # enqueue
| push(43) # enqueue
| pop # dequeue
| .[0] # the value
# produces 43
 
def fifo|push(1): as{queue: $q1[]};
 
| fifo|push(2) as $q2
# Is the input an object that represents the empty queue?
| [($q1|pop|.[0]), ($q2|pop|.[0])]
def isempty:
# produces: [1, 2]</syntaxhighlight>
type == "object"
and (.queue | length == 0); # so .queue == null and .queue == [] are equivalent
 
def push(e): .queue += [e];
 
def pop: if isempty then empty else .item = .queue[0] | .queue |= .[1:] end;
 
def pop_or_error: if isempty then error("pop_or_error") else pop end;
 
# Examples
# fifo | pop // "nothing" # produces the string "nothing"
 
fifo
| observe(push(42); "length after pushing: \(.queue | length)" )
| observe(push(43); "length after pushing: \(.queue | length)" )
| pop # dequeue
| .emit, .item
</syntaxhighlight>
'''Output'''
<pre>
length after pushing: 1
length after pushing: 2
42
</pre>
 
=={{header|Julia}}==
Line 3,968 ⟶ 4,085:
{{VI solution|LabVIEW_Queue_Definition.png}}
 
=={{header|Lambdatalk}}==
The APIs of queues are built on lambdatalk array primitives, [A.new, A.disp, A.join, A.split, A.array?, A.null?, A.empty?, A.in?, A.equal?, A.length, A.get, A.first, A.last, A.rest, A.slice, A.duplicate, A.reverse, A.concat, A.map, A.set!, A.addlast!, A.sublast!, A.addfirst!, A.subfirst!, A.reverse!, A.sort!, A.swap!, A.lib]. Note that the [A.addlast!, A.sublast!, A.addfirst!, A.subfirst!] primitives are the standard [push!, shift!, pop!, unshift!] ones.
 
<syntaxhighlight lang="scheme">
{def queue.add
{lambda {:v :q}
{let { {_ {A.addlast! :v :q}}}
} ok}}
-> queue.add
 
{def queue.get
{lambda {:q}
{let { {:v {A.first :q}}
{_ {A.subfirst! :q}}
} :v}}}
-> queue.get
 
{def queue.empty?
{lambda {:q}
{A.empty? :q}}}
-> queue.empty?
 
{def Q {A.new}} -> Q []
{queue.add 1 {Q}} -> ok [1]
{queue.add 2 {Q}} -> ok [1,2]
{queue.add 3 {Q}} -> ok [1,2,3]
{queue.get {Q}} -> 1 [2,3]
{queue.add 4 {Q}} -> ok [2,3,4]
{queue.empty? {Q}} -> false
{queue.get {Q}} -> 2 [3,4]
{queue.get {Q}} -> 3 [4]
{queue.get {Q}} -> 4 []
{queue.get {Q}} -> undefined
{queue.empty? {Q}} -> true
 
</syntaxhighlight>
=={{header|Lasso}}==
Definition:
Line 4,694 ⟶ 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 5,569 ⟶ 5,722:
Error: queue is empty
</pre>
 
=={{header|RPL}}==
It is rather easy to create queues in RPL, thanks to the list data structure. For mysterious reasons, it is very simple to add an item to a list, but quite complex to remove one: the size difference between <code>PUSH</code> and <code>POP</code> highlights it.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
'''QUEUE''' SIZE NOT ≫ ''''EMPTY'''' STO
'''QUEUE''' + ''''QUEUE'''' STO ≫ ''''PUSH'''' STO
IF '''QUEUE''' SIZE THEN
LAST { }
1 LAST 1 - FOR j
'''QUEUE''' j GET + NEXT
'''QUEUE''' ROT GET SWAP ''''QUEUE'''' STO
ELSE "ERR_Empty" END
≫ ''''POP'''' STO
|
'''EMPTY''' ''( -- )''
Test the global variable QUEUE
'''PUSH''' ''( item -- )''
Add the item at the beginning of the list
'''POP''' ''( -- item )''
Initialize stack
Copy all items except the last
in a new list
Get last item, update queue with new list
Handles the case of an empty queue
|}
{{in}}
<pre>
{ } 'QUEUE' STO
EMPTY
"The" PUSH
7 PUSH
{ Wonders } PUSH
QUEUE
EMPTY
POP
</pre>
{{out}}
<pre>
4: 1
3: { 'Wonders' 7 "The" }
2: 0
1: "The"
</pre>
===Shorter POP version===
This approach might be suitable for small queue sizes only, since it uses the stack to temporarily store the whole queue.
≪ IF '''QUEUE''' SIZE THEN
'''QUEUE''' LIST→ ROLLD LAST 1 - →LIST ''''QUEUE'''' STO
ELSE "ERR_Empty" END
≫ ''''POP'''' STO
 
=={{header|Ruby}}==
Line 6,219 ⟶ 6,435:
</pre>
 
=={{header|V (Vlang)}}==
Updated to V (Vlang) version 0.2.2
<syntaxhighlight lang="v (vlang)">const max_tail = 256
struct Queue<T> {
Line 6,312 ⟶ 6,528:
{{libheader|Wren-queue}}
The above module contains a suitable Queue class.
<syntaxhighlight lang="ecmascriptwren">import "./queue" for Queue
 
var q = Queue.new()
Line 6,448 ⟶ 6,664:
q.empty(); //-->False
q.pop();q.pop();q.pop() //-->IndexError thrown</syntaxhighlight>
 
 
{{omit from|PL/0}}
2,012

edits