Stack: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add CLU) |
(Added solution for Action!) |
||
Line 360: | Line 360: | ||
pop -> Stack3 = empty |
pop -> Stack3 = empty |
||
</pre> |
|||
=={{header|Action!}}== |
|||
===Static memory=== |
|||
<lang Action!>DEFINE MAXSIZE="200" |
|||
BYTE ARRAY stack(MAXSIZE) |
|||
BYTE stacksize=[0] |
|||
BYTE FUNC IsEmpty() |
|||
IF stacksize=0 THEN |
|||
RETURN (1) |
|||
FI |
|||
RETURN (0) |
|||
PROC Push(BYTE v) |
|||
IF stacksize=maxsize THEN |
|||
PrintE("Error: stack is full!") |
|||
Break() |
|||
FI |
|||
stack(stacksize)=v |
|||
stacksize==+1 |
|||
RETURN |
|||
BYTE FUNC Pop() |
|||
IF IsEmpty() THEN |
|||
PrintE("Error: stack is empty!") |
|||
Break() |
|||
FI |
|||
stacksize==-1 |
|||
RETURN (stack(stacksize)) |
|||
PROC TestIsEmpty() |
|||
IF IsEmpty() THEN |
|||
PrintE("Stack is empty") |
|||
ELSE |
|||
PrintE("Stack 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=== |
|||
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="3" |
|||
TYPE StackNode=[BYTE data PTR nxt] |
|||
StackNode POINTER stack |
|||
BYTE FUNC IsEmpty() |
|||
IF stack=0 THEN |
|||
RETURN (1) |
|||
FI |
|||
RETURN (0) |
|||
PROC Push(BYTE v) |
|||
StackNode POINTER node |
|||
node=Alloc(NODE_SIZE) |
|||
node.data=v |
|||
node.nxt=stack |
|||
stack=node |
|||
RETURN |
|||
BYTE FUNC Pop() |
|||
StackNode POINTER node |
|||
BYTE v |
|||
IF IsEmpty() THEN |
|||
PrintE("Error stack is empty!") |
|||
Break() |
|||
FI |
|||
node=stack |
|||
v=node.data |
|||
stack=node.nxt |
|||
Free(node,NODE_SIZE) |
|||
RETURN (v) |
|||
PROC TestIsEmpty() |
|||
IF IsEmpty() THEN |
|||
PrintE("Stack is empty") |
|||
ELSE |
|||
PrintE("Stack 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) |
|||
stack=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 program is intentional. |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Stack_array.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Stack is empty |
|||
Push: 10 |
|||
Stack is not empty |
|||
Push: 31 |
|||
Pop: 31 |
|||
Stack is not empty |
|||
Push: 5 |
|||
Pop: 5 |
|||
Pop: 10 |
|||
Pop: Error: stack is empty! |
|||
RETURN |
|||
Error: 128 |
|||
</pre> |
</pre> |
||