/* Filename: ArrStack.cpp Programmer: Br. David Carlson Date: August 8, 1998 Modified: April 23, 1999 to use an array-based implementation. Modified: July 31, 2000 to use modern headers. This file implements the functions of the ArrStackClass found in ArrStack.h. */ #include "arrstack.h" /* Given: Nothing. Task: This is a constructor. It creates and initializes a new stack to empty. Return: Nothing other than the newly created object. */ ArrStackClass::ArrStackClass(void) { Top = -1; } /* Given: Nothing (other than the implicit ArrStackClass object). Task: To check whether this object is empty. Return: true if it is empty, false otherwise. */ bool ArrStackClass::Empty(void) const { if (Top < 0) return true; else return false; } /* Given: Item A data item. Task: To push item onto the stack (the implicit object). Return: Nothing directly, but the implicit object is modified. */ void ArrStackClass::Push(const ItemType & Item) { Top++; if (Top == StackMax) { cerr << "Error: no room to push an item onto the stack" << endl; exit(1); } else Info[Top] = Item; // assumes = works for ItemType } /* Given: Nothing. Task: To pop the top item from the stack (the implicit object). Return: Item The item popped from the stack. */ void ArrStackClass::Pop(ItemType & Item) { if (Top < 0) { cerr << "Error: cannot pop an item from an empty stack" << endl; exit(1); } else { Item = Info[Top]; // assumes that = works on this type Top--; } }