public class ListQueue implements Queue { private class Node { Object item; Node next; } private Node head; private Node tail; public ListQueue() { clear(); } public void clear() { head = null; tail = null; } public boolean isEmpty() { return head == null; } public void enqueue(Object x) { Node n = new Node(); n.item = x; if (isEmpty()) head = n; else tail.next = n; tail = n; } public Object dequeue() { if (isEmpty()) throw new java.util.EmptyStackException(); Object x = head.item; head = head.next; return x; } public Object first() { if (isEmpty()) throw new java.util.EmptyStackException(); return head.item; } }