Queue/Usage: Difference between revisions

Replaced the deprecated and no longer existing module “queues” with the module “deques”.
(obsolete)
(Replaced the deprecated and no longer existing module “queues” with the module “deques”.)
Line 1,770:
 
=={{header|Nim}}==
Nim standard library no longer provides a “queues” module, but it provides the more powerful module “deques” which allows to manage FIFO and stacks. Internally, this module uses a sequence and,thus, is more efficient than a linked list implementation.
<lang nim>import queues
 
When popping from an empty list, the module raises an IndexDefect which, as defect, is considered to be non catchable. In fact, by default, with version 1.4 of Nim the defects are still catchable but this may (will) change in some next version. The option <code>--panics:on|off</code> allows to control this behavior. Here, we have chosen to not try to catch the exception and the program terminates in error when trying to pop a fourth element from the queue.
var deq: TQueue[int] = initQueue[int]()
 
<lang nim>import queuesdeques
deq.enqueue(26)
 
deq.add(99) # same as enqueue()
var queue = initDeque[int]()
deq.enqueue(2)
 
echo("Dequeue size: ", deq.len())
echo("De-queue: ", deq.dequeueaddLast()26)
echo("De-queue: ", deq.dequeueaddLast()99)
echo("De-queue: ", deq.dequeueaddLast()2)
echo( "DequeueQueue size: ", deqqueue.len())
#echo("De-queue: ", deq.dequeue()) # dequeue an empty dequeue raises [EAssertionFailed]</lang>
echo "Popping: ", queue.popFirst()
echo "Popping: ", queue.popFirst()
echo "Popping: ", queue.popFirst()
echo "Popping: ", queue.popFirst()</lang>
{{out}}
<pre>DequeueQueue size: 3
De-queuePopping: 26
De-queuePopping: 99
Popping: 2
De-queue: 2</pre>
/home/lse/Documents/nim/Rosetta/queue_usage.nim(13) queue_usage
/home/lse/.choosenim/toolchains/nim-1.4.4/lib/pure/collections/deques.nim(113) popFirst
Error: unhandled exception: Empty deque. [IndexDefect]</pre>
 
=={{header|Objeck}}==
Anonymous user