// File: Lister.java from the package edu.colorado.nodes
// Complete documentation is available from the Lister link in:
// http://www.cs.colorado.edu/~main/docs/
import java.util.Iterator;
import java.util.NoSuchElementException;
/******************************************************************************
* A Lister implements Java's Iterator<E>
* interface for a linked list made from Node<E> nodes.
* Note that this implementation of an Iterator<E> does not
* support the remove method. Any activation of remove
* results in an
* UnsupportedOperationException.
*
*
head
* a head reference for a linked list of objects
* next will return the elements
* from this linked list, one after another. If the linked list changes
* in any way before all the elements have been returned, then the
* subsequent behavior of this Lister is unspecified.
**/
public Lister(Nodetrue if there are more elements in this
* Lister; false otherwise.
**/
public boolean hasNext( )
{
return (list != null);
}
/**
* Retrieve the next element of this Lister.
* @param - none
* a bag whose contents will be added to this bag
* hasMoreElements() must be true.
* @return
* The return value is the next element of this Lister.
* Note that each element is only returned once, and then the
* Lister automatically advances to the next element.
* @exception NoSuchElementException
* Indicates that hasMoreElements() is false.
**/
public E next( )
{
E answer;
if (!hasNext( ))
throw new NoSuchElementException("The Lister is empty");
answer = list.getData( );
list = list.getLink( );
return answer;
}
public void remove( )
{
throw new UnsupportedOperationException("Lister.remove not allowed");
}
}