Queue/Definition: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
|||
Line 317: | Line 317: | ||
(defun empty (xs) |
(defun empty (xs) |
||
(endp xs))</lang> |
(endp xs))</lang> |
||
=={{header|Action!}}== |
|||
===Static memory=== |
|||
Following solution uses fixed array as a buffer for the queue. |
|||
<lang Action!>DEFINE MAXSIZE="200" |
|||
BYTE ARRAY queue(MAXSIZE) |
|||
BYTE queueFront=[0],queueRear=[0] |
|||
BYTE FUNC IsEmpty() |
|||
IF queueFront=queueRear THEN |
|||
RETURN (1) |
|||
FI |
|||
RETURN (0) |
|||
PROC Push(BYTE v) |
|||
BYTE rear |
|||
rear=queueRear+1 |
|||
IF rear=MAXSIZE THEN |
|||
rear=0 |
|||
FI |
|||
IF rear=queueFront THEN |
|||
PrintE("Error: queue is full!") |
|||
Break() |
|||
FI |
|||
queue(queueRear)=v |
|||
queueRear=rear |
|||
RETURN |
|||
BYTE FUNC Pop() |
|||
BYTE v |
|||
IF IsEmpty() THEN |
|||
PrintE("Error: queue is empty!") |
|||
Break() |
|||
FI |
|||
v=queue(queueFront) |
|||
queueFront==+1 |
|||
IF queueFront=MAXSIZE THEN |
|||
queueFront=0 |
|||
FI |
|||
RETURN (v) |
|||
PROC TestIsEmpty() |
|||
IF IsEmpty() THEN |
|||
PrintE("Queue is empty") |
|||
ELSE |
|||
PrintE("Queue is not empty") |
|||
FI |
|||
RETURN |
|||
PROC TestPush(BYTE v) |
|||
PrintF("Push: %B%E",v) |
|||
Push(v) |
|||
RETURN |
|||
PROC TestPop() |
|||
BYTE v |
|||
Print("Pop: ") |
|||
v=Pop() |
|||
PrintBE(v) |
|||
RETURN |
|||
PROC Main() |
|||
TestIsEmpty() |
|||
TestPush(10) |
|||
TestIsEmpty() |
|||
TestPush(31) |
|||
TestPop() |
|||
TestIsEmpty() |
|||
TestPush(5) |
|||
TestPop() |
|||
TestPop() |
|||
TestPop() |
|||
RETURN</lang> |
|||
===Dynamic memory=== |
|||
Following solution uses module for dynamic memory allocation. The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre> |
|||
<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="3" |
|||
TYPE QueueNode=[BYTE data PTR nxt] |
|||
QueueNode POINTER queueFront,queueRear |
|||
BYTE FUNC IsEmpty() |
|||
IF queueFront=0 THEN |
|||
RETURN (1) |
|||
FI |
|||
RETURN (0) |
|||
PROC Push(BYTE 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 |
|||
BYTE FUNC Pop() |
|||
QueueNode POINTER node |
|||
BYTE 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(BYTE v) |
|||
PrintF("Push: %B%E",v) |
|||
Push(v) |
|||
RETURN |
|||
PROC TestPop() |
|||
BYTE v |
|||
Print("Pop: ") |
|||
v=Pop() |
|||
PrintBE(v) |
|||
RETURN |
|||
PROC Main() |
|||
AllocInit(0) |
|||
queueFront=0 |
|||
queueRear=0 |
|||
Put(125) PutE() ;clear screen |
|||
TestIsEmpty() |
|||
TestPush(10) |
|||
TestIsEmpty() |
|||
TestPush(31) |
|||
TestPop() |
|||
TestIsEmpty() |
|||
TestPush(5) |
|||
TestPop() |
|||
TestPop() |
|||
TestPop() |
|||
RETURN</lang> |
|||
{{out}} |
|||
Error at the end of the program is intentional. |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Queue_array.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Queue is empty |
|||
Push: 10 |
|||
Queue is not empty |
|||
Push: 31 |
|||
Pop: 10 |
|||
Queue is not empty |
|||
Push: 5 |
|||
Pop: 31 |
|||
Pop: 5 |
|||
Pop: Error: queue is empty! |
|||
RETURN |
|||
Error: 128 |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |