(***************************************************************) (* STACK ADT *) (* (Array-based Implementation) *) (***************************************************************) CONST MaxStack = 26; TYPE StackElementType = Char; StackType = RECORD Elements : ARRAY [1 .. MaxStack] OF StackElementType; Top : 0 .. MaxStack END; (* StackType *) (***************************************************************) PROCEDURE CreateStack (VAR Stack : StackType); (* Initializes Stack to empty state. *) BEGIN (* CreateStack *) Stack.Top := 0 END; (* CreateStack *) (****************************************************************) PROCEDURE DestroyStack (VAR Stack : StackType); (* Destroys all elements in stack, leaving Stack empty. *) BEGIN (* DestroyStack *) Stack.Top := 0 END; (* DestroyStack *) (****************************************************************) FUNCTION EmptyStack (Stack : StackType) : Boolean; (* Returns True if Stack is empty; returns False otherwise. *) BEGIN (* EmptyStack. *) EmptyStack := Stack.Top = 0 END; (* EmptyStack *) (****************************************************************) FUNCTION FullStack (Stack : StackType) : Boolean; (* Returns True if Stack is full; returns False otherwise. *) BEGIN (* FullStack *) FullStack := Stack.Top = MaxStack END; (* FullStack *) (****************************************************************) PROCEDURE Push (VAR Stack : StackType; NewElement : StackElementType); (* Adds NewElement to the top of Stack. Assumes that the *) (* stack is not full. *) BEGIN (* Push *) Stack.Top := Stack.Top + 1; Stack.Elements[Stack.Top] := NewElement END; (* Push *) (****************************************************************) PROCEDURE Pop (VAR Stack : StackType; VAR PoppedElement : StackElementType); (* Removes the top element from Stack and returns its *) (* value in PoppedElement. Assumes that the stack is *) (* not empty. *) BEGIN (* Pop *) PoppedElement := Stack.Elements[Stack.Top]; Stack.Top := Stack.Top - 1 END; (* Pop *) (****************************************************************)