Iterator Solutions
nAbstract traversal interface
narrayIterator.next()
nArrayIterator iterator(array);   // C++
nfor (iterator.first();!iterator.atEnd();iterator.next())
n    sum+=iterator.current();
nlinkedListIterator.next()
nLinkedListIterator iterator(linkedList);   // C++
nfor (iterator.first();!iterator.atEnd();iterator.next())
n    sum+=iterator.current();
ntreeIterator.next()
nqueueIterator.next()
nor just iterator.next()
Let’s look at how some of the problematic examples of iteration might look if we used an iterator instead.  Here we have different iterators to traverse different kinds of structures: array, linked list, tree, and queue.  Since they all use the same method, next(), we can define an interface or abstract class and use that for traversal.

In C++ we can override operator++ and use it instead of next() so that we stay compatible with the old procedural code.