/** Linked queue implementation */ class Lqueue implements Queue { private Link front; // Pointer to front queue node private Link rear; // Pointer to rear queue node int size; // Number of elements in queue /** Constructors */ public LQueue() { init(); } public LQueue(int size) { init(); } // Ignore size /** Initialize queue */ private void init() { front = rear = new Link(null); size = 0; } /** Reinitialize queue */ private void clear() { init(); }; /** Put element on rear */ public void enqueue(E it) { rear.setNext(new Link(it, null)); rear = rear.next(); ++size; } /** Remove and return element from front */ public E dequeue() { assert size != 0 : "Queue is empty"; E it = front.next().element(); // Store dequeued value front.setNext(front.next().next()); // Advance front if (front.next() == null) rear = front; // Last object --size; return it; //Return object } /** @return Front element */ public E frontValue() { assert size != 0 : "Queue is empty"; return front.next().element(); } /** @return Queue size */ public int length() { return size; } }