#pragma once #include #include using namespace std; class stack { public: string * items; //the array int numItems; //number items in stack int capacity; //size of current items array //Default constructor stack() { capacity = 5; items = new string[5]; numItems = 0; } //parameterized constructor stack(int cap) { capacity = cap; items = new string[cap]; numItems = 0; } //A helper method to resize the given //array to a larger given value. //Run time: O(n) void resize(int newCap) { //Step 0: For fun, announce what you are resizing too: cout << "Resizing array to " << newCap << endl; //Step 1: Make a new array of size newCap string* bigArray = new string[newCap]; //Step 2: Copy items over from old array to new array for (int i = 0; i < capacity; i++) { bigArray[i] = items[i]; } //Step 2.5: clean up (delete) old array to prevent memory leak delete [] items; //Step 3: point items variable to the new, bigger array items = bigArray; //Step 4: Update capacity variable capacity = newCap; } //Add item x to top of stack //Run time: Depends... if it has to resize, O(n) // if not, O(1) // void push(string x) { if (numItems == capacity) { //out of room! resize(2*capacity); } items[numItems] = x; numItems++; } //Remove and return top item from stack string pop() { numItems--; return items[numItems]; } //Determine if stack is empty bool empty() { if (numItems == 0) return true; else return false; } };