#include "stack.h" Stack::Stack() //Default constructor, creates empty stack. { Top = -1; } void Stack::Push(StackElement X, int &Success) //Pushes X onto stack. //Pre : X is defined and Stack is initialized. //Post: Sets Success to True to indicate successful push; // and False to indicate failure. { if (Top >= MaxStack) Success = False; //no room on stack else { Top++; //increment stack top pointer Items[Top] = X; //copy X to stack Success = True; } } void Stack::Pop(StackElement &X, int &Success) //Pops top of a stack into X. //Pre : Stack has been initialized. //Post: X contains top stack element which has been removed // from the stack. Success is set to True if pop is // successful and False if not. { if (Top < 0) Success = False; //empty stack else { X = Items[Top]; //pop top of stack into X Top--; //decrement top of stack pointer Success = True; } } void Stack::Retrieve(StackElement &X, int &Success) //Copies value at top of stack to X. //Pre : Stack is defined. //Post: X contains top stack element and the stack is // unchanged. Success set to True for succesful // retrieve and False ortherwise. { if (Top < 0) Success=0; else { X = Items[Top]; //copy top stack element Success = True; } } int Stack::IsEmpty() //Returns True if stack is empty and False otherwise. { return(Top < 0); } int Stack::IsFull() //Returns True is stack is Full and False otherwise. { return(Top >= MaxStack); }