84 lines
1.5 KiB
Plaintext
84 lines
1.5 KiB
Plaintext
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.
|