// A stack class is defined here with a private internal storage // and public functions that can manipulate it. // include this file in any program that needs a stack of strings // After page 139 of C++ for Pascal Programmers by Ira Pohl // Upgraded to 1997 style C++ in Feb 1998 // Updated to ISO C++ and modern style Wed May 12 07:13:30 PDT 2004 #include #include #include using namespace std; #define MAX_LEN 255 struct stack{ private: string s[MAX_LEN]; int top_at; static const int EMPTY= -1; static const int FULL = MAX_LEN -1; void make_new(void) { top_at=EMPTY; } public: void push(string c) { if(!full()){top_at++; s[top_at]=c; }} void pop(void) { if( !empty()) s[top_at--]; } string top(void) { return (s[top_at]); } bool empty(void) { return (top_at==EMPTY); } bool full(void) { return (top_at== FULL); } stack(void){ make_new();} ~stack(void){while(!empty())pop();} }; /***** simple tests int main() { stack s; s.push("a"); cout << s.top(); s.push("b"); cout << s.top(); s.push("c"); cout << s.top(); cout << endl; while(!s.empty()) { cout << s.top(); s.pop(); } return 0; } ****/