/**
 * Queue - a simple linked list implementation of a queue.
 * @author Paul Chew
 */
public class Queue {
	
	/**
	 * Internal class corresponding to a single linked node.
	 * Private implies that it's "invisible" outside of Queue.
	 * Static implies that it's associated with the entire class rather
	 * than with a single instance.
	 */	private static class QNode {
		Object data;			// Holds the data
		QNode next = null;		// Initialized to null	}	
 	private QNode head, last;	// Last is valid only when head is nonnull

	/**
	 * Put a new item into the Queue.
	 * @param item the Object to put into the Queue
	 */
	public void put (Object item) {
		QNode node = new QNode();
		node.data = item;
		if (head == null) head = node;
		else last.next = node;
		last = node;
	}

	/**
	 * Get the first item from the Queue.
	 * @return the first item of the Queue
	 */
	public Object get () {
		Object result = head.data;
		head = head.next;
		return result;
	}

	/**
	 * Check if the Queue is empty.
	 * @return true iff the Queue is empty
	 */
	public boolean isEmpty () {
		return head == null;
	}

	/**
	 * Clear the Queue.
	 */
	public void clear () {
		head = null;
	}

	/**
	 * Creates a String that represents the Queue.
	 * @return a String representation of the Queue
	 */
	public String toString () {
		if (head == null) return "<<empty>>";
		StringBuffer buf = new StringBuffer(head.data.toString());
		for (QNode cur = head.next; cur != null; cur = cur.next)
			buf.append(" "  + cur.data);
		return buf.toString();
	}

	/**
	 * Method used when run as an application.	 * This is a handy place to put test code for a class.
	 */
	public static void main (String[] args) {
		Queue q = new Queue();
		System.out.println ("Created.");
		q.put(new Integer(5)); q.put(new Integer(33));
		System.out.println (q.toString());
	}
}

