#include "Stack.h" Stack::Stack() { myCount = 0; } Stack::Node::Node(const Item& item, Node* next) { myItem = item; myNext = next; } Stack::Node::Node(const Item& item) { myItem = item; myNext = NULL; } Stack::~Stack() { // Node* ptr = NULL; // while (true) { // ptr = myTop; // if (ptr == NULL) break; // myTop = myTop->myNext; // delete ptr; // // } delete myTop; myTop = NULL; } //Stack::Node::~Node() { //// delete myNext; //// myNext = NULL; //} void Stack::push(const Item& newItem) { myTop = new Node(newItem, myTop); myCount++; } Item Stack::getTop() const { if ( isEmpty() ) { throw std::underflow_error("Stack::getTop(): stack is empty"); } return myTop->myItem; } Item Stack::pop() { if ( isEmpty() ) { throw std::underflow_error("Stack::pop(): stack is empty"); } Item result = myTop->myItem; Node* nPtr = myTop; myTop = myTop->myNext; nPtr->myNext = NULL; delete nPtr; myCount--; return result; }