#include "StackLi.h" #include /** * 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 full. * Return false always, in this implementation. */ template bool Stack::isFull( ) const { return false; } /** * 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( ); } /** * Get the most recently inserted item in the stack. * Return the most recently inserted item in the stack * or throw an exception if empty. */ template const Object & Stack::top( ) const { if( isEmpty( ) ) throw Underflow( ); return topOfStack->element; } /** * Remove the most recently inserted item from the stack. * Exception Underflow if the stack is empty. */ template void Stack::pop( ) { if( isEmpty( ) ) throw Underflow( ); 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; }