Site Search:

maze quiz 4

import java.util.Iterator;
public class MyQueue<Item> implements Iterator<Item> {
    private int N;
    private Node first, last;
    private class Node {
        Item item;
        Node next;
    }
    public boolean isEmpty() {return first == null;}
    public int size() {return N;}
    public Item poll() {
        Item item = first.item;
        first = first.next;
        //what is missing here?
        N--;
        return item;
    }
    public void add(Item item) {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if(isEmpty()) first = last;
        else oldlast.next = last;
        N++;
    }
    public Iterator<Item> iterator() {
        return MyIterator();
    }
    private class MyIterator implements Iterator<Item> {
        Node current = first;
        public boolean hasNext() {return current != null;}
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}