{ Chapter 8, pp 379 - 381 } unit stackadt; { ******************************************************** BASIC STACK OPERATIONS - Array-Based Implementation CreateStack, StackIsEmpty, Push, Pop, and GetStackTop ******************************************************** } interface const MaxStack = 52; {;} type itemType = integer; {; } stackType = record Top : 0..MaxStack; Items : array[1..MaxStack] of itemType end; { record } procedure CreateStack(var S : stackType); { -------------------------------------------------------- Creates an empty stack. Precondition: None. Postcondition: The stack S is empty. -------------------------------------------------------- } function StackIsEmpty(S : stackType) : boolean; { -------------------------------------------------------- Determines whether a stack is empty. Precondition: CreateStack(S) has been called. Postcondition: Returns true if S was empty, else returns false. -------------------------------------------------------- } procedure Push(var S : stackType; NewItem : itemType; var Success : boolean); { -------------------------------------------------------- Adds an item to the top of a stack. Precondition: CreateStack(S) has been called. Postcondition: If S was not full, NewItem is on the top of the stack S and Success is true; otherwise Success is false. Note: S is full if it has MaxStack items. -------------------------------------------------------- } procedure Pop(var S : stackType; var Success : boolean); { -------------------------------------------------------- Removes the top of a stack. Precondition: CreateStack(S) has been called. Postcondition: If S wass not empty, the item that was added most recently to S is removed and Success is true. However, if S was empty, deletion is impossible and Success is false. -------------------------------------------------------- } procedure GetStackTop(S : stackType; var StackTop: itemType; var Success : boolean); { -------------------------------------------------------- Retrieves the top of a stack. Precondition: CreateStack(S) has been called. Postcondition: If S was not empty, StackTop contains the item that was added most recently to S and Success is true. However, if S was empty, the operation fails and Success is false. S is unchanged. -------------------------------------------------------- } implementation procedure CreateStack(var S : stackType); begin S.Top := 0; end; { CreateStack } function StackIsEmpty(S : stackType) : boolean; begin StackIsEmpty := (S.Top < 1) end; { StackIsEmpty } procedure Push(var S : stackType; NewItem : itemType; var Success : boolean); begin Success := (S.Top < MaxStack); if Success then { stack is not full } begin S.Top := succ(S.Top); S.Items[S.Top] := NewItem; end end; { Push } procedure Pop(var S : stackType; var Success : boolean); begin Success := not StackIsEmpty(S); if Success then S.Top := pred(S.Top) end; { Pop } procedure GetStackTop(S : stackType; var StackTop: itemType; var Success : boolean); begin Success := not StackIsEmpty(S); if Success then StackTop := S.Items[S.Top] end; { GetStackTop } end. { unit }