package lab.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* An iterator over Node
-based linked sequences.
*
* @param the element type held by a node
*/
public class NodeIterator implements Iterator> {
private Node prev;
private Node curr;
/**
* Create an iterator that starts iterating at the node
* head
*
* @param head the node at which to start iterating
*/
public NodeIterator(Node head) {
this.curr = head;
this.prev = null;
}
/**
* Returns true if this iterator has another node in the sequence.
*
* @return true if and only if this iterator has another node
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
return curr != null;
}
/**
* Returns the next node in the sequence.
*
* @return the next node in the sequence
* @throws NoSuchElementException if no more nodes are available
* @see java.util.Iterator#next()
*/
@Override
public Node next() {
if (this.hasNext()) {
this.prev = this.curr;
this.curr = this.curr.getNext();
}
else {
throw new NoSuchElementException();
}
return this.prev;
}
/**
* The remove operation is not supported by this implementation of Iterator.
*
* @throws UnsupportedOperationException if this method is invoked
* @see java.util.Iterator#remove()
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}