Insertion in the Beginning of a Circular Linked List in JAVA

Insertion at the Beginning of a circular linked list

JAVA Program to Insert a Node in the Beginning of a Circular Linked List

Insertion in any type of a Linked List is just modification of the address stored in the references of the Nodes, Insertion in the Beginning of a Circular Linked List in JAVA is also achieved using similar minor modification. For insertion in the beginning the address stored in the HEAD now points to the new node and the New Node will now point towards the Node where head was earlier pointing to. Here in this page we’ll take a look at the JAVA Program for doing so.

Given below is the description of how you can insert a node in the Beginning of a Circular Linked List

While Insertion in the Beginning of a Circular Linked List in JAVA two cases can be considered.

  • The circular linked is empty –
    • To insert an element in an empty circular list is quite easy.
    • Both the head and the tail points to the very first element in a circular linked list.
  • The circular linked list is not empty –
    • In a circular linked list the first element serves the purpose of a head. It stores the information of the next node of the circular linked.
    • To insert an element in the beginning of the linked list. The new node should point towards the previous Head making it the new head of the circular linked list.
    • The tail is the last node that points back to the first note making the linked list circular because the traversal can be done from start till end.
    • To add a node in the beginning means to change the node tail is referring to newly added node. By doing this the new node will be inserted in the beginning of the circular linked list.

Here is the Algorithm to add a Node at the Beginning of a Linked List

  • addFirst(element)
  • IF Head==Null
    • Head -> newNode
    • Tail -> newNode
    • newNode->next = Head
    • EXIT
  • ELSE
    • Node Temp = head 
    • newNode->next = Temp
    • head = newNode
    • tail->next = Head
    • EXIT

Code for insertion in JAVA

public class Main {             
    public static void main(String[] args) {  
        Main Obj = new Main();  
        Obj.addFirst(1);  
        Obj.print();  
        Obj.addFirst(2);  
        Obj.print();  
        Obj.addFirst(3);  
        Obj.print();  
        Obj.addFirst(4);  
        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 {  
            System.out.println("After adding node at the beginning");  
            do{     
                //Prints each node by incrementing pointer.  
                System.out.print(" "+ current.element);  
                current = current.next;  
            }while(current != head);  
                        System.out.println();  
                    }  
                }
                
    public void addFirst(int data){  
        Node newNode = new Node(data);  
        if(head == null) {  
            head = newNode;  
            tail = newNode;  
            newNode.next = head;  
        }  
        else {  
            Node temp = head;  
            newNode.next = temp;  
            head = newNode;  
            tail.next = head;  
        }  
    }    
}    
After adding node at the beginning
 1
After adding node at the beginning
 2 1
After adding node at the beginning
 3 2 1
After adding node at the beginning
 4 3 2 1