Queue/Definition: Difference between revisions

→‎{{header|REXX}}: version 2 elided. -- ~~~~
m (→‎version 2: undented subroutine fence. -- ~~~~)
(→‎{{header|REXX}}: version 2 elided. -- ~~~~)
Line 2,578:
 
=={{header|REXX}}==
Support for '''LIFO''' & '''FIFO''' queues is built into the [[REXX|Rexx]] language.
<br>The following are supported in REXX:
<br>The &quot;<code>PUSH</code>&quot; <small>(LIFO)</small>, &quot;<code>QUEUE</code>&quot; <small>(FIFO)</small>, &quot;<code>PULL</code>&quot; (and/or &quot;<code>PARSE PULL ...</code>&quot;; &quot;<code>PULL</code>&quot; is a synonym for &quot;<code>PARSE UPPER PULL ...</code>&quot;) <small>(dequeue)</small> keywords in conjunction with the <code>QUEUED()</code> built&ndash;in function deliver this capability.
* '''PUSH''' (lifo)
===version 1===
* '''QUEUE''' (fifo)
<lang Rexx>
* '''PULL''' --- which is a short version of:
/* Rexx */
* '''PARSE UPPER PULL'''
 
* '''PARSE PULL'''
Do
* '''QUEUED()''' [a BIF which returns the number of queued entries.]
Call viewQueue
 
a = "Fred"
Push /* Puts a null line onto the queue */
Push a 2 /* Puts "Fred 2" onto the queue */
Call viewQueue
 
a = "Toft"
Queue a 2 /* Enqueues "Toft 2" */
Queue /* Enqueues a null line behind the last */
Call viewQueue
 
Do q_ = 1 while queued() > 0
Parse pull line
Say right(q_, 3)':' line
End q_
Call viewQueue
 
Return
End
Exit
 
viewQueue:
Procedure
Do
If queued() = 0 then Do
Say 'Queue is empty'
End
else Do
Say 'There are' queued() 'elements in the queue'
End
 
Return
End
Exit
</lang>
 
'''Output:'''
<pre style="height: 20ex; overflow: scroll;">
Queue is empty
There are 2 elements in the queue
There are 4 elements in the queue
1: Fred 2
2:
3: Toft 2
4:
Queue is empty
</pre>
===version 2===
This REXX program performs like version 1 but with superfluous statements removed.
<lang rexx>/*REXX program to demonstrate FIFO queue usage by some simple operations*/
call viewQueue
Line 2,647 ⟶ 2,598:
do n=1 while queued()\==0
parse pull xxx
say "queue entry" n': ' xxx
end /*n*/
call viewQueue
Line 2,655 ⟶ 2,606:
else say 'There are' queued() 'elements in the queue'
return</lang>
'''Output:output'''
<pre style="height: 20ex; overflow: scroll;">
Say 'Queue is empty'
Say 'There are' queued()2 'elements in the queue'
There are 24 elements in the queue
queue entry 1: Fred 2
queue entry 2:
queue entry 3: Toft 2
queue entry 4:
Queue is empty
</langpre>
 
=={{header|Ruby}}==