All pastes #375448 Raw Edit

Stack Unit

public text v1 · immutable
#375448 ·published 2007-02-28 10:01 UTC
rendered paste body
(***************************************************************)
(*                        STACK ADT                            *)
(*                (Array-based Implementation)                 *)
(***************************************************************)

UNIT STACK1;

INTERFACE

CONST
  MaxStack = 26;

TYPE
  StackElementType = Char;

  StackType  = RECORD
    Elements : ARRAY [1 .. MaxStack] OF StackElementType;
    Top      : 0 .. MaxStack
  END; (* StackType *)

(***************************************************************)

PROCEDURE CreateStack
  (VAR Stack : StackType);
PROCEDURE DestroyStack
  (VAR Stack : StackType);
FUNCTION EmptyStack
  (Stack : StackType) : Boolean;
FUNCTION FullStack
  (Stack : StackType) : Boolean;
PROCEDURE Push
  (VAR Stack  : StackType;
   NewElement : StackElementType);
PROCEDURE Pop
  (VAR Stack         : StackType;
   VAR PoppedElement : StackElementType);

(***************************************************************)
IMPLEMENTATION

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 *)

END.
(****************************************************************)