/* 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: cx7-3.cpp,v 1.1 1997/08/07 20:50:48 berman Exp $ // cx7-3.cpp // Code Example 7-3: implementation file, list implemented with an array #include "list.h" List::List() { // initialize to an empty list numberOfElements = 0; currentPosition = -1; } void List::insert(const ListElementType & elem) { //assert(numberOfElements < maxListSize); listArray[numberOfElements] = elem; numberOfElements++; } bool List::first(ListElementType & elem) { if (numberOfElements == 0) return false; else { currentPosition = 0; elem = listArray[currentPosition]; return true; } } bool List::next(ListElementType & elem) { // with proper use, currentPosition should always be // greater than or equal to zero // assert(currentPosition >= 0); if (currentPosition >= numberOfElements - 1) return false; else { currentPosition++; elem = listArray[currentPosition]; return true; } }