package list.mylinkedlist;

import java.util.List;
import java.util.NoSuchElementException;

public class MyLinkedList<E> implements List<E> {
	
	/*
	 * Classe Nodo
	 */
	private static class Node<E> {
		
		// Dati del nodo
		private E data;
		
		// Riferimenti elemento precedente e successivo
		private Node<E> prev;
		private Node<E> next;
		
		// Costruttore
		public Node(E data, Node<E> prev, Node<E> next) {
			this.data = data;
			this.prev = prev;
			this.next = next;
		}
		
	}
	
	private Node<E> head = null;
	private Node<E> tail = null;
	private int size = 0;
	
	// Costruttore
	public MyLinkedList() {}
	
	// Metodi
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	public int size() {
		return size;
	}
	
	public E getFirst() {
		if (head == null) throw new NullPointerException();
		return head.data;
	}
	
	public void addFirst(E item) {
		if (item == null) throw new NullPointerException();
		if (size == 0) {
			head = tail = new Node<E>(item, null, null);
		} else {
			Node<E> newNode = new Node<E>(item, null, null);
			head.prev = newNode;
			newNode.next = head;
			head = newNode;
		}
		size++;
	}
	
	public E removeFirst() {
		if (head == null) throw new NoSuchElementException();
		if (size == 1) {
			E tmp = head.data;
			head = tail = null;
		}
	}
	
}
