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 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 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 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.
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
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.insertBegin(11); Obj.insertBegin(22); Obj.insertEnd(33); Obj.insertEnd(44); Obj.insertAfter(3,77); Obj.print(); } public class Node{ int element; Node next; public Node(int element) { this.element = element; } } public Node head = null; public Node tail = null; int size=0; public void insertBegin(int element){ Node newEle = new Node(element); if(head == null) { head = newEle; tail = newEle; newEle.next = head; } else { newEle.next=head; head=newEle; tail.next = head; } size++; } public void insertEnd(int element){ Node newEle = new Node(element); if(head == null) { head = newEle; tail = newEle; newEle.next = head; } else { tail.next=newEle; newEle.next=head; tail=newEle; } size++; } public void insertAfter(int n,int data) { int size = calcSize(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(n < 1 || n > size) { System.out.println("Can't insert\n"); } else { Node newNode = new Node(data); // required to traverse Node temp = head; // traverse to the nth node while(--n > 1) temp=temp.next; newNode.next= temp.next; temp.next = newNode; } } public int calcSize(Node node){ int size = 0; while(node!=tail){ node = node.next; size++; } return size+1; } public void print() { //print function Node current = head; if(head == null) { System.out.println("List is empty"); } else { System.out.println("Nodes of the circular linked list: "); do{ System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } }
Output
Insertion at Beginning : 22 11 Insertion at End : 22 11 33 44 Insertion after 2nd position 44 22 77 11 33
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.insertBegin(11); Obj.insertBegin(22); System.out.println("Insertion at Beginning : "); Obj.print(); Obj.insertEnd(33); Obj.insertEnd(44); System.out.println("Insertion at End :" ); Obj.print(); Obj.insertAfterPosition(2,77); System.out.println("Insertion after 2nd position"); Obj.print(); } public class Node{ int element; Node next; Node prev; public Node(int element) { this.element = element; } } public Node head = null; public Node tail = null; int size=0; 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++; } 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; } } 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; } } public int getLength() { int size = 0; Node temp=head; // traverse to the last node each time incrementing the size while (temp != tail) { temp = temp.next; size++; } return size; } public void print() { //print function Node current = head; if(head == null) { System.out.println("List is empty"); } else { do{ System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } }
Output
Insertion at Beginning : 22 11 Insertion at End : 22 11 33 44 Insertion after 2nd position 22 11 77 33 44
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
Circular Linked List
- Introduction to Circular Linked List
Click Here - Circular Linked List Applications
Click Here - Circular Linked List in –
- Insertion in Circular Linked List –
- Insertion at the beginning–
- Insertion at the end –
- Insertion at nth position –
- Deletion in Circular Linked List –
- Deletion from beginning in Circular Linked List –
- Deletion from nth position in Circular Linked List –
- Deletion from end in Circular Linked List –
- Insertion and Deletion in Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves –
- Count nodes in Circular Linked List –
- Sorted Insert In Circular Linked List –
- Insertion in the middle in Circular Linked List –
Circular Linked List
- Introduction to Circular Linked List
- Circular Linked List Applications
- Circular Linked List in – C | C++ | Java
- Insertion in Circular Linked List – C | C++ | Java
- Deletion in Circular Linked List – C | C++ | Java
- Insertion and Deletion in a 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
Login/Signup to comment