Circular Linked List Insertion and Deletion in Java

Java Program for Ciruclar Linked List Insertion and Deletion

We will look at different ways to do insertion or deletion in a circular linked list in Java at different possible positions.

circular Linked List in Java

What is a Circular Linked List

A circular Linked list is a connected series of nodes. Where each node has a data value and a next pointer.

The next reference for each node has the location of the next node.

The first node in the circular linked list is called the head node and the last node (sometimes referred to as the tail node) has the address of the first node.

Thus making whole connected nodes circular in nature.

  • Head – The first node in Circular Linked List
  • Next – Has the location for the next node in the circular linked list
  • Data – Part of the node that stores the actual data value
  • Node – Combination of Data value and next
Insertion and deletion for Circular Linked List in Java

Some variations of the Circular linked list may also require storing the tail along with the head. The tail tells us where the circular linked list is ending.

Circular Linked List in Java Insertion and deletion

Structure of a Linked Circular List in Java

Below is the structure –

// Node Class
class Node{
    int data;
    Node next;

    Node(int x) // parameterized constructor
    {
        data = x;
        next = null;
    }
}

Insertion in a Circular Linked List in Java

We can do insertion at the following –

  • At Start
  • At end
  • After a node

We will code for all three possibilities –

Run
public class Main {  
    //node declaration.  
        public class Node{  
            int data;  
            Node next;  
            public Node(int data) {  
                this.data = data;  
            }  
        }  
  
        //Declaring head and tail pointer as null.  
        public Node head = null;  
        public Node tail = null;  
  
        //add new node at the end of the list.  
        public void addAtStart(int data){  
            //Create new node  
            Node newNode = new Node(data);  
            //Checks if the list is empty.  
            if(head == null) {  
                head = newNode;  
                tail = newNode;  
                newNode.next = head;  
            }  
            else {  
                
                Node temp = head;  
                
                newNode.next = temp;  
                
                head = newNode;  
                //Since, it is circular linked list tail will point to head.  
                tail.next = head;  
            }  
        }  
  
        //Displays all the nodes in the list  
        public void show() {  
            Node current = head;  
            if(head == null) {  
                System.out.println("empty list");  
            }  
            else {  
                System.out.println("");
                 do{     
                    System.out.print(" "+ current.data);  
                    current = current.next;  
                }while(current != head);  
                System.out.println();  
            }  
        }  
  
        public static void main(String[] args) {  
            Main cl = new Main();  
  
            //Adding 2 to the list  
            cl.addAtStart(2);  
            cl.show();  
            //Adding 4 to the list  
            cl.addAtStart(4);  
            cl.show();  
            //Adding 6 to the list  
            cl.addAtStart(6);  
            cl.show();  
            //Adding 8 to the list  
            cl.addAtStart(8);  
            cl.show();  
        }  
}  
OUTPUT

2

4 2

6 4 2

8 6 4 2

Deletion in a Circular Linked List in Java

We can do deletion at the following –

  • At Start
  • At end
  • Deleting nth node

We will code for all three possibilities –

Run
public class Main {             
    public static void main(String[] args) {  
        Main Obj = new Main();  
        Obj.add(10); 
        Obj.add(20);  
        Obj.add(30);
        Obj.add(40); 
        System.out.println("List Before Deletion");  
        Obj.print();
        Obj.deleteLast();
        System.out.println("List After Deletion");
        Obj.print();
        } 
    public class Node{  
        int element;  
        Node next; 
        
        public Node(int element) {  
            this.element = element;  
        }  
    } 
    public Node head = null;  
    public Node tail = null;
    public void print() {  
        Node current = head;    
        if(head == null) {  
            System.out.println("Empty List");  
        }  
        else {
            do{     
                //Prints each node by incrementing pointer.  
                System.out.print(" "+ current.element);  
                current = current.next;  
            }while(current != head);  
                        System.out.println();  
                    }  
                }
                
    public void add(int element){  
        Node newNode = new Node(element);  
        if(head == null) {   
            head = newNode;  
            tail = newNode; 
            newNode.next = head;  
            }  
        else {  
            tail.next = newNode;  
            tail = newNode; 
            tail.next = head;  
        }  
    }
    public void deleteLast() {  
        if(head == null) {  
            return;  
        }  
        else {  
            if(head != tail ) {  
                Node current = head;  
                while(current.next != tail) {  
                    current = current.next;  
                }  
                tail = current; 
                tail.next = head;  
            }  
             else {  
                head = tail = null;  
            }  
        }  
    }   
    }
List Before Deletion
 10 20 30 40
List After Deletion
 10 20 30