#include "stdstuff.h" #include "IAStack.h" static const int initialCapacity = 10, capacityIncrement = 5; IntArrayStack::IntArrayStack () { data = new int [initialCapacity]; capacity = initialCapacity; elastic = true; count = 0; } IntArrayStack::IntArrayStack (int capacity) { if (capacity <= 0) { throw invalid_argument ("IntArrayStack::IntArrayStack - invalid capacity"); } data = new int [capacity]; this -> capacity = capacity; elastic = false; count = 0; } IntArrayStack::IntArrayStack (const IntArrayStack &otherStack) { data = new int [otherStack.capacity]; capacity = otherStack.capacity; elastic = otherStack.elastic; count = otherStack.count; for (int i = 0; i < count; i++) { data[i] = otherStack.data[i]; } } IntArrayStack::~IntArrayStack () { delete [] data; } void IntArrayStack::push (int value) { int *temp, i; if (elastic && (count == capacity)) { // time to increase the capacity capacity += capacityIncrement; temp = new int [capacity]; for (i = 0; i < count; i++) temp[i] = data[i]; delete [] data; data = temp; } else if (count == capacity) { throw overflow_error ("IntArrayStack::push - stack overflow"); } data[count++] = value; } int IntArrayStack::pop () { if (count == 0) { throw overflow_error ("IntArrayStack::pop - stack underflow"); } return data[--count]; } IntArrayStack& IntArrayStack::operator= (const IntArrayStack &otherStack) { if (capacity < otherStack.count) { if (!elastic) { throw overflow_error ("IntArrayStack - copy overflow"); } delete [] data; data = new int[otherStack.count]; capacity = otherStack.count; } for (int i = 0; i < otherStack.count; i++) { data[i] = otherStack.data[i]; } count = otherStack.count; return *this; }