Jump to content

Queue/Definition: Difference between revisions

(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
Line 5,597:
Pop: 8
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'''' STO ≫ ''''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
|
''( -- )''
Store an empty list in the QUEUE variable
''( item -- )''
Add the item at the beginning of the list
''( -- 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>
'''EMPTY'''
"The" '''PUSH'''
7 '''PUSH'''
{ Wonders } '''PUSH'''
'''QUEUE'''
'''POP'''
</pre>
{{out}}
<pre>
2: { 'Wonders' 7 "The" }
1: "The"
</pre>
 
1,150

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.