Queue/Usage: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 173:
q:len . cr \ displays 0
</lang>
 
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<lang Action!>CARD EndProg ;required for ALLOCATE.ACT
 
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
 
DEFINE PTR="CARD"
DEFINE NODE_SIZE="4"
TYPE QueueNode=[PTR data,nxt]
 
QueueNode POINTER queueFront,queueRear
 
BYTE FUNC IsEmpty()
IF queueFront=0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC Push(CHAR ARRAY v)
QueueNode POINTER node
 
node=Alloc(NODE_SIZE)
node.data=v
node.nxt=0
IF IsEmpty() THEN
queueFront=node
ELSE
queueRear.nxt=node
FI
queueRear=node
RETURN
 
PTR FUNC Pop()
QueueNode POINTER node
CHAR ARRAY v
IF IsEmpty() THEN
PrintE("Error: queue is empty!")
Break()
FI
 
node=queueFront
v=node.data
queueFront=node.nxt
Free(node,NODE_SIZE)
RETURN (v)
 
PROC TestIsEmpty()
IF IsEmpty() THEN
PrintE("Queue is empty")
ELSE
PrintE("Queue is not empty")
FI
RETURN
 
PROC TestPush(CHAR ARRAY v)
PrintF("Push: %S%E",v)
Push(v)
RETURN
 
PROC TestPop()
CHAR ARRAY v
 
Print("Pop: ")
v=Pop()
PrintE(v)
RETURN
 
PROC Main()
AllocInit(0)
queueFront=0
queueRear=0
 
Put(125) PutE() ;clear screen
 
TestIsEmpty()
TestPush("foo")
TestIsEmpty()
TestPush("bar")
TestPop()
TestIsEmpty()
TestPush("baz")
TestPop()
TestIsEmpty()
TestPop()
TestIsEmpty()
TestPop()
RETURN</lang>
{{out}}
Error at the end of the program is intentional.
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Queue_Usage.png Screenshot from Atari 8-bit computer]
<pre>
Queue is empty
Push: foo
Queue is not empty
Push: bar
Pop: foo
Queue is not empty
Push: baz
Pop: bar
Queue is not empty
Pop: baz
Queue is empty
Pop: Error: queue is empty!
 
RETURN
Error: 128
</pre>
 
=={{header|Ada}}==
Anonymous user