Queue/Usage: Difference between revisions

m
m (syntax highlighting fixup automation)
(9 intermediate revisions by 7 users not shown)
Line 421:
(*1*)
</pre>
 
=={{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
]
 
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|Astro}}==
Line 523 ⟶ 569:
}</syntaxhighlight>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> FIFOSIZE = 1000
Line 1,134 ⟶ 1,181:
 
E also has queues in the standard library such as <code>&lt;import:org.erights.e.examples.concurrency.makeQueue></code>, but they are designed for concurrency purposes and do not report emptiness but rather return a promise for the next element.
 
=={{header|EasyLang}}==
Uses the queue-definition given at [[Queue/Definition#EasyLang]]
<syntaxhighlight>
#
# queue definition
#
##
qu_enq 2
qu_enq 5
qu_enq 7
while qu_empty = 0
print qu_deq
.
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'collections;
import extensions;
Line 1,145 ⟶ 1,207:
var queue := new Queue();
queue.push:(1);
queue.push:(3);
queue.push:(5);
// "Pop" items from the queue in FIFO order
Line 1,159 ⟶ 1,221:
// If we try to pop from an empty queue, an exception
// is thrown.
queue.pop() |\\ on::(e){ console.writeLine:("Queue empty.") }
}</syntaxhighlight>
 
Line 1,877 ⟶ 1,939:
Can't remove elements from an empty queue
</pre>
 
=={{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}}==
Line 2,784 ⟶ 2,883:
 
=={{header|Ruby}}==
Sample usage at [[FIFO#Ruby]].<p>
Or use the built-in Queue class:
 
<syntaxhighlight lang="ruby">q = Queue.new
q.push "Hello" # .enq is an alias
q.push "world"
p q.pop # .deq is an alias
p q.empty? # => false
</syntaxhighlight>
 
=={{header|Rust}}==
Line 3,016 ⟶ 3,123:
=={{header|Wren}}==
{{libheader|Wren-queue}}
<syntaxhighlight lang="ecmascriptwren">import "./queue" for Queue
 
var q = Queue.new()
2,063

edits