#include #include using namespace std; class stack { private: //Let's declare some variables string * items; //size of current array int capacity; //number of items currently in the stack int numItems; public: stack() { items = new string[5]; capacity = 5; numItems = 0; } void resize(int newCapacity) { cout << "Now resizing to: " << newCapacity << endl; //Step 1: create a new (bigger) array string * otherArray = new string[newCapacity]; //Step 2: copy items over from old array to new array for (int i = 0; i < numItems; i++) { otherArray[i] = items[i]; } //Step 3: update the capacity capacity = newCapacity; //Step 3.5: free the old array memory delete[] items; //Step 4: Set array variable to point at the new (bigger) array items = otherArray; } void push(string x) { //First check if we are out of room if (numItems == capacity) { resize(2*capacity); } items[numItems] = x; numItems++; } string pop() { string outputItem; outputItem = items[numItems-1]; numItems--; return outputItem; } //Return true if stack is empty, false otherwise bool empty() { if (numItems == 0) return true; else return false; } //remove all items from stack void clear() { numItems = 0; } }; int main() { stack S; S.push("ace"); S.push("queen"); S.push("joker"); S.push("king"); S.push("five of clubs"); S.push("jack of hearts"); S.push("2 of hearst"); S.push("3 of hearts"); S.push("10 of spades"); S.push("4 of hearts"); S.push("5 of hearts"); S.push("5 of spades"); cout << S.pop() << endl; //jack of hearts cout << S.pop() << endl; //five of clubs S.push("7 of diamonds"); cout << S.pop() << endl; //7 of diamonds cout << S.pop() << endl; //king //empty should return true if stack is empty, false if not while( ! S.empty() ) cout << S.pop() << endl; return 0; }