/** Program 6.23 Linked Queue Representation */ /*----------------------------------------------------------------*/ class QueueNode { Object item; QueueNode link; } /*----------------------------------------------------------------*/ class Queue { private QueueNode front; // front contains a reference to the // front item of the queue private QueueNode rear; // rear contains a reference to the // rear item of the queue private int count; // the number of items in the queue /*-----------------*/ // Java's automatically defined no-arg constructor suffices /*-----------------*/ public boolean empty() { return (count == 0); } /*-----------------*/ public void insert(Object newItem) { QueueNode temp = new QueueNode(); temp.item = newItem; temp.link = null; if (rear == null) { front = rear = temp; } else { rear.link = temp; rear = temp; } count++; } //end insert /*-----------------*/ public Object remove() { if (count == 0) { // if the queue is empty, return null return null; } else { // otherwise, return the front item Object tempItem = front.item; front = front.link; if (front == null) { rear = null; } count--; return tempItem; } } //end remove /*-----------------*/