Circular Linked List in Java
What is Circular Linked List in JAVA?
Circular linked list is a variation of linear data structures known as linked list. Every element in a linked list is an object and every object has two parts : the data and a reference to the next linked object(node). A linked list isn’t stored at a contagious location but the reference part of one node helps to identify the next node in the list. The referencing is done using pointers. The last node points to the address of null. Head is the first reference that points to the element of the linked list.
Why add on another data structure and increase the hazzle?
Singly linked and doubly linked lists were easy to traverse through. While you cannot go back in a singly linked list, doubly linked list took a little more time when the first element was to be reached from the last element. Circular linked lists on the other hand are very easy to traverse through and make it easier for programmers to get results.
- Circular linked lists on the other hand have eliminated the concept of null.
- The head points to the very first element and the first node points to the second and the chain continues.
- The last node instead of pointing towards null, points to the first node making the linked list complete a imaginary circle.
Implementation of Circular Linked List in JAVA
public void addNode(int value) { Node newNode = new Node(value); if (head == null) { head = newNode; } else { tail.nextNode = newNode; } tail = newNode; tail.nextNode = head; }
Operation on A Circular Linked List
- Insertion in Circular Linked List
- Deletion in Circular Linked List
Java Code for circular Linked List
import java.lang.*; public class Main { public static void main(String[] args) { Main Obj = new Main(); Obj.add(11); Obj.add(22); Obj.add(33); Obj.add(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; public void add(int element){ Node newEle = new Node(element); if(head == null) { head = newEle; tail = newEle; newEle.next = head; } else { tail.next = newEle; tail = newEle; tail.next = head; } } 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.add(11); Obj.add(22); Obj.add(33); Obj.add(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; public void add(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.prev=tail; tail = newEle; tail.next = head; head.prev=tail; } } public void print() { //print function Node current = head; if(head == null) { System.out.println("List is empty"); } else { System.out.println("Nodes of the doubly circular linked list: "); do{ System.out.print(" "+ current.element); current = current.next; }while(current != head); System.out.println(); } } }
output
Nodes of the doubly circular linked list: 11 22 33 44
Merits and Demerits of a circular linked list-
Merits –
1. Any node can be accessed with easy in a circular linked list.
2. Some coding problems involve a circular for which circular linked lists are optimal choice.
3. Queues and advanced data structures can be implemented using circular linked list.
Demerits –
1. Insertion of node in the beginning of the list is a little time consuming since it involves searching of last node to update the reference address.
2. Loop control isn’t easy.
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
Woah! Finally got a site that explains it all so clearly. I always used to get stuck while using circular lists, finally understood it perfectly!
Glad that we could help you. blush blush “D