package util.list; import java.util.NoSuchElementException; public class QueueIterator implements Iterator { private Queue myQueue = null; private ListLink cursor = null; QueueIterator(Queue aQueue) { myQueue = aQueue; start(); } public void start() { cursor = myQueue.getHead(); } public boolean cont() { return cursor != null; } public void next() { cursor = cursor.getNext(); } public Object getCurrent() { return cursor.getInfo(); } public Object getNext() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException(); return cursor.getNext().getInfo(); } public boolean hasNext() { return (cont() && cursor.getNext() != null); } public void insertNext(Object info) { cursor.setNext(new ListLink(info, cursor.getNext())); ListLink last = myQueue.getTail().getNext(); if (last != null) myQueue.setTail(last); } public void removeNext() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException(); ListLink next = cursor.getNext(); if (myQueue.getTail() == next) myQueue.setTail(cursor); cursor.setNext(next.getNext()); } }