Queue/Usage: Difference between revisions

Content added Content deleted
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 27: Line 27:
q:len . cr \ displays 0
q:len . cr \ displays 0
</lang>
</lang>

=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with FIFO;
<lang ada>with FIFO;
Line 103: Line 104:
</pre>
</pre>
'''See also:''' [[Stack#ALGOL_68|Stack]]
'''See also:''' [[Stack#ALGOL_68|Stack]]

=={{header|App Inventor}}==
This Rosetta Code Task requires that the queue operations of push (enqueue), pop (dequeue) and empty be demonstrated with App Inventor.<br>
This is easy to do as those operations are basically available in a slightly different form as list operations.<br>
In addition for this example, I added a top function to view the first item in the queue.<br>
<br>
The solution is a complete (although greatly simplified) hamburger restaurant where the customers and orders are the queues.<br>

Customers enter the restaurant at random intervals between 2 and 10 seconds (Customers Clock Timer)<br>
Each customer will request a random item from the menu.<br>
If the item is not available, the customer leaves.<br>
If that item is available (there are only 30 of each item) then the order is placed and payment is accepted (push|enqueue Customer, push|enqueue Order).<br>
Once an order is placed, the customer must wait for the meal to be prepared -- each menu item takes a different number of seconds to prepare (Orders Clock Timer.)<br>
Once the item is prepared, their customer name and the ordered item are removed from the queues (pop|dequeue Customer, pop|dequeue Order).<br>
If there are no pending orders, (empty Orders queue) the cook just waits for one to be placed (the orders clock continues to run to poll for new orders by testing if the Orders queue is not empty.)<br>
Eventually, all items will have been sold, and the store manager will empty the cash register and fly to Tahiti with the waitress.<br>
The eager -- but destined to be frustrated customers -- will continue to request their random items, forever. :)<br>
[https://lh6.googleusercontent.com/-dTvs9totvDE/Uu3ZiFeE90I/AAAAAAAAJ-w/lJBVHOd-p0g/s1600/Untitled.png CLICK HERE TO VIEW THE CODE BLOCKS AND ANDROID APP SCREEN]
---
END


=={{header|AppleScript}}==
=={{header|AppleScript}}==
Line 144: Line 165:
(*1*)
(*1*)
</pre>
</pre>

=={{header|App Inventor}}==
This Rosetta Code Task requires that the queue operations of push (enqueue), pop (dequeue) and empty be demonstrated with App Inventor.<br>
This is easy to do as those operations are basically available in a slightly different form as list operations.<br>
In addition for this example, I added a top function to view the first item in the queue.<br>
<br>
The solution is a complete (although greatly simplified) hamburger restaurant where the customers and orders are the queues.<br>

Customers enter the restaurant at random intervals between 2 and 10 seconds (Customers Clock Timer)<br>
Each customer will request a random item from the menu.<br>
If the item is not available, the customer leaves.<br>
If that item is available (there are only 30 of each item) then the order is placed and payment is accepted (push|enqueue Customer, push|enqueue Order).<br>
Once an order is placed, the customer must wait for the meal to be prepared -- each menu item takes a different number of seconds to prepare (Orders Clock Timer.)<br>
Once the item is prepared, their customer name and the ordered item are removed from the queues (pop|dequeue Customer, pop|dequeue Order).<br>
If there are no pending orders, (empty Orders queue) the cook just waits for one to be placed (the orders clock continues to run to poll for new orders by testing if the Orders queue is not empty.)<br>
Eventually, all items will have been sold, and the store manager will empty the cash register and fly to Tahiti with the waitress.<br>
The eager -- but destined to be frustrated customers -- will continue to request their random items, forever. :)<br>
[https://lh6.googleusercontent.com/-dTvs9totvDE/Uu3ZiFeE90I/AAAAAAAAJ-w/lJBVHOd-p0g/s1600/Untitled.png CLICK HERE TO VIEW THE CODE BLOCKS AND ANDROID APP SCREEN]
---
END


=={{header|Arturo}}==
=={{header|Arturo}}==
Line 394: Line 395:


exit(0);
exit(0);
}</lang>

=={{header|C sharp}}==
In C# we can use the Queue<T> class in the .NET 2.0 framework.
<lang csharp>using System;
using System.Collections.Generic;

namespace RosettaCode
{
class Program
{
static void Main()
{
// Create a queue and "push" items into it
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(3);
queue.Enqueue(5);

// "Pop" items from the queue in FIFO order
Console.WriteLine(queue.Dequeue()); // 1
Console.WriteLine(queue.Dequeue()); // 3
Console.WriteLine(queue.Dequeue()); // 5

// To tell if the queue is empty, we check the count
bool empty = queue.Count == 0;
Console.WriteLine(empty); // "True"

// If we try to pop from an empty queue, an exception
// is thrown.
try
{
queue.Dequeue();
}
catch (InvalidOperationException exception)
{
Console.WriteLine(exception.Message); // "Queue empty."
}
}
}
}</lang>
}</lang>


Line 449: Line 490:


(and add <tt>#include <list></tt>, of course). Also note that the containers can be used directly; in that case <tt>push</tt> and <tt>pop</tt> have to be replaced by <tt>push_back</tt> and <tt>pop_front</tt>.
(and add <tt>#include <list></tt>, of course). Also note that the containers can be used directly; in that case <tt>push</tt> and <tt>pop</tt> have to be replaced by <tt>push_back</tt> and <tt>pop_front</tt>.

=={{header|C sharp}}==
In C# we can use the Queue<T> class in the .NET 2.0 framework.
<lang csharp>using System;
using System.Collections.Generic;

namespace RosettaCode
{
class Program
{
static void Main()
{
// Create a queue and "push" items into it
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(3);
queue.Enqueue(5);

// "Pop" items from the queue in FIFO order
Console.WriteLine(queue.Dequeue()); // 1
Console.WriteLine(queue.Dequeue()); // 3
Console.WriteLine(queue.Dequeue()); // 5

// To tell if the queue is empty, we check the count
bool empty = queue.Count == 0;
Console.WriteLine(empty); // "True"

// If we try to pop from an empty queue, an exception
// is thrown.
try
{
queue.Dequeue();
}
catch (InvalidOperationException exception)
{
Console.WriteLine(exception.Message); // "Queue empty."
}
}
}
}</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 761: Line 762:
Third
Third
Queue is empty.</pre>
Queue is empty.</pre>

=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==
This uses the definition from [[Queue/Definition#Déjà Vu]]
This uses the definition from [[Queue/Definition#Déjà Vu]]
Line 800: Line 802:


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.
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|Elena}}==
=={{header|Elena}}==
ELENA 4.x :
ELENA 4.x :
Line 1,070: Line 1,073:


end program FIFOTest</lang>
end program FIFOTest</lang>



=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Line 1,574: Line 1,576:
// => true
// => true
</lang>
</lang>



=={{header|Logo}}==
=={{header|Logo}}==
Line 1,589: Line 1,590:
show :fifo ; [2 3]
show :fifo ; [2 3]
print empty? :fifo ; false</lang>
print empty? :fifo ; false</lang>

=={{header|Lua}}==
=={{header|Lua}}==
Uses the queue-definition given at [[Queue/Definition#Lua]]
Uses the queue-definition given at [[Queue/Definition#Lua]]
Line 1,634: Line 1,636:
queue[empty](q);
queue[empty](q);
>>>true</lang>
>>>true</lang>



=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 1,871: Line 1,872:
2345
2345
array is empty</lang>
array is empty</lang>

=={{header|Perl 6}}==

Perl 6 maintains the same list operators of Perl, for this task, the operations are:
<lang>push (aka enqueue) -- @list.push
pop (aka dequeue) -- @list.shift
empty -- !@list.elems</lang>
but there's also @list.pop which removes a item from the end,
and @list.unshift which add a item on the start of the list.<br/>
Example:
<lang perl6>my @queue = < a >;

@queue.push('b', 'c'); # [ a, b, c ]

say @queue.shift; # a
say @queue.pop; # c

say @queue.perl; # [ b ]
say @queue.elems; # 1

@queue.unshift('A'); # [ A, b ]
@queue.push('C'); # [ A, b, C ]</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,190: Line 2,169:
false.
false.
</pre>
</pre>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>NewList MyStack()
<lang PureBasic>NewList MyStack()
Line 2,264: Line 2,244:


(queue-empty? queue) ; #t</lang>
(queue-empty? queue) ; #t</lang>

=={{header|Raku}}==
(formerly Perl 6)

Perl 6 maintains the same list operators of Perl, for this task, the operations are:
<lang>push (aka enqueue) -- @list.push
pop (aka dequeue) -- @list.shift
empty -- !@list.elems</lang>
but there's also @list.pop which removes a item from the end,
and @list.unshift which add a item on the start of the list.<br/>
Example:
<lang perl6>my @queue = < a >;

@queue.push('b', 'c'); # [ a, b, c ]

say @queue.shift; # a
say @queue.pop; # c

say @queue.perl; # [ b ]
say @queue.elems; # 1

@queue.unshift('A'); # [ A, b ]
@queue.push('C'); # [ A, b, C ]</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==
Line 2,502: Line 2,505:
=={{header|Stata}}==
=={{header|Stata}}==
See [[Singly-linked list/Element definition#Stata]].
See [[Singly-linked list/Element definition#Stata]].

=={{header|Tcl}}==
=={{header|Tcl}}==
See [[FIFO#Tcl|FIFO]] for operation implementations:
See [[FIFO#Tcl|FIFO]] for operation implementations:
Line 2,550: Line 2,554:
End Sub</lang>{{out}}
End Sub</lang>{{out}}
<pre>One Two Three True</pre>
<pre>One Two Three True</pre>

=={{header|Wart}}==
=={{header|Wart}}==
See [[FIFO#Wart|FIFO]] for implementation.
See [[FIFO#Wart|FIFO]] for implementation.