600.226 Data Structures (Spring 2004 - Prof. Eisner)

Hint 3 for sorted iterators in Homework 4

Here is some classic pseudocode for finding the minimum-element position in a list of Integers:

min = null;
for each element e in the list  // hey, why not use an ordinary iterator for this?
  if (min==null || e < min)
     min = e;

// now min holds the minimum element, or null if the list was empty

You can do something like that for this problem, too. Your job is a little more complicated, for four reasons:

By the way - a comment in the pseudocode above suggests that when findNextPosition needs to scan through the list positions in left-to-right order, it can simply use one of the list iterators you've already written. (As long as you have given that iterator a public nextPosition method. If not, it is easy to turn your next method into a nextPosition method, and then introduce a new next method that simply calls nextPosition, as here.)

I actually recommend doing that; it will simplify your code and make your life easier. The whole point of writing iterators is that they make your life easier - even when writing other iterators!


Jason Eisner - jason@cs.jhu.edu - $Date: 2004/02/18 07:15:17 $