#include "StackAr.h" /** * Construct the stack. */ template Stack::Stack( int capacity ) : theArray( capacity ) { topOfStack = -1; } /** * Test if the stack is logically empty. * Return true if empty, false otherwise. */ template bool Stack::isEmpty( ) const { return topOfStack == -1; } /** * Test if the stack is logically full. * Return true if full, false otherwise. */ template bool Stack::isFull( ) const { return topOfStack == theArray.size( ) - 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. * Exception Underflow if stack is already empty. */ template const Object & Stack::top( ) const { if( isEmpty( ) ) throw Underflow( ); return theArray[ topOfStack ]; } /** * Remove the most recently inserted item from the stack. * Exception Underflow if stack is already empty. */ template void Stack::pop( ) { if( isEmpty( ) ) throw Underflow( ); topOfStack--; } /** * Insert x into the stack, if not already full. * Exception Overflow if stack is already full. */ template void Stack::push( const Object & x ) { if( isFull( ) ) throw Overflow( ); theArray[ ++topOfStack ] = x; } /** * Return and remove most recently inserted item from the stack. * Return most recently inserted item. * Exception Underflow if stack is already empty. */ template Object Stack::topAndPop( ) { if( isEmpty( ) ) throw Underflow( ); return theArray[ topOfStack-- ]; }