How to resolve the algorithm Stack step by step in the Oberon-2 programming language
How to resolve the algorithm Stack step by step in the Oberon-2 programming language
Table of Contents
Problem Statement
A stack is a container of elements with last in, first out access policy. Sometimes it also called LIFO. The stack is accessed through its top. The basic stack operations are:
Sometimes the last pushed stack element is made accessible for immutable access (for read) or mutable access (for write):
Stacks allow a very simple hardware implementation. They are common in almost all processors. In programming, stacks are also very popular for their way (LIFO) of resource management, usually memory. Nested scopes of language objects are naturally implemented by a stack (sometimes by multiple stacks). This is a classical way to implement local variables of a re-entrant or recursive subprogram. Stacks are also used to describe a formal computational framework. See stack machine. Many algorithms in pattern matching, compiler construction (e.g. recursive descent parsers), and machine learning (e.g. based on tree traversal) have a natural representation in terms of stacks.
Create a stack supporting the basic operations: push, pop, empty.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Stack step by step in the Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE Stacks;
IMPORT
Object,
Object:Boxed,
Out := NPCT:Console;
TYPE
Pool(E: Object.Object) = POINTER TO ARRAY OF E;
Stack*(E: Object.Object) = POINTER TO StackDesc(E);
StackDesc*(E: Object.Object) = RECORD
pool: Pool(E);
cap-,top: LONGINT;
END;
PROCEDURE (s: Stack(E)) INIT*(cap: LONGINT);
BEGIN
NEW(s.pool,cap);s.cap := cap;s.top := -1
END INIT;
PROCEDURE (s: Stack(E)) Top*(): E;
BEGIN
RETURN s.pool[s.top]
END Top;
PROCEDURE (s: Stack(E)) Push*(e: E);
BEGIN
INC(s.top);
ASSERT(s.top < s.cap);
s.pool[s.top] := e;
END Push;
PROCEDURE (s: Stack(E)) Pop*(): E;
VAR
resp: E;
BEGIN
ASSERT(s.top >= 0);
resp := s.pool[s.top];DEC(s.top);
RETURN resp
END Pop;
PROCEDURE (s: Stack(E)) IsEmpty(): BOOLEAN;
BEGIN
RETURN s.top < 0
END IsEmpty;
PROCEDURE (s: Stack(E)) Size*(): LONGINT;
BEGIN
RETURN s.top + 1
END Size;
PROCEDURE Test;
VAR
s: Stack(Boxed.LongInt);
BEGIN
s := NEW(Stack(Boxed.LongInt),100);
s.Push(NEW(Boxed.LongInt,10));
s.Push(NEW(Boxed.LongInt,100));
Out.String("size: ");Out.Int(s.Size(),0);Out.Ln;
Out.String("pop: ");Out.Object(s.Pop());Out.Ln;
Out.String("top: ");Out.Object(s.Top());Out.Ln;
Out.String("size: ");Out.Int(s.Size(),0);Out.Ln
END Test;
BEGIN
Test
END Stacks.
MODULE Stacks; (** AUTHOR ""; PURPOSE ""; *)
IMPORT
Out := KernelLog;
TYPE
Object = OBJECT
END Object;
Stack* = OBJECT
VAR
top-,capacity-: LONGINT;
pool: POINTER TO ARRAY OF Object;
PROCEDURE & InitStack*(capacity: LONGINT);
BEGIN
SELF.capacity := capacity;
SELF.top := -1;
NEW(SELF.pool,capacity)
END InitStack;
PROCEDURE Push*(a:Object);
BEGIN
INC(SELF.top);
ASSERT(SELF.top < SELF.capacity,100);
SELF.pool[SELF.top] := a
END Push;
PROCEDURE Pop*(): Object;
VAR
r: Object;
BEGIN
ASSERT(SELF.top >= 0);
r := SELF.pool[SELF.top];
DEC(SELF.top);RETURN r
END Pop;
PROCEDURE Top*(): Object;
BEGIN
ASSERT(SELF.top >= 0);
RETURN SELF.pool[SELF.top]
END Top;
PROCEDURE IsEmpty*(): BOOLEAN;
BEGIN
RETURN SELF.top < 0
END IsEmpty;
END Stack;
BoxedInt = OBJECT
(Object)
VAR
val-: LONGINT;
PROCEDURE & InitBoxedInt*(CONST val: LONGINT);
BEGIN
SELF.val := val
END InitBoxedInt;
END BoxedInt;
PROCEDURE Test*;
VAR
s: Stack;
bi: BoxedInt;
obj: Object;
BEGIN
NEW(s,10); (* A new stack of ten objects *)
NEW(bi,100);s.Push(bi);
NEW(bi,102);s.Push(bi);
NEW(bi,104);s.Push(bi);
Out.Ln;
Out.String("Capacity:> ");Out.Int(s.capacity,0);Out.Ln;
Out.String("Size:> ");Out.Int(s.top + 1,0);Out.Ln;
obj := s.Pop(); obj := s.Pop();
WITH obj: BoxedInt DO
Out.String("obj:> ");Out.Int(obj.val,0);Out.Ln
ELSE
Out.String("Unknown object...");Out.Ln;
END (* with *)
END Test;
END Stacks.
You may also check:How to resolve the algorithm Execute Brain step by step in the Jsish programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Scilab programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Befunge programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the AutoHotkey programming language