Presentation is loading. Please wait.

Presentation is loading. Please wait.

List Iterator Implementation

Similar presentations


Presentation on theme: "List Iterator Implementation"— Presentation transcript:

1 List Iterator Implementation
Lecture 33 Fri, Apr 13, 2007

2 List Traversals To traverse a list is to move from one end to the other, “visiting” each node along the way. Forward traversals go from head to tail Reverse traversals go from tail to head. The meaning of “visiting” a node is left unspecified at this point. The meaning will be specified at the time of the traversal. 5/3/2019 List Iterators

3 The Traversal Function
Introduce a new List member function The parameter, named visit, is a pointer to a function. The function visit() has prototype void traverse(ListIterator& it, void (* visit)(ListIterator&)); void visit(ListIterator&); 5/3/2019 List Iterators

4 Traversals and Iterators
A traversal may be implemented as follows. void traverse(ListIterator& it, void (* visit)(ListIterator&)) { for (it.begin(); !it.atEnd(); ++it) visit(it); return; } 5/3/2019 List Iterators

5 Example: Print the List
Define a visiting function print(). Use print() to print (visit) the members of the List. void print(ListIterator& it) { cout << *it << endl; return; } ListIterator it(list); list.traverse(it, print); 5/3/2019 List Iterators

6 The Visiting Function Suppose we made print() a member function.
The program would not compile because the prototype of print() would be What can be done about this? void print(List* this, ListIterator& it); 5/3/2019 List Iterators

7 Static Member Functions
A static member function does not have the implicit this parameter. It may not invoke any non-static member function. If we make print() a static member function then it will work. static void print(ListIterator& it); 5/3/2019 List Iterators

8 Implemention of Reverse Iterators
To construct a reverse iterator for a linked list, Push NULL onto the stack. Then push the addresses of the nodes onto a stack. To increment the reverse list iterator Pop an address off the stack. Assign it to the node pointer. 5/3/2019 List Iterators

9 Insertion Sort with Iterators
InsertionSortwIter.cpp 5/3/2019 List Iterators


Download ppt "List Iterator Implementation"

Similar presentations


Ads by Google