// ********************************************************* // Implementation file StackA.cpp for the ADT stack. // Array-based implementation. // ********************************************************* #include "StackA.h" // Stack class specification file Stack::Stack(): top(-1) { } // end default constructor Stack::~Stack() { popAll(); } bool Stack::isEmpty() const { return top < 0; } // end isEmpty void Stack::push(Object* newItem) throw(StackException) { // if stack has no more room for another item if (top >= MAX_STACK-1) throw StackException("StackException: stack full on push"); else { ++top; items[top] = newItem; } // end if } // end push Object* Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on pop"); else { Object* stackTop=items[top]; --top; // stack is not empty; pop top return stackTop; } } // end pop void Stack::popAll() { while (!isEmpty()) { // stack is not empty; deallocate object at top delete items[top]; --top; // pop top } // end if } // end popAll const Object* Stack::peek() const throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty on getTop"); else // stack is not empty; retrieve top return items[top]; } // end peek // End of implementation file.