LinkedListQueue.java

package edu.hawaii.ics.yucheng;

import java.util.NoSuchElementException;

/**
 * Implementation of a generic linked list queue. This class exists only to
 * support the JRE 1.5.10, which is currently installed on
 * uhunix2.its.hawaii.edu.
 */
class LinkedListQueue<T> {

    /**
     * A node class for the generic queue.
     */
    private static class Node<T> {
        /** A pointer to the next node. */
        private Node<T> myNext;

        /** The value stored in the node. */
        public final T  value;


        /**
         * Initializes a new instance of the class.
         * 
         * @param value The value stored in the node.
         */
        public Node(final T value) {
            this(value, null);
        }


        /**
         * Initializes a new instance of the class.
         * 
         * @param value The value stored in the node.
         * 
         * @param next A pointer to the next node.
         */
        public Node(final T value, final Node<T> next) {
            this.value = value;
            myNext = next;
        }


        /**
         * Returns a pointer to the next node.
         * 
         * @return A pointer to the next node.
         */
        public Node<T> next() {
            return myNext;
        }


        /**
         * Sets the pointer to the next node.
         * 
         * @param next A pointer to the next node.
         */
        public void setNext(final Node<T> next) {
            myNext = next;
        }
    }

    private Node<T> myHead = null;
    private Node<T> myTail = null;


    /**
     * Returns true if the collection contains no elements.
     * 
     * @return true if this collection contains no elements
     */
    public boolean isEmpty() {
        return null == myHead;
    }


    /**
     * Retrieves and removes the head of this queue.
     * 
     * @return the head of this queue
     * 
     * @throws NoSuchElementException if this queue is empty
     */
    public T pop() {
        if (null == myHead)
            throw new NoSuchElementException();

        final T value = myHead.value;
        myHead = myHead.next();
        if (null == myHead)
            myTail = null;

        return value;
    }


    /**
     * Inserts the specified element into this queue.
     * 
     * @param e the element to add
     */
    public void push(final T e) {
        if (null == myHead) {
            myHead = new Node<T>(e);
            myTail = myHead;
        } else {
            myTail.setNext(new Node<T>(e));
            myTail = myTail.next();
        }
    }


    /**
     * Returns the string representation of the class data.
     * 
     * @return The string representation of the class data.
     */
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("{");
        for (Node<T> node = myHead; node != null; node = node.next()) {
            builder.append(" ");
            builder.append(node.value);
            builder.append(null == node.next() ? " }" : ",");
        }
        return builder.toString();
    }
}
Valid HTML 4.01 Valid CSS