#include "StackLi.h" // Construct the stack. template Stack::Stack( ) { topOfStack = NULL; } // Copy constructor. template Stack::Stack( const Stack & rhs ) { topOfStack = NULL; *this = rhs; } // Destructor. template Stack::~Stack( ) { makeEmpty( ); } // Test if the stack is logically empty. // Return true if empty, false, otherwise. template bool Stack::isEmpty( ) const { return topOfStack == NULL; } // Make the stack logically empty. template void Stack::makeEmpty( ) { while( !isEmpty( ) ) pop( ); } // Return the most recently inserted item in the stack. // or throw an UnderflowException if empty. template const Object & Stack::top( ) const { if( isEmpty( ) ) throw UnderflowException( ); return topOfStack->element; } // Remove the most recently inserted item from the stack. // Throw Underflow if the stack is empty. template void Stack::pop( ) { if( isEmpty( ) ) throw UnderflowException( ); ListNode *oldTop = topOfStack; topOfStack = topOfStack->next; delete oldTop; } // Return and remove the most recently inserted item // from the stack. template Object Stack::topAndPop( ) { Object topItem = top( ); pop( ); return topItem; } // Insert x into the stack. template void Stack::push( const Object & x ) { topOfStack = new ListNode( x, topOfStack ); } // Deep copy. template const Stack & Stack::operator=( const Stack & rhs ) { if( this != &rhs ) { makeEmpty( ); if( rhs.isEmpty( ) ) return *this; ListNode *rptr = rhs.topOfStack; ListNode *ptr = new ListNode( rptr->element ); topOfStack = ptr; for( rptr = rptr->next; rptr != NULL; rptr = rptr->next ) ptr = ptr->next = new ListNode( rptr->element ); } return *this; }