ain#pragma once #include template class Stack { public: // Constructor and destructor // Default constructor Stack(); // Constructor that sets size of stack Stack(int n); // Copy constructor Stack(const Stack & st); // Destructor ~Stack(); // Overloaded assignment operator Stack& operator=(const Stack & st); // Mutators //PRE: Stack is not full //PARAM: value = value to be inserted //POST: value is inserted at the top of the stack void push(T value); //PRE: Stack is not empty //POST: removes and returns value at the top of the stack T pop(); // Accessor //PRE: Stack is not empty //POST: returns value at the top of the stack T peek() const; //PRE: //POST: returns true if stack is empty, otherwise false bool empty() const; private: // Array version T* arr; int top; int arrSize; void copyStack(const Stack & st); }; // Array Version // Default constructor - creates Stack of size 10 template Stack::Stack() { top = 0; arrSize = 10; arr = new T[arrSize]; } // Constructor(int) - creates Stack of size n template Stack::Stack(int n) { top = 0; arrSize = n; arr = new T[arrSize]; } // Copy constructor template Stack::Stack(const Stack & st) { copyStack(st); } // Destructor template Stack::~Stack() { delete[] arr; } // Overloaded assignment operator template Stack& Stack::operator=(const Stack & st) { if(this != &st){ delete[] arr; copyStack(st); } return *this; } // Mutators //PRE: Stack is not full //PARAM: value = value to be inserted //POST: value is inserted at the top of the stack template void Stack::push(T value) { if(top == arrSize) throw std::length_error("stack full"); arr[top++] = value; } //PRE: stack is not empty //POST: removes and returns value at the top of the stack template T Stack::pop() { if(top == 0) throw std::out_of_range("stack empty"); return arr[--top]; } // Helper method for copy constructor and operator= //PRE: //POST: copies st to the calling object //PARAM: st = stack to be copied template void Stack::copyStack(const Stack & st) { arrSize = st.arrSize; top = st.top; // Make a new array arr = new T[arrSize]; // Copy content of parameter for(int i=0; i < top; i++){ arr[i] = st.arr[i]; } } // Accessors //Returns value at the top of the stack //PRE: stack is not empty //POST: returns arr[top-1] template T Stack::peek() const { if(top == 0) throw std::out_of_range("stack empty"); return arr[top-1]; } // Returns the number of items stored in the stack //PRE: //POST: returns top template bool Stack::empty() const { return top == 0; }