Advances The Iterator To The Next Node example essay topic

668 words
#include #include #include using name space std; class List; class Iterator; class Node{public: / Constructs a node with a given data value. @param's the data to store in this node / Node (string s); private: string data; Node previous; Node next; friend class List; friend class Iterator; }; class List{public: / Constructs an empty list; /List ; / Appends an element to the list. @param's the value to append / void push back (string s); / Inserts an element into the list. @param it er the position before which to insert@param's the value to append / void insert (Iterator it er, string s); / Removes an element from the list. @param i the position to remove@return an iterator pointing to the element after the erased element / Iterator erase (Iterator i); / Gets the beginning position of the list. @return an iterator pointing to the beginning of the list / Iterator begin ; / Gets the past-the-end position of the list. @return an iterator pointing past the end of the list / Iterator end ; void reverse ; private: Node first; Node last; }; class Iterator{public: / Constructs an iterator that does not point into any list. /Iterator ; / Looks up the value at a position. @return the value of the node to which the iterator points / string get cost; / Advances the iterator to the next node. /void next ; / Moves the iterator to the previous node. /void previous ; / Compares two iterators@param b the iterator to compare with this iterator@return true if this iterator and b are equal / book equals (Iterator b) cost; private: Node position; Node last; friend class List; }; Node: : Node (string s) { data ='s; previous = NULL; next = NULL; }List: : List { first = NULL; last = NULL; }void List: : push back (string s) { Node new node = new Node (s); if (last = = NULL) / list is empty /{ first = new node; last = new node; }else{ new node- previous = last; last- next = new node; last = new node; }}void List: : insert (Iterator it er, string s) { if (it er. position = = NULL) { push back (s); return; }Node after = it er. position; Node before = after- previous; Node new node = new Node (s); new node- previous = before; new node- next = after; after- previous = new node; if (before = = NULL) / insert at beginning / first = new node; else before- next = new node; }Iterator List: : erase (Iterator i) { Iterator it er = i; assert (it er. position! = NULL); Node remove = it er. position; Node before = remove- previous; Node after = remove- next; if (remove = = first) first = after; else before- next = after; if (remove = = last) last = before; else after- previous = before; it er. position = after; delete remove; return it er; }void List: : reverse {}Iterator List: : begin { Iterator it er; it er. position = first; it er. last = last; return it er; }Iterator List: : end { Iterator it er; it er. position = NULL; it er. last = last; return it er; }Iterator: : Iterator { position = NULL; last = NULL; }string Iterator: : get cost{ assert (position! = NULL); return position- data; }void Iterator: : next { assert (position! = NULL); position = position- next; }void Iterator: : previous { if (position = = NULL) position = last; else position = position- previous; assert (position! = NULL); }book Iterator: : equals (Iterator b) cost{ return position = = b. position; }int main { List staff; staff. push back ('Cracker, Carl'); staff. push back ('Hacker, Harry'); staff. push back ('Lam, Larry'); staff. push back ('Sandman, Susan'); staff. reverse ; / print all values / for (Iterator pos = staff. begin ; ! pos. equals (staff. end ); pos. next ) c out.