﻿/*
Filename: LinkedList.java
Name: Mr.  Bui
Date: 11/21/2016
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 Integer.MIN_VALUE;
		}

		//1) remove from empty LL - return error code
		else if (size == 0)
		{
			return Integer.MIN_VALUE;
		}

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

		//3) remove from the middle (b/w first and last) of the LL
		else 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.data;
					previous.next = current.next;
					size--;
					return x;
				}

				previous = current;
				count++;
			}
		}

		//4) remove from the first
		else if (index == 0 && first != last)
		{
			int tmp = first.data;
			first = first.next;
			size--;
			return tmp;
		}

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

			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;
		}

		return Integer.MIN_VALUE;
	}
	
	// add() adds a new number to the end of the LinkedList
	public void add(int data)
	{

		//1st case - appending to empty LL
		if (isEmpty())
		{
			Node myNode = new Node();
			myNode.data = data;
			first = myNode;
			last = myNode;
			size++;
		}
		else //2nd case - appending to non-empty LL
		{
			Node myNode = new Node();
			myNode.data = data;
			last.next = myNode;
			last = myNode;
			size++;
		}
	}
	
	//add data to the location index
	public void add(int index, int data)
	{
		//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 - print error message

	}

	//boolean isEmpty() returns true if the size is 0 and false otherwise

	//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 (Integer.MIN_VALUE if list is empty)

	//int getLast() - returns the last element in the list (Integer.MIN_VALUE if list is empty()

	//void addFirst(int data) - adds data so that it is the first element

	//void addLast(int data) - adds data so that it is the last

	//int removeFirst() - removes and returns the first element (Integer.MIN_VALUE if list is empty())

	//int removeLast() - removes and returns the first element (Integer.MIN_VALUE if list is empty())

	//int indexOf(int data) 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

}