// Project StackCopy // File stack.cpp #include "stack.h" Stack::Stack(int n) { array = new int[n]; size = n; top = array - 1; } // Create a new stack by copying an // existing Stack object. A "deep copy" // is made of the existing stack. Stack::Stack(const Stack &other) { int *p, *q; size = other.size; array = new int[size]; for(p = other.array, q = array; p <= other.top; p++) *(q++) = *p; top = q - 1; } Stack::~Stack() { delete[] array; } void Stack::push(int value) { if (!is_full()) *(++top) = value; } void Stack::pop() { if (!is_empty()) top--; } int Stack::peek() { if (!is_empty()) return *top; else return 0; } bool Stack::is_full() { return top >= array + size - 1; } bool Stack::is_empty() { return top < array; }