package fha.inf3.queue;
/**
 * implementation of a set.
 * @author tardy
 */
public class ListQueue extends AbstractQueue
{
	private ListQueueNode head;
	private ListQueueNode last;
	
	public ListQueue ()
	{}
	
	public ListQueue (Object x)
	{	this.head = new ListQueueNode(null,x);
		this.last = this.head;
	}
	
	public void enqueue(Object x)
	{	if (isEmpty())
		{	this.head = new ListQueueNode(null,x);
			this.last = this.head;
		}
		else
		{	ListQueueNode tmp = this.last;
			this.last = new ListQueueNode(null,x);
			tmp.next = this.last;
		}	
	}
	
	public Object dequeue()
	{	if (isEmpty())
		{	throw new QueueEmptyExceptions();}
		else
		{	if (size() == 1)
			{	try
				{	return this.head.data;}
				finally
				{	this.head = null;
					this.last = null;
				}
			}	
			else
			{	try
				{	return this.head.data;}
				finally
				{	this.head = this.head.next;}
			}
		}
	}
	
	public int size()
	{	if (this.head == null)
		{	return 0;}
		else
		{	ListQueueNode tmp = this.head;
			int i = 1;
			while(tmp.next !=null)
			{	i++;
				tmp = tmp.next;
			}
			return i;
		}
	}
	
	public boolean isFull()
	{	return false;}
}