#include "stack.h" // note -- don't need destructor, copy constructor // -- subject to typos //------------------------------ constructor --------------------------------- Stack::Stack() { top = -1; } //------------------------------ clear --------------------------------------- // deallocate memory (or just set top to -1 if there's no dynamic memory) void Stack::clear() { for (int i = 0; i <= top; i++) { delete array[i]; array[i] = NULL; } top = -1; } //------------------------------ isEmpty ------------------------------------- // check to see if stack is empty bool Stack::isEmpty() const { return (top == -1); } //------------------------------ isFull -------------------------------------- // check to see if stack is full bool Stack::isFull() const { return (top == MAX-1); } //--------------------------------- push ------------------------------------- // push (insert) item onto top of stack bool Stack::push(NodeData* dataptr) { if (!isFull()) { array[++top] = dataptr; return true; } return false; } //---------------------------------- pop ------------------------------------- // pop (remove) item off top of stack bool Stack::pop(NodeData*& dataptr) { if (isEmpty()) return false; else { dataptr = array[top--]; return true; } } //---------------------------------- peek ------------------------------------ // return item at top of stack bool Stack::peek(NodeData*& dataptr) const { if (isEmpty()) return false; else { dataptr = array[top]; return true; } } //---------------------------------- << ------------------------------------ // Overloaded output operator for class Stack ostream& operator<<(ostream& output, const Stack& theStack) { if (theStack.isEmpty()) output << "There is nothing in the requested stack." << endl; else { for (int i = theStack.top; i >= 0; i--) output << *theStack.array[i] // or theStack.array[i]->printNode(); << endl; output << endl; } return output; }