// -------------------------------------------------------------------------- // Linear Containers // A. Fischer June 12, 2001 file: linear.hpp // -------------------------------------------------------------------------- #pragma once #include #include "container.hpp" #include "cell.hpp" #include "item.hpp" using namespace std; template class Linear: public Container { protected: // ------------------------------------------------------------- Cell* head; // This is a dummy header for the list. private: // ------------------------------------------------------------- Cell* here; // Cursor for traversing the container. Cell* prior; // Trailing pointer for traversing the container. protected: // ------------------------------------------------------------- Linear(): head(new Cell), here( NULL ), prior( head ) {} virtual ~Linear (); void reset() { prior = head; here = head->next; } bool end() const { return here == NULL; } void operator ++(); virtual void insert( Cell* cp ); virtual void focus() = 0; Cell* remove(); void setPrior(Cell* cp){ prior = cp; here = prior->next; } operator T*() const { return *here; } // Cast Linear to T*. ------------ // Delegate to fetch cell contents const T* getData( Cell* cp ) const { return cp->data; } public: // -------------------------------------------------------------- void put(T * ep) { if (ep) insert( new Cell(ep) ); } T* pop(); T* peek() { focus(); return *here; } virtual ostream& print( ostream& out ); }; template inline ostream& operator<<(ostream& out, Linear& s) {return s.print(out); }