// A private iterator for circular lists.
// (c) 1998, 2001 duane a. bailey
package structure;
//+iteratorInterface
/**
* An iterator for traversing the elements of a circular list.
* The iterator traverses the list beginning at the head, and heads toward
* tail.
*
* Typical use:
*
*
* List l = new CircularList();
* // ...list gets built up...
* Iterator li = l.iterator();
* while (li.hasNext())
* {
* System.out.println(li.get());
* li.next();
* }
* li.reset();
* while (li.hasNext())
* { .... }
*
* @version $Id: CircularListIterator.java,v 1.2 2005/10/18 19:47:20 terescoj Exp $
* @author, 2001 duane a. bailey
*/
class CircularListIterator extends AbstractIterator
{
//-iteratorInterface
//+iteratorConstructor
//+iteratorPrivate
/**
* The tail of the traversed list.
*/
protected SinglyLinkedListElement tail;
/**
* The current value of the iterator.
*/
protected SinglyLinkedListElement current;
//-iteratorPrivate
//+iteratorInterface
/**
* Constructs an iterator over circular list whose tail is t
*
* @pre t is a reference to a circular list element
* @post constructs an iterator for traversing circular list
*
* @param t The tail of the list to be traversed.
*/
public CircularListIterator(SinglyLinkedListElement t)
//-iteratorInterface
{
tail = t;
reset();
}
//+iteratorInterface
//+iteratorReset
/**
* Resets iterator to consider the head of the list.
*
* @post rests iterator to point to head of list
*/
public void reset()
//-iteratorInterface
{
if (tail == null) current = null;
else current = tail.next();
}
//-iteratorReset
//-iteratorConstructor
//+iteratorInterface
//+iteratorHasMoreElements
/**
* Determine if there are unconsidered elements.
*
* @post returns true if some elements not visited
*
* @return True iff some element has not been considered.
*/
public boolean hasNext()
//-iteratorInterface
{
return current != null;
}
//-iteratorHasMoreElements
//+iteratorInterface
//+next
/**
* Return the current value and increment iterator.
*
* @pre hasNext()
* @post returns current element, increments iterator
*
* @return The current value before incrementing.
*/
public ELTTYPE next()
//-iteratorInterface
{
ELTTYPE result = current.value();
if (current == tail) current = null;
else current = current.next();
return result;
}
//-next
//+iteratorInterface
//+iteratorValue
/**
* Determine the current value of iterator.
*
* @pre hasNext()
* @post returns current value
*
* @return The current value of the iterator.
*/
public ELTTYPE get()
//-iteratorInterface
{
return current.value();
}
//-iteratorValue
//+iteratorInterface
}
//-iteratorInterface