/* Copyright (c) 1997 Oxford University Press. All Rights Reserved. This code is from "Data Structures via C++: Objects by Evolution", published by Oxford University Press. Permission is hearby granted to use this code for any educational, non-commercial purpose, provided this notice remains intact. A. Michael Berman, Rowan University, berman@rowan.edu http://www.rowan.edu/evolve */ // RCS $Id: cx8-2.cpp,v 1.2 1997/08/08 14:14:53 berman Exp $ // cx8-2.cpp // Code Example 8-2: Implementation file, stack implemented using an Array #include "stack.h" Stack::Stack() { topIndex = -1; } void Stack::push(StackElementType item) { ++topIndex; // ensure array bounds not exceeded // assert(topIndex < maxStackSize); stackArray[topIndex] = item; } StackElementType Stack::pop() { // ensure array bounds not exceeded // assert(topIndex >= 0); int returnIndex(topIndex); --topIndex; return stackArray[returnIndex]; } StackElementType Stack::top() { // ensure array bounds not exceeded // assert(topIndex >= 0); return stackArray[topIndex]; } bool Stack::isEmpty() { return bool(topIndex == -1); }