package iterator; import position.Position; import position.PositionList; import java.util.Iterator; import java.util.NoSuchElementException; public class ElementIterator implements Iterator { protected PositionList list; // the underlying list protected Position cursor; // the next position public ElementIterator(PositionList L) { list = L; cursor = (list.isEmpty())? null : list.first(); } public boolean hasNext() { return (cursor != null); } public E next() throws NoSuchElementException { if (cursor == null) throw new NoSuchElementException("No next element"); E toReturn = cursor.element(); cursor = (cursor == list.last())? null : list.next(cursor); return toReturn; } public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException("remove"); } }