//---------------------------------------------------------------- // // stack-with-ctors.cpp // // Implements a simple character stack, with constructors and // destructor. Based on the example from Pohl, C++ by Dissection, // pages 218-223. // // written by: Simon Parsons // modified : 14th March 2009 // // #include #include using namespace std; //---------------------------------------------------------------- // // charStack // // Unlike the basic stack, this version dynamically allocates memory // to allow different stack sizes. class charStack { private: enum{EMPTY}; char *stack; int top, FULL, length; public: explicit charStack(int size); // constructor charStack( const charStack& stk ); // copy constructor ~charStack(); // destructor void reset(); void push(char); char pop(); char topOfStack() const; bool isEmpty() const; bool isFull() const; }; // constructor // // Set the values of FULL and top, and allocate enough storage for the // stack. charStack::charStack(int size) : top(EMPTY), FULL(size - 1), length(size) { stack = new char[size]; } // copy constructor // // Says how to set up the values for the copy from the values from the // current item, which is called stk here. charStack::charStack( const charStack& stk ) : top(stk.top), FULL(stk.FULL), length(stk.length) { stack = new char[stk.length]; memcpy(stack, stk.stack, length); } // destructor // // Delete the storage that was allocated for the stack. charStack::~charStack(){ delete[]stack; } // reset // // Reset the stack. We don't change what is in it, we just forget that // anything is in there. void charStack::reset() { top = EMPTY; } // push // // Push an item onto the stack. We actually write it into the next element // of the array, adjusting the value of top, which tells us which the top // element is. // // Note that ++top increments top before we write the new value into the // array. void charStack::push(char c) { stack[++top] = c; } // pop // // Remove an element from the top of the stack. We do this by first reading // out the top element and then reducing the value of top, combining these // operations by the use of top--. char charStack::pop() { return stack[top--]; } // topOfStack // // Return the value at the top of the stack without removing it from the // stack. char charStack::topOfStack() const { return stack[top]; } // isEmpty // // Return true if there is nothing in the stack, false otherwise. bool charStack::isEmpty() const { return (top == EMPTY); } // isFull // // Return true the stack is full, false otherwise. bool charStack::isFull() const { return (top == FULL); } //---------------------------------------------------------------- // // Glocally declared function // showDynamicStack // // Use the fact that we can dynamically allocate storage to create // a stack that is just big enough for the data it has to hold. void showDynamicStack(){ int i = 0; string str; cout << "Enter string: "; getline(cin, str); charStack s(str.size()+1); s.reset(); while(str[i] && ! s.isFull()) { s.push( str[i++] ); } cout << "reversed string= "; while (! s.isEmpty() ) { cout << s.pop(); } cout << endl; } //---------------------------------------------------------------- // // Main program // Creates a stack, pushing a string into it, character by character // and then popping them out, reversing the string. int main() { charStack s(200); string str = "hello world"; int i = 0; cout << "string = " << str << endl; s.reset(); while(str[i] && ! s.isFull()) { s.push( str[i++] ); } cout << "reversed string= "; while (! s.isEmpty() ) { cout << s.pop(); } cout << endl << endl; showDynamicStack(); cout << endl; }