Insertion in Circular Linked List in java

Performing Insertion in Circular Linked List in Java Programming

About Insertion in Circular Linked List in Java

In this Insertion in Circular Linked List in Java article, we will explore the different types of insertion operations and how to implement them effectively.

A circular linked list is a variation of a linked list where each node consists of two parts: data (which stores the value) and next (a pointer to the next node). The head points to the first node, and the tail also points to the first node, forming a continuous loop.

Insertion in Circular Linked List in java

Insertion in Circular Linked List in Java

Circular Linked List is a type of linked list where the last node points back to the first node instead of null, forming a circular chain. This structure is particularly useful in applications requiring cyclic traversal.

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

1. Insertion at the Beginning

This involves inserting a new node before the first node. We need to adjust the last node’s next pointer to point to the new node.

2. Insertion at the End

Inserting a new node at the end involves adjusting the last node’s next pointer to point to the new node, and then pointing the new node’s next to the head node.

3. Insertion After a Specific Node

In this case, we locate the target node and insert the new node right after it, adjusting the pointers accordingly.

1. Algorithm for Insertion at the Beginning of a Circular Linked List

  1. Node Class: Create a Node class representing a node in the list, containing two properties: data (the value of the node) and next (a reference to the next node).
  2. Circular LinkedList Class: Develop a separate class for the circular linked list, containing two nodes: head and tail.

This class will have two main methods: addAtStart() and show().

addAtStart() Method:

  1. If the list is empty (head is null), the new node becomes the head, and both head and tail will point to this node.
  2. And If the list is not empty, the new node is set as the new head, and its next property is set to the previous head.
Insertion in Circular Linked List in java

Insertion at beginning 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++;
    }

Prime Course Trailer

Related Banners

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

2. Algorithm for Insertion at the End of a Circular Linked List

  1. Node Class: Define a Node class that represents a node in the list, consisting of two properties: data (the node’s value) and next (a reference to the next node).
  2. Circular LinkedList Class: Implement a separate class for the circular linked list, containing two nodes: head and tail.

This class includes two primary methods: addAtEnd() and show().

addAtEnd() Method:

  • If the list is empty (head is null), the new node is added as the head, and both head and tail will reference this node.
  • Where as If the list is not empty, the new node is set as the new tail, and the previous tail’s next property is updated to point to this new node.
  • And the next property of the new tail is set to the head, maintaining the circular structure.
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;
        }
    }

3. Algorithm for Insertion at the Nth Node in a Circular Linked List

  1. Node Class: Define a Node class representing a node in the list, containing two properties: data (the node’s value) and next (a reference to the next node).
  2. Circular Linked List Class: Create a separate class for the circular linked list with two nodes: head and tail.

This class includes two main methods: addAtNthPos() and show().

addAtNthPos() Method:

  • If the list is empty (head is null), the new node is added as the head, and both head and tail will reference this node.
  • And If the list is not empty, the new node is inserted at the specified position n. The new node’s next property is set to the current node at position n, and the previous node’s next is updated to point to the new node.
Insertion in Between The Nodes in Circular Linked List in java

Insertion at specific position :

// Insert after a specific node

public void insertAfter(int data, int target) { if (last == null) return; Node newNode = new Node(data); Node temp = last.next; do { if (temp.data == target) { newNode.next = temp.next; temp.next = newNode; if (temp == last) { last = newNode; } return; } temp = temp.next; } while (temp != last.next); }

Java code for insertion in a circular Linked List

The two methods are:

  1. insertAfter(): This method is present in the first code and is used to insert a new node after the specified nth node in a Circular Singly Linked List.
  2. insertAfterPosition(): This method is present in the second code and is used to insert a new node after the specified nth node in a Circular Doubly Linked List.

Learn DSA

Conclusion:

  • Insertion operations in Circular Linked Lists can be performed at the beginning, end, or after a specific node.
  • Each insertion method has its specific use case and time complexity implications.
  • Understanding these operations provides a strong foundation for implementing more advanced data structures and algorithms.

FAQ's Related Insertion in Circular Linked List

Answer:

In Java, a node is typically defined as a class with three attributes: int data, Node next, and Node prev. This structure allows each node to store data and maintain links to both the next and previous nodes.

Answer:

  1. Create a new node with the given data.
  2. Set the new node’s next to the current head and prev to the tail.
  3. Update head.prev to the new node and tail.next to the new node.
  4. Update head to the new node.

Answer:

Both operations have a time complexity of O(1) because they involve a constant number of pointer updates without the need to traverse the entire list.

Answer:

  1. Traverse the list to locate the specified position.
  2. Create a new node and adjust its next and prev pointers to link it between the nodes at position and position + 1.
  3. Update the surrounding nodes’ pointers to maintain the circular structure.

Answer:

  1. Java handles null pointers by throwing a NullPointerException.
  2. To prevent this, checks are implemented before accessing node references.

For example, checking if (head == null) before accessing head.next ensures safe execution.

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