/* a class for an iterator over nodes that store arbitrary objects
/*             this iterator does not support the remove method
 * @author	Biagioni, Edoardo
 * @assignment	lecture 12
 * @date	February 25, 2008
 */

import java.util.*;

public class ListIterator<T> extends LinkedList<T> implements Iterator<T> {
    /* a single field to store the first node, or null.
     */
    private Node<T> nextNode;

    public ListIterator(LinkedList<T> list) {
	nextNode = list.head;	// head is protected, so accessible to subclass
    }

    /* can at least one more value that can be accessed with this iterator?
     * @return  whether there is another node
     */
    public boolean hasNext() {
        return (nextNode != null);
    }

    /* @return  the next value
     * @throws  NoSuchElementException if there is no such value
     */
    public T next() {
	if (hasNext()) {
	    T value = nextNode.getValue();
	    nextNode = nextNode.getNext();
	    return value;
	} else {
	    throw new NoSuchElementException("NodeIterator");
	}
    }

    /* @throws  UnsupportedOperationException, meaning remove is not supported
     */
    public void remove() {
	throw new UnsupportedOperationException("NodeIterator");
    }

}