Insertion in Circular Linked List in java

Insertion in circular linked in Java  programming?

 The circular linked list is another type of linked list. The node is an element of the list, and it has two parts that are, data and next. Data represents the data stored in the node and next is the pointer that will point to next node. Head will point to the first element of the list, and tail will also point to the first element in the list.. 

In this article, we will discuss what are different types of insertion and how to perform them .

Insertion in Java

Insertion in Circular Linked List in Java

We can perform three different types of insertion in circular linked list, these different types of insertion are:-

  • Insertion at beginning .
  • Insertion at specific position .
  • Insertion at end .

ALGORITHM FOR INSERTION AT BEGINING:

  • Make a Node class which represents a node in the list. It consists two properties data and next which will point to the next node.
  • Create another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: addAtStart() and show() .
  • addAtStart() will add the node to the beginning of the list:
    •  first checks whether the head is null (empty list), then it will make the node as the head.
    • Both head and tail will point to newly added node.
    • If the list is not empty, then the newly added node will become the new head, and it will point to previous head.
Insertion in Circular Linked List in java

Insertion at begining Java code :

public void insertBegin(int element){
        Node newEle = new Node(element);
        if(head == null) {
            head = newEle;
            tail = newEle;
            newEle.next = head;
            newEle.prev=head;
        }
        else {
            head.prev = newEle;
            newEle.prev=tail;
            newEle.next = head;
            tail.next = newEle;
            head=newEle;
        }
        size++;
    }

ALGORITHM FOR INSERTION AT END:

  • Make a Node class which represents a node in the list. It consist two properties data and next which will point to the next node.
  • Make another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: addAtEnd() and show() .
  • addAtEnd() will add the node to the end of the list:
    • It first checks whether the head is null (empty list), then it will add the node as the head.
    • Both head and tail will point to the newly added node.
    • If the list is not empty, then the newly added node will become the new tail, and previous tail will point to new node as its next node. The new tail will point to the head.
Insertion in Circular Linked List

Insertion at End :

public void insertEnd(int element){
       Node newEle=new Node(element);
        if(head == null) {
            head = newEle;
            tail = newEle;
            newEle.next = head;
            newEle.prev=head;
        }
        else{
            tail.next=newEle;
            newEle.next=head;
            newEle.prev=tail;
            head.prev=newEle;
            tail=newEle;
        }
    }

ALGORITHM FOR INSERTION AT Nth NODE :

  • Make a Node class which represents a node in the list. It consist two properties data and next which will point to the next node.
  • Make another class for creating the circular linked list and it has two nodes: head and tail. It has two methods: addAtNthPos() and show() .
  • addAtNthPos() will add the Nth position to the list:
    • It first checks whether the head is null (empty list), then it will add the node as the head.
    • Both head and tail will point to the newly added node.
    • If the list is not empty, then the newly added node will become the previous of the nth node , and new node next will be nth node.
Insertion in Between The Nodes in Circular Linked List in java
public void insertAfterPosition(int n, int data) {
        int len = getLength();

        // if insertion position is 0 means entering at start
        if (n == 0) {
            insertBegin(data);
            return;
        }
        // means inserting after last item
        if (n == len) {
            insertEnd(data);
            return;
        }

        // else insertion will happen somewhere in the middle


        if (n < 1 || len < n) System.out.println("Invalid position");
        else { Node freshNode = new Node(data);
            // can remove null assignments also (constructor takes care of these)
            // added just for explanation purpose

            freshNode.next = null;

            freshNode.prev = null;
            // nthNode used to traverse the Linked List

            Node nthNode = head;

            // traverse till the nth node

            while (--n > 0) {
                nthNode = nthNode.next;
            }

            Node nextNode = nthNode.next; // (n+1)th node

            // assigning (n+1)th node's previous to this new node
            nextNode.prev = freshNode;

            // new_node's next assigned to (n+1)th node
            freshNode.next = nextNode;
            // new_node's previous assigned to nth node
            freshNode.prev = nthNode;

            // assign nth node's next to new_node
            nthNode.next = freshNode;
        }
    }

Java code for insertion in a circular Linked List

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • Circular Linked List in –
    C | C++ | Java
  • Insertion in Circular Linked List –
    C | C++ | Java
  • Insertion at the beginning–
    C | C++ | Java
  • Insertion at the end –
    C | C++ | Java
  • Insertion at nth position –
    C | C++ | Java
  • Deletion in Circular Linked List –
    C | C++ | Java
  • Deletion from beginning in Circular Linked List –
    C | C++ | Java
  • Deletion from nth position in Circular Linked List –
  • Deletion from end in Circular Linked List –
    C | C++ | Java
  • Insertion and Deletion in Circular Linked List – C | C++ | Java
  • Split a Circular Linked List in two halves –
    C | C++ | Java
  • Count nodes in Circular Linked List –
    C | C++ | Java
  • Sorted Insert In Circular Linked List –
    C | C++ | Java
  • Insertion in the middle in Circular Linked List –
    C | C++ | Java

Circular Linked List