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

public class LinkedList
{
	// attributes
	private Node first = null;
	private Node last = null;
	private int size = 0;

	// default constructor
	public LinkedList()
	{
	}

	// 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 (size == 0)
		{
			return -1;
		}

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

		//3) remove from the middle (b/w first and last) of the LL
		if (index != 0 && index != size -1)
		{
			int count = 0;
			Node previous = first;
			for (Node current = first; 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 first
		if (index == 0 && first != last)
		{
			int tmp = first.num;
			first = first.next;
			size--;
			return tmp;
		}

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

			for (Node current = first; current != null; current = current.next)
			{
				if (current.next == last)
				{
					last = current;
					last.next = null;	//current.next = null;
					size--;
					return tmp;
				}
			}
			return tmp;
		}
	}
	
	// add() 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;
			first = myNode;
			last = myNode;
			size++;
		}
		else //2nd case - appending to non-empty LL
		{
			Node myNode = new Node()
			myNode.num = num;
			last.next = myNode;
			last = myNode;
			size++;
		}
	}
	
	//add num to the location index
	public void add(int index, int num)
	{
			//0) adding to empty LL
			//1) adding to the first (beginning)
			//2) adding to the last (end)
			//3) adding to the middle
			//4) adding to an index that is out of bounds
	}

	//int size() - returns the size of the list
	
	//void clear() - removes all the elements in the list
	
	//int get(int index) - returns the number found in the Node at the specified index
	
	//int getFirst() - returns the first element in the list (-999 if list is empty)
	
	//int getLast() - returns the last element in the list (-999 if list is empty()
	
	//void addFirst(int num) - adds num so that it is the first element
	
	//void addLast(int num) - adds num so that it is the last
	
	//int removeFirst() - removes and returns the first element (-999 if list is empty())
	
	//int removeLast() - removes and returns the first element (-999 if list is empty())
	
	//int indexOf(int num) traverses the linked list and returns the index of the Node with num
	
	//void print() traverses the linked list and prints out the numbers in each Node

	
	
}