/**
 * QNode - a single node in a linked list implementation of a Queue.
 */
class QNode 
{
	Object data;
	QNode next;
}

/**
 * Queue - a simple linked list implementation of a queue.
 * @author Paul Chew
 */
public class Queue 
{
 	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.
	 */
	public static void main (String[] args) {
		Queue q = new Queue();
		System.out.println ("Created.");
		q.put(new Integer(5)); q.put(new Integer(2));
		System.out.println (q.toString());
		// Prevent the jview.exe window from immediately disappearing.
		try {System.in.read();} catch (java.io.IOException e) {}
	}
}

