// From the software distribution accompanying the textbook // "A Practical Introduction to Data Structures and Algorithm Analysis, // Third Edition (C++)" by Clifford A. Shaffer. // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer. // This is the file to include in your code if you want access to the // complete LStack template class // Include the link class #include "link.h" // First, get the declaration for the base stack class #include "stack.h" // This is the declaration for LStack. // Linked stack implementation template class LStack: public Stack { private: Link* top; // Pointer to first element int size; // Number of elements public: LStack(int sz =defaultSize) // Constructor { top = NULL; size = 0; } ~LStack() { clear(); } // Destructor void clear() { // Reinitialize while (top != NULL) { // Delete link nodes Link* temp = top; top = top->next; delete temp; } size = 0; } void push(const E& it) { // Put "it" on stack top = new Link(it, top); size++; } E pop() { // Remove "it" from stack Assert(top != NULL, "Stack is empty"); E it = top->element; Link* ltemp = top->next; delete top; top = ltemp; size--; return it; } const E& topValue() const { // Return top value Assert(top != 0, "Stack is empty"); return top->element; } int length() const { return size; } // Return length };