Updated April 17, 2023
Introduction to Circular linked list in java
A circular linked list has a slight difference if compared to a linked list. In a circular linked list, the last node points towards the first node, which completes a full circle of nodes. In other words, we can also say that in this linked list, the last element is not null. In this type of linked list, any of the nodes can act as a starting point. This means that the complete list can be traversed, even if we start from any node. Enqueue and Dequeue operations are very easy to perform in the circular linked lists as the last node points towards the first node. In this article, we will understand the circular linked list with examples. In this topic, we are going to learn about the Circular linked list in java.
Syntax of Circular linked list in Java:
Below is the syntax:
- Node Class defining syntax –
public class Course{
float marksscored;
Course comingup;
public Course(float marksscored) {
this.marksscored = marksscored;
}
}
- Creating Circular List components syntax –
public Course evaluated = null;
public Course notevaluated = null;
- Adding Circular List component in circular list syntax –
public void continued(float marksscored){
Course newCourse = new Course(marksscored);
if(evaluated == null) {
evaluated = newCourse;
notevaluated = newCourse;
newCourse.comingup = evaluated;
}
else {
notevaluated.comingup = newCourse;
notevaluated = newCourse;
notevaluated.comingup = evaluated;
}
}
- Displaying Circular List Components syntax –
public void show() {
Course current = evaluated;
if(evaluated == null) {
System.out.println("Results are underevaluation");
}
else {
System.out.println("The Marks Scored are as follows: ");
do{
System.out.print(" \n"+ current.marksscored);
current = current.comingup;
}while(current != evaluated);
System.out.println();
}
}
public static void main(String[] args) {
EDUCBA marks = new EDUCBA();
marks.continued(100);
marks.continued(230);
marks.continued(349);
marks.continued(423);
marks.show();
}
}
Working of Circular Linked List in Java
As we read earlier that the circular linked list is nothing but a list that has a collection of nodes where the last nodes point towards the first node. The below diagram explains this. Here Node 1 is the head node, and Node 4 is the last node. So, here we can see that Node 1 points to Node 2, then Node 2 points to Node 3 and Node 3 points to Node 4, and at the end, Node 4 points back to Node 1.
- Firstly, we will have to define a node class which will be a node in the particular list. It will have two properties data and next which will be pointing towards the upcoming node.
- Another class has to be defined, which will create the circular linked list with two nodes, where one node would be head, and another would be tail. There would be two methods in it which would be add() and display().
- The add() method would add the node to the list. Firstly, this method would check if the head is null, and then the node would be inserted as the head.
- Now both the head and tail would be pointing towards the newly added node.
- In case if the head isn’t null, then the new node will act as the new tail, and its tail would be pointing towards the head as this list is a circular linked list.
Examples of Circular linked list in Java
Given below are the examples of circular linked list in java:
Example #1
In the example below, a string-based circular list is displayed. Firstly, we have defined a node class Course. Then we have defined another class to create a circular linked list, and this class is added and displayed in the circular linked list.
public class EDUCBA {
public class Course{
float marksscored;
Course comingup;
public Course(float marksscored) {
this.marksscored = marksscored;
}
}
public void continued(float marksscored){
Course newCourse = new Course(marksscored);
if(evaluated == null) {
evaluated = newCourse;
notevaluated = newCourse;
newCourse.comingup = evaluated;
}
else {
notevaluated.comingup = newCourse;
notevaluated = newCourse;
notevaluated.comingup = evaluated;
}
}
public Course evaluated = null;
public Course notevaluated = null;
public void show() {
Course current = evaluated;
if(evaluated == null) {
System.out.println("Results are underevaluation");
}
else {
System.out.println("The Marks Scored are as follows: ");
do{
System.out.print(" \n"+ current.marksscored);
current = current.comingup;
}while(current != evaluated);
System.out.println();
}
}
public static void main(String[] args) {
EDUCBA marks = new EDUCBA();
marks.continued(100);
marks.continued(230);
marks.continued(349);
marks.continued(423);
marks.show();
}
}
Output:
Example #2
In the example below, a string-based circular list is displayed. Firstly, we have defined a node class Coursename. Then we have defined another class to create a circular linked list, and this class is added and displayed in the circular linked list.
public class EDUCBA {
public class Coursename{
String name;
Coursename comingup;
public Coursename(String name) {
this.name = name;
}
}
public void continued(String name){
Coursename newCoursename = new Coursename(name);
if(evaluated == null) {
evaluated = newCoursename;
notevaluated = newCoursename;
newCoursename.comingup = evaluated;
}
else {
notevaluated.comingup = newCoursename;
notevaluated = newCoursename;
notevaluated.comingup = evaluated;
}
}
public Coursename evaluated = null;
public Coursename notevaluated = null;
public void show() {
Coursename current = evaluated;
if(evaluated == null) {
System.out.println("No desired course found");
}
else {
System.out.println("The requested courses are as follow: ");
do{
System.out.print(" \n"+ current.name);
current = current.comingup;
}while(current != evaluated);
System.out.println();
}
}
public static void main(String[] args) {
EDUCBA names = new EDUCBA();
names.continued("Course 1: Data Science");
names.continued("Course 2: Finance");
names.continued("Course 3: React Native");
names.continued("Course 4: React");
names.continued("Course 5: Excel");
names.continued("Course 6: Java");
names.continued("Course 7: Lua");
names.continued("Course 8: TypeScript");
names.continued("For more Information");
names.continued("Feel free to visit us on www.EDUCBA.com");
names.show();
}
}
Output:
Conclusion
On the basis of this article, we understood the concept of a circular linked list in Java and how it is different from the linked list. We went through the working of the circular linked list, and the demonstrated examples as well. The examples would help the beginners in understanding the concept of circular linked lists very easily.
Recommended Articles
This is a guide to the Circular linked list in java. Here we discuss the concept of a circular linked list in Java and how it is different from the linked list. You may also have a look at the following articles to learn more –