/*
Filename: LinkedList.java
Name: Mr.  Bui
Date: 11/5/10
Purpose: A dynamically sized list data structure
*/

public class LinkedList
{
	// attributes
	private Node head = null;
	private Node tail = null;
	private int size = 0;

	// default constructor
	public LinkedList()
	{
	}

	// isEmpty() returns true if the LinkedList is empty, and false otherwise
	public boolean isEmpty()
	{
		return size == 0;
		//return head == null;
	}

	// append() adds a new number to the end of the LinkedList
	public void add(int num)
	{

		//1st case - appending to empty LL
		if (isEmpty())
		{
			Node myNode = new Node();
			myNode.num = num;
			head = myNode;
			tail = myNode;
			size++;
		}
		//2nd case - appending to non-empty LL
		if (!isEmpty())
		{
			Node myNode = new Node()
			myNode.num = num;
			tail.next = myNode;
			tail = myNode;
			size++;
		}
	}

	// remove(int index) - removes a Node and number at a particular index
	public int remove(int index)
	{
		//0) removing from index out of bounds
		if (index >= size || index < 0)
		{
			return -1;
		}

		//1) remove from empty LL - return error code
		if (isEmpty())
		{
			return -1;
		}

		//2) remove last Node from LL
		if (index == 0 && head == tail)	// if (size == 1)
		{
			int tmp = head.num;
			head = null;
			tail = null;
			size--;
			return tmp;
		}

		//3) remove from the middle (b/w head and tail) of the LL
		if (index != 0 && index != size -1)
		{
			int count = 0;
			Node previous = head;
			for (Node current = head; current != null; current = current.next)
			{
				//if you are at the correct current, set the previous next to the current's next
				if (count == index)
				{
					int x = current.num;
					previous.next = current.next;
					size--;
					return x;
				}

				previous = current;
				count++;
			}
		}

		//4) remove from the head
		if (index == 0 && head != tail)
		{
			int tmp = head.num;
			head = head.next;
			size--;
			return tmp;
		}

		//5) remove from the tail
		if ( index == size - 1 && size > 1)
		{
			int tmp = tail.num;

			for (Node current = head; current != null; current = current.next)
			{
				if (current.next == tail)
				{
					tail = current;
					tail.next = null;	//current.next = null;
					size--;
					return tmp;
				}
			}
			return tmp;
		}
	}

		//add num to the location index
	public void add(int num, int index)
	{
			//0) adding to empty LL
			//1) adding to the head (beginning)
			//2) adding to the tail (end)
			//3) adding to the middle
			//4) adding to an index that is out of bounds
	}

	//int getNum(int index) returns the number found in the Node at the specified index
	//CREATE getNum() METHOD HERE

	//void print() traverses the linked list and prints out the numbers in each Node
	//CREATE print() METHOD HERE

	//int search(int num) traverse the linked list and returns the index of the Node with num
	//CREATE search() METHOD HERE
}