package DataStructures; import java.util.NoSuchElementException; /** * A linked list implementation of a queue * @param the type of elements held in this queue * @author Peter Williams */ public class QueueList implements Queue { private ListNode front; // read from here private ListNode back; // write to here /** * Creates an empty queue suitable for holding items of type E */ public QueueList() { front = null; } public boolean isEmpty() { return front == null; } // join the back public void enqueue(E item) { if (front == null) { front = back = new ListNode(item, null); } else { back = back.next = new ListNode(item, null); } } // leave the front public E dequeue() { if (front == null) { throw new NoSuchElementException(); } else { E item = front.data; front = front.next; return item; } } }