// ********************************************************* // Header file StackP.h for the ADT stack. // Pointer-based implementation. // ********************************************************* #include "StackException.h" #include "Object.h" class Stack { public: // constructors and destructor: Stack(); // default constructor Stack(const Stack& aStack); // copy constructor // Note: the copy constructor will create copies of all items on the stack // rather than just creating copies of the pointers to those items // (this way it is safe to create a copy of stack and destroy it // immediately without affecting the original stack). ~Stack(); // destructor // stack operations: bool isEmpty() const; // Determines whether a stack is empty. // Precondition: None. // Postcondition: Returns true if the stack is empty; // otherwise returns false. void push(Object* newItem); // Adds an item to the top of a stack. // Precondition: newItem is the item to be added. // Postcondition: If the insertion is successful, newItem // is on the top of the stack. Object* pop() throw(StackException); // Retrieves and removes the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, the item // that was added most recently is removed. However, if // the stack is empty, deletion is impossible. // Exception: Throws StackException if the stack is empty. void popAll(); // Removes all the items from the stack. // Precondition: None. // Postcondition: Stack is empty. Calls delete on all objects on the stack. const Object* peek() const throw(StackException); // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, stackTop // contains the item that was added most recently. // However, if the stack is empty, the operation fails // and stackTop is unchanged. The stack is unchanged. // Exception: Throws StackException if the stack is empty. private: class StackNode // a node on the stack { public: Object* item; // a data item on the stack StackNode *next; // pointer to next node StackNode(Object* o, StackNode* n=NULL) : item(o), next(n) {} }; // end struct StackNode *top; // pointer to first node in the stack }; // end Stack class // End of header file.