//////////////////////////////////////////////////////// // // intcircle.h // // Started 1/26/98 by Jeff Ondich // Last modified: 1/26/98 // // This is the interface for the class IntCircle, // which stores integers in a doubly-linked // circular linked list. // // The particular member functions described below // are sort of scattered. There are lots of other // functions you'd want to implement if you wanted // to make this class generally useful (see the List // class on page 62 of Weiss for some ideas). // //////////////////////////////////////////////////////// #ifndef INTCIRCLE_H #define INTCIRCLE_H struct Node { int mData; Node *mNext; Node *mPrevious; }; class IntCircle { private: Node *mFirstNode; void DeleteCircle(); public: // Constructors & destructor IntCircle(); virtual ~IntCircle(); // Manipulating the circle. void InsertItem( int theItem ); void RemoveFirstItem(); const IntCircle& operator++(); const IntCircle& operator--(); const IntCircle& operator=( IntCircle& otherCircle ); // Getting information about data members bool IsEmpty() const { return( mFirstNode == 0 ); } int GetFirstItem() const { if( mFirstNode == 0 ) return(0); else return( mFirstNode->mData ); } int Size() const; int Sum() const; // Printing void Print() const; }; //////////////////////////////////////////////////////// // // Descriptions of the member functions // //////////////////////////////////////////////////////// // An empty circle has mFirstNode == 0. // If the circle is empty when DeleteCircle, // RemoveItem, operator++, or // operator-- is called, then the function // in question does nothing. // DeleteCircle disposes of all the nodes in the // list and sets mFirstNode to 0. DeleteCircle is // distinct from ~IntCircle so that operator= can // also dispose of the nodes before making a copy // of another node. // The constructor IntCircle initializes the circle // to empty. // The destructor ~IntCircle disposes of all the // nodes in the circle by calling DeleteCircle. // InsertItem inserts a new node containing the // data "theItem" between the first node and the // last node. The new node becomes the new first // node. // RemoveItem removes the first node of the circle. // The old second node becomes the new first node. // operator++ makes the second node the new first node. // You might think of this as moving the pointer // mFirstNode clockwise around the circle. // operator-- makes the last node the new first node. // If ++ moves mFirstNode clockwise, then -- moves it // counter-clockwise. // operator= makes a copy of the circle "otherCircle" // and sets mFirstNode to point to the first item // in the copy. Don't forget to dispose of whatever // nodes mFirstNode pointed to before operator= got called. // IsEmpty returns true if the circle is empty, false otherwise. // GetFirstItem returns the data in the first node. // Returns 0 if the circle is empty. To make sure // you can tell the difference between an empty circle // and a circle whose first node contains a zero, you // should always call IsEmpty before calling GetFirstItem. // Size returns the number of nodes in the circle. // Sum returns the sum of the integers stored in the // nodes of the circle. // Print prints out the contents of the circle in // some reasonably readable way. #endif