package LinkedList.Doubly;

public class ElementenList {
	Node head;
	Node newNode;
	public ElementenList(){
		newNode = new Node();
	}
	
	public void print() {
		Node temp = head;
		System.out.print("[");
		while(temp != null) {
			System.out.print(temp.value + " ");
			temp = temp.next;
		}
		System.out.print("]");
	}
	
	// Add Methoden
	public Node addLast(int value) {
		Node newNode = new Node();
		newNode.value = value;
		
		if (head == null) {
			head = newNode;
			return newNode;
		}
		
		Node temp = head;
		while(temp.next != null)
			temp = temp.next;
		
		temp.next = newNode;
		newNode.prev = temp;
		return newNode;

	}
	public Node addFirst(int value) {
		Node newNode = new Node();
		newNode.value = value;
		
		if (head == null) {
			head = newNode;
			return newNode;
		}
		newNode.next = head;
		head = newNode;
		return newNode;
		
	}
	public Node addAtIndex(int value,int index) {
		if (head == null) {
			head = newNode;
			return newNode;
		}
		if (index < 0)
			return null;
			
		newNode.value = value;
		Node temp = head;
		for (int i = 0; i < index - 1 && temp.next != null; i++) 
			temp =  temp.next;
		
		
		newNode.next = temp.next;
		newNode.prev = temp;
		temp.next = newNode;
		return newNode;
	}
	
}
