Jump to page content Jump to navigation

College Board

AP Central

AP Exam Reader
Siemens Awards for Advanced Placement

APAC 2010
Print Page
Home > The Courses > Course Home Pages > C++ Classes: apstack.cpp

C++ Classes: apstack.cpp

    // *******************************************************************  //  Last Revised: 8/18/98  //                abort() changed to exit(1)  //                comments updated  //  //  September 1, 1997 -- APCS stack class  IMPLEMENTATION  //  //  stack implemented using the APCS vector class  // *******************************************************************    #include "apstack.h"  #include     const int SDEFAULT_SIZE = 10;        // default initial stack size    template   apstack::apstack( )      : myTop(-1),        myElements(SDEFAULT_SIZE)         // postcondition: the stack is empty       {        }    template   apstack::apstack(const apstack & s)     : myTop(s.myTop),            myElements(s.myElements)    // postcondition: stack is a copy of s       {        }         template   apstack::~apstack()  // postcondition: stack is destroyed  {      // vector destructor frees memory  }    template   const apstack &  apstack::operator = (const apstack & rhs)  // postcondition: normal assignment via copying has been performed  {      if (this != &rhs)      {          myTop = rhs.myTop;          myElements = rhs.myElements;      }      return *this;  }    template   bool  apstack::isEmpty() const  // postcondition: returns true if stack is empty, false otherwise  {      return myTop == -1;  }    template   int  apstack::length() const  // postcondition: returns # of elements currently in stack  {      return myTop+1;  }    template   void  apstack::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  apstack::pop()  // precondition: stack is [e1,e2,...en] with n >= 1  // postcondition: stack is [e1,e2,...e(n-1)]  {      if (isEmpty())      {          cerr << endl; itemtype an mytop--; empty << }   void  apstack::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 << endl; itemtype an mytop--; empty << }   const itemType &  apstack::top() const  // precondition: stack is [e1, e2, ... en] with n >= 1  // postcondition: returns en  {      if (isEmpty())      {          cerr << endl; itemtype an empty << }   void  apstack::makeEmpty()  // postcondition:  stack is empty       {      myTop = -1;      }     
  ABOUT MY AP CENTRAL
    Course and Email Newsletter Preferences
  AP COURSES AND EXAMS
    Course Home Pages
    Course Descriptions
    The Course Audit
    Sample Syllabi
    Teachers' Resources
    Exam Calendar and Fees
    Exam Questions
    FAQs
  PRE-AP
    Teachers' Corner
    Workshops
  AP COMMUNITY
    About Electronic Discussion Groups
    Become an AP Exam Reader

Back to top