#include "StackAr1.h" /** * Construct the stack. */ template Stack::Stack( ) : theArray( 1 ) { topOfStack = -1; } /** * Test if the stack is logically empty. * Return true if empty, false, otherwise. */ template bool Stack::isEmpty( ) const { return topOfStack == -1; } /** * Make the stack logically empty. */ template void Stack::makeEmpty( ) { topOfStack = -1; } /** * Get the most recently inserted item in the stack. * Does not alter the stack. * Return the most recently inserted item in the stack. * Throw Underflow if stack is already empty. */ template const Object & Stack::top( ) const { if( isEmpty( ) ) throw UnderflowException( ); return theArray[ topOfStack ]; } /** * Remove the most recently inserted item from the stack. * Throw Underflow if stack is already empty. */ template void Stack::pop( ) { if( isEmpty( ) ) throw UnderflowException( ); topOfStack--; } /** * Insert x into the stack. */ template void Stack::push( const Object & x ) { if( topOfStack == theArray.size( ) - 1 ) theArray.resize( theArray.size( ) * 2 + 1 ); theArray[ ++topOfStack ] = x; } /** * Return and remove most recently inserted item from the stack. * Return most recently inserted item. * Throw Underflow if stack is already empty. */ template Object Stack::topAndPop( ) { if( isEmpty( ) ) throw UnderflowException( ); return theArray[ topOfStack-- ]; }