/* Lawrenceville Press lvpstack type IMPLEMENTATION */ /* This file modified to operate with CodeWarrior 3.3 */ /* June 1997, updated March 1999 comments updated */ /* */ /* The lvpstack class is based on the class apstack defined */ /* for use in the AP Computer Science courses. */ /* */ /* Inclusion of the C++ classes defined for use in the */ /* Advanced Placement Computer Science courses does not */ /* constitute endorsement of the other material in */ /* "A Guide to Programming in C++" (Lawrenceville Press, */ /* June 1997) by the College Board, Educational Testing */ /* Service, or the AP Computer Science Development Committee. */ // ******************************************************************* // Last Revised: 8/14/98 abort changed to exit // // APCS stack class IMPLEMENTATION // // stack implemented using the APCS vector class // ******************************************************************* //#include "lvpstack.h" #include const int SDEFAULT_SIZE = 10; // default initial stack size template lvpstack::lvpstack( ) : myTop(-1), myElements(SDEFAULT_SIZE) // postcondition: the stack is empty { } template lvpstack::lvpstack(const lvpstack & s) : myTop(s.myTop), myElements(s.myElements) // postcondition: stack is a copy of s { } template lvpstack::~lvpstack() // postcondition: stack is destroyed { // vector destructor frees memory } template const lvpstack & lvpstack::operator = (const lvpstack & rhs) // postcondition: normal assignment via copying has been performed { if (this != &rhs) { myTop = rhs.myTop; myElements = rhs.myElements; } return *this; } template bool lvpstack::isEmpty() const // postcondition: returns true if stack is empty, false otherwise { return myTop == -1; } template int lvpstack::length() const // postcondition: returns # of elements currently in stack { return myTop+1; } template void lvpstack::push(const itemType & item) // precondition: stack is [e1, e2...en] with n >= 0 // postcondition: stack is [e1, e2, ... en, item] { if( myTop + 1 >= myElements.length() ) // grow vector if necessary { myElements.resize(myElements.length() * 2); } myTop++; // new top most element myElements[myTop ] = item; } template void lvpstack::pop() // precondition: stack is [e1,e2,...en] with n >= 1 // postcondition: stack is [e1,e2,...e(n-1)] { if (isEmpty()) { cerr << "error, popping an empty stack" << endl; exit(1); } myTop--; } template void lvpstack::pop(itemType & item) // precondition: stack is [e1,e2,...en] with n >= 1 // postcondition: stack is [e1,e2,...e(n-1)] and item == en { if (isEmpty()) { cerr << "error, popping an empty stack" << endl; exit(1); } item = myElements[myTop]; myTop--; } template const itemType & lvpstack::top() const // precondition: stack is [e1, e2, ... en] with n >= 1 // postcondition: returns en { if (isEmpty()) { cerr << "error, popping an empty stack" << endl; exit(1); } return myElements[myTop]; } template void lvpstack::makeEmpty() // postcondition: stack is empty { myTop = -1; }