#include const int MAXSTACK = 100; const int TRUE = 1; const int FALSE = 0; // class declaration // preconditions: requires that MAXSTACK the maximum size of the stack, be defined // : requires that TRUE be defined as a nonzero integer // : requires that FALSE be defined as 0 class Stack { private: int top; // top-of-stack position int num[MAXSTACK]; public: Stack(void); // constructor void push(int); int pop(void); int isempty(void); int isfull(void); }; // implementation section Stack::Stack() // constructor { top = -1; // initialize the top-of-stack position } void Stack::push(int value) { top++; // increment the index stored in top num[top] = value; // store the value } int Stack::pop(void) { int topval; topval = num[top]; // retrieve the top element top--; // decrement the index stored in top return(topval); } int Stack::isempty(void) { if (top == -1) return(TRUE); else return(FALSE); } int Stack::isfull(void) { if (top == MAXSTACK - 1) return(TRUE); else return(FALSE); } int main() { Stack digits; // define a Stack named digits int newnum; cout << "Enter as many digits as you wish, one per line" << "\nTo stop entering digits, enter a number greater than 9\n"; while (1) { cout << "Enter a digit: "; cin >> newnum; if (newnum > 9) break; if (digits.isfull()) // check for overflow { cout <<"\nNo more storage allocation space" << "\nThe last digit has not been entered on the stack" << endl; break; } else digits.push(newnum); // push value onto the stack } // pop and display digits from the stack cout << "\nThe values popped from the stack are:\n"; while(!digits.isempty()) // check for underflow { cout << digits.pop() << endl; } return 0; }