#ifndef LISTITERATOR_H #define LISTITERATOR_H //***************************************************************************** // // ListIterator.h // //***************************************************************************** // // Copyright (C) 1996 // // Ronald A. MacCracken // Graduate Student // Computer Science Department // University of California // Davis, CA 95616 // // Permission is granted to use at your own risk and // distribute this software in source and binary forms // provided the above copyright notice and this paragraph // are preserved on all copies. This software is provided // "as is" with no express or implied warranty. // //***************************************************************************** //***************************************************************************** // // Iterators are a special type of class in C++. They allow the programmer // to access elements of a particular data type without having to know // the implementation of that data type. In this case, the ListIterator // gives the programmer access to every element in the singly-linked // List from head to tail. If this was a doubly-linked List, it would // probably be nice to also have an iterator going from tail to head. // // The example below shows how easy it is to access every element of the // List. Also notice that no matter how the List is implemented, either // using pointers, a dynamically allocated array like DynArray, or others, // this code will always stay the same - so long as the appropriate changes // are made internally to the ListIterator class. // // Example Usage: // // List list ; // // ... // // for ( ListIterator i ( list ) ; ! i.Is_Done ( ) ; ++i ) // { // int element = i.Get_Current ( ) ; // // ... // } // //***************************************************************************** #include "List.h" #include "ListElement.h" template class ListIterator { private: ListElement* first_element ; ListElement* current_element ; private: void Copy ( const ListIterator& ) ; void Delete ( void ) ; public: // constructors ListIterator ( void ) ; // initialize the current element to be // x elements into the list l ListIterator ( const List& l , const int x = 0 ) ; // copy constructor ListIterator ( const ListIterator& l ) ; // destructor ~ListIterator ( void ) ; // assignment operator ListIterator& operator= ( const ListIterator& l ) ; // advances current element ListIterator& operator++ ( void ) ; ListIterator& operator++ ( int ) ; // access current element const TYPE& Get_Current ( void ) const ; // accesses next element const TYPE& Get_Next ( void ) const ; // tells whether or not iterator is finished int Is_Done ( void ) const ; // resets current element to first element void Reset ( void ) ; // resets current element as in constructor void Reset ( const List& l , const int x = 0 ) ; } ; #include "ListIterator.C" #endif