#include "stdstuff.h" #include "ILStack.h" IntListStack::IntListStack () { head = NULL; elastic = true; count = 0; } IntListStack::IntListStack (int capacity) { if (capacity <= 0) { throw invalid_argument ("IntListStack::IntListStack - invalid capacity"); } head = NULL; elastic = false; this -> capacity = capacity; count = 0; } IntListStack::IntListStack (const IntListStack &otherStack) { // we must create a copy of the linked list in the other stack SNode *c, *l, *n; // so far the list we're creating does not have a last node l = head = NULL; // for all nodes in the other stack's list for (c = otherStack.head; c != NULL; c = c -> next) { // create a copy of the node n = new SNode(c -> data, NULL); // make the last node in the list being created point to it if (l == NULL) { // no last node yet head = n; // new node goes at the start of the list } else { l -> next = n; } // our new node is now the last node l = n; } count = otherStack.count; elastic = otherStack.elastic; capacity = otherStack.capacity; } IntListStack::~IntListStack () { // we must roll up the entire linked list SNode *c; while (head != NULL) { c = head; head = head -> next; delete c; } } void IntListStack::push (int value) { if (!elastic && (count == capacity)) { throw overflow_error ("IntListStack::push - stack overflow"); } head = new SNode(value, head); count++; } int IntListStack::pop () { SNode *c; int result; if (head == NULL) { throw overflow_error ("IntListStack::pop - stack underflow"); } c = head; head = head -> next; count--; result = c -> data; delete c; return result; } // *** NOT IMPLEMENTED *** (left as an exercise for the student!) // IntListStack& IntListStack::operator= (const IntListStack &otherStack) {}