Insertion at the End of a Circular Linked List in JAVA
JAVA Program to Insert a Node in the end of a Circular Linked List
Circular linked list is a very flexible data structure where you can traverse through it without it being at a random location. Inserting a node at the end of the circular linked means replacing the address of the last node with the address of new node and then making the new node point towards the head node.
Below in this page we will see two methods for insertion at the end of a circular linked list in java.
Steps to insert a node at the End of a Circular Linked List
There are can be two conditions here –
- The circular linked list is empty –
- If the circular linked is empty which means the head is null then the new node will be added as head.
-
Both the head and the tail will point towards the head because there is only one element.
- The circular linked list is not empty –
- A head is the first node of the circular linked list which can be considered as the starting of the list and points towards the next element.
- The tail is the last element of the circular linked list and it point towards the head making the list appear circular.
- If the list is not empty which means if the list has elements the last node will be the tail.
- To insert an element in the end of circular linked list, the present tail should be replaced by the new node.
- By changing the node referred by the current tail to the newly added node, it has become the new tail.
- Now, make the new tail point towards the head of the circular linked list and the element is added in the end of the circular linked list.
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 to insertion at the end of a circular linked list
- insertEnd(element)
- IF Head==Null
- Head -> newNode
- Tail -> newNode
- newNode->next = Head
- EXIT
- ELSE
- tail->next = newNode
- tail = newNode
- tail->next = Head
- EXIT
Code for insertion at the end of a circular Linked List in Java
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.insertEnd(11); Obj.insertEnd(22); Obj.insertEnd(33); Obj.insertEnd(44); 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 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 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
Nodes of the circular linked list: 11 22 33 44
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.insertEnd(11); Obj.insertEnd(22); Obj.insertEnd(33); Obj.insertEnd(44); 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 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 print() { //print function Node current = head; if(head == null) { System.out.println("List is empty"); } else { System.out.println("Nodes of circular Linked List "); do{ System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } }
Output
Nodes of circular Linked List 11 22 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