package com.linkedlist;

public class NodeC {
    public String data;
    public NodeC next;

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

    public String getData() {
        return this.data;
    }

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

package com.linkedlist;

public class CLList {
    NodeC head;
    NodeC tail;
    NodeC listItemNode;

    public CLList() {
        head = new NodeC(null);
        tail = new NodeC(null);

        head.next = tail;
        tail.next = head;
    }

    public void add(String value) {
        //System.out.println("CList Add");
        if(head.data == null) {
            //System.out.println("LList tail == head Empty List at this point");
            head.data = value;
        } else if(tail.data == null) {
            //System.out.println("LList has a head but empty tail data");
            tail.data = value;
        }
        else {
            //System.out.println("LList tail != head List has in place Head and Tail data values");
            listItemNode = new NodeC(value);
            listItemNode.next = tail.next;
            tail.next = listItemNode;
            tail = listItemNode;
        }
    }

    public void insert(int insertPosition, NodeC newNode) {
        NodeC p = head;
        System.out.println();
        System.out.println("Inserting a Node");
        int i = 0;
        boolean positionFound = true;
        // Move the node to the insertion point
        while(p.next != head && i < insertPosition -1) {
            p = p.next;
            i++;
            if(p.next == head) {
                System.out.println("\nInsert Position not available in the Circular List");
                positionFound = false;
                break;
            }
        }
        if(positionFound) {
            newNode.next = p.next; //
            p.next = newNode;
        }
    }

    public void remove(int insertPosition) {
        NodeC p = head;
        System.out.println();
        System.out.println("Removing a Node");
        int i = 0;
        boolean positionFound = true;
        // Move the node to the insertion point
        while(p.next != head && i < insertPosition -1) {
            p = p.next;
            i++;
            if(p.next == head) {
                System.out.println("\nInsert Position not available in the Circular List");
                positionFound = false;
                break;
            }
        }
        if(positionFound) {
            NodeC temp = p;
            temp = temp.next;
            p.next = p.next.next;
            temp = null;
        }
    }

    public void print() {
        System.out.println("CList Print List");
        NodeC p = head;
        while(p != null) {
            System.out.print(p.data + "->");
            p = p.next;
            if(p == head) {break;}
        }
        //System.out.println("Tail.");
    }
}

package com.linkedlist;

public class CLLIstDemo {
    public static void main(String[] args) {
        CLList circleList = new CLList();

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

        circleList.print();
        circleList.insert(2, new NodeC("Gowerton"));
        circleList.print();
        circleList.remove(2);
        circleList.print();
    }
}