import DoublyLinkedNode;

/**
 * A quick-and-dirty linked implementation of queues.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of April 2000
 */
public class QuickQueue {
    // +--------+----------------------------------------
    // | Fields |
    // +--------+

    /** 
     * The front of the queue.  Set to null when the queue 
     * is empty. 
     */
    protected DoublyLinkedNode front;

    /**
     * The back of the queue.  Set to null when the queue
     * is empty.
     */
    protected DoublyLinkedNode back;

    // +--------------+----------------------------------
    // | Constructors |
    // +--------------+

    public QuickQueue() {
        this.front = null;
        this.back = null;
    } // QuickQueue()

    // +---------+---------------------------------------
    // | Methods |
    // +---------+

    /**
     * Get and remove the first element in the queue.
     * Pre: The queue is not empty.
     */
    public Object dequeue() {
        // Grab the front.
        Object temp = this.front.getContents();
        // And drop it
        this.front = this.front.getNext();
        // Return
        return temp;
    } // dequeue()

    /**
     * Determine if the queue is empty.
     */
    public boolean isEmpty() {
        return (this.front == null);
    } // isEmpty()

    /**
     * Add an element to the end of the queue.
     */
    public void enqueue(Object newValue) {
        // Build the new node.
        DoublyLinkedNode temp = 
          new DoublyLinkedNode(newValue);
        // Special case: Empty queue
        if (this.front == null) {
            this.front = temp;
            this.back = temp;
        }
        // Normal case: Shove it on the end and update the back.
        else {
            this.back.setNext(temp);
            temp.setPrev(this.back);
            this.back = temp;
        }
    } // enqueue(Object)

} // class QuickQueue

