Insertion in the Beginning of a Circular Linked List in JAVA
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.
Steps to 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.
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++; }
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
Java code for insertion at beginning 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.insertBegin(33); Obj.insertBegin(44); Obj.insertBegin(55); 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 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: 55 44 33 22 11
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.insertBegin(11); Obj.insertBegin(22); Obj.insertBegin(33); Obj.insertBegin(44); Obj.insertBegin(55); 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 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 : 55 44 33 22 11
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