#include "Stack.h" #include // Array Version /* // Default constructor - creates Stack of size 10 Stack::Stack() { top = 0; arrSize = 10; arr = new int[arrSize]; } // Constructor(int) - creates Stack of size n Stack::Stack(int n) { top = 0; arrSize = n; arr = new int[arrSize]; } // Copy constructor Stack::Stack(const Stack & st) { copyStack(st); } // Destructor Stack::~Stack() { delete[] arr; } // Overloaded assignment operator 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 void Stack::push(int 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 int Stack::pop() { if(top == 0) throw std::out_of_range("stack empty"); int result = arr[top-1]; arr[top-1] = 0; top--; return result; } // Helper method for copy constructor and operator= //PRE: //POST: copies st to the calling object //PARAM: st = stack to be copied void Stack::copyStack(const Stack & st) { arrSize = st.arrSize; top = st.top; // Make a new array arr = new int[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] int 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 bool Stack::empty() const { return top == 0; } //PRE: //POST: prints the contents of the stack from top to bottom void Stack::print() const { for (int i = top - 1; i >= 0; i--) { std::cout << arr[i] << std::endl; } std::cout << "arr = " << arr << std::endl; } */ // Linked List Version------------------------ ///* // Default contructor Stack::Stack() { top = nullptr; } // Constructor(int) - creates Stack of size n Stack::Stack(int n) { top = nullptr; } // Copy constructor Stack::Stack(const Stack & st) { copyStack(st); } // Destructor Stack::~Stack() { deleteStack(); } // Overloaded assignment operator Stack& Stack::operator=(const Stack & st) { if (this != &st) { deleteStack(); 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 void Stack::push(int value) { Node* newNode = new Node(value, top); top = newNode; } //PRE: stack is not empty //POST: removes and returns value at the top of the stack int Stack::pop() { // TO DO throw exception if stack is empty int result = top->data; Node* temp = top; top = top->next; delete temp; return result; } // Helper method for copy constructor and operator= //PRE: //POST: copies st to the calling object //PARAM: st = stack to be copied void Stack::copyStack(const Stack & st) { top = nullptr; Node* original = st.top; // Copy top of st if (original != nullptr) { // Copy top of st top = new Node(original->data); original = original->next; Node* copy = top; // Copy rest of stack while (original != nullptr) { Node* newNode = new Node(original->data); copy->next = newNode; copy = copy->next; original = original->next; } } } // Helper method for destructor and operator= //PRE: //POST: deallocates memory associated with stack void Stack::deleteStack() { Node* temp = top; while (top != nullptr) { top = top->next; delete temp; temp = top; } top = nullptr; } // Accessors //Returns value at the top of the stack //PRE: stack is not empty //POST: returns arr[top-1] int Stack::peek() const { // TO DO throw exception if stack is empty return top->data; } // Returns the number of items stored in the stack //PRE: //POST: returns top bool Stack::empty() const { return top == nullptr; } //PRE: //POST: prints the contents of the stack from top to bottom void Stack::print() const { Node* temp = top; while (temp != nullptr) { std::cout << temp->data << std::endl; temp = temp->next; } } //*/