package com.linkedlist;

public class Node {
    private String data;
    public Node prev;
    public Node next;

    public Node (String data) {
        this.data = data;
    }

    public Node(String data, Node prev, Node next) {
        this.data = data;
        this.prev = prev;
        this.next = next;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public void setData(Node itemNode, String data) {

    }
}

package com.linkedlist;

public class DLList {
    Node head;
    Node tail;
    Node listItemNode;

    public DLList() {
        head = new Node(null, null, null);
        tail = head;
    }

    public void add(String value) {
        if(head == tail) {
            head.setData(value);
            //head.data = value;
            head.prev = null;
            tail = new Node(null, null, null);
            head.next = tail;
        }
        else if(tail.getData() == null) {
            tail.setData(value);
            tail.next = null;
            tail.prev = head;
        }
        else {
            listItemNode = new Node(value, tail, null);
            tail.next = listItemNode;
            tail = listItemNode;
        }
    }

    public  void insert(int insertPos, String value) {
        Node p = head;
        Node newNode;
        int i = 0;
        //Move to to the insertion point
        while(p.next != null && i < insertPos -1) {
            p = p.next;
            i++;
        }
        newNode = new Node(value, null, null);
        newNode.next = p.next;
        p.next = newNode;
        newNode.prev = p;
        newNode.next.prev = newNode;
    }

    public void delete(int deletePos) {
        Node p = head;
        int i = 0;
        //Move to the deletion point
        while(p.next != null && i < deletePos -1) {
            p = p.next;
            i++;
        }
        //Save the mode you want to delete
        Node temp = p.next;
        p.next = p.next.next;
        p.next.prev = p;
        temp.next = null; //Set the deleted node next to null
        temp.prev = null; //Set the deleted node prev(ious) to null
    }

    public void print() {
        Node p = head;
        Node end = null;
        System.out.print("Start -> ");
        while(p != null) {
            String data = p.getData();
            System.out.print(data + " -> ");
            end = p;
            p = p.next;
        }
        System.out.println("End");

        p = end;

        System.out.print("End -> ");
        while (p != null) {
            String data = p.getData();
            System.out.print(data + " -> ");
            p = p.prev;
        }
        System.out.println("Start");
        System.out.println();
    }
}

package com.linkedlist;

public class DLListAll {

    public static void main(String[] args) {
        DLList linkedDList = new DLList();

        linkedDList.add("Carmarthen");
        linkedDList.add("Llanelli");
        linkedDList.add("Swansea");
        linkedDList.add("Neath");
        linkedDList.add("Port Talbot");
        linkedDList.add("Bridgend");
        linkedDList.add("Cardiff");
        linkedDList.add("Newport");
        linkedDList.add("Bristol");
        linkedDList.add("Swindon");
        linkedDList.add("Reading");
        linkedDList.add("London Paddington");

        linkedDList.print();

        linkedDList.insert(2, "Gowerton");
        linkedDList.print();

        linkedDList.delete(2);
        linkedDList.print();
    }
}