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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of Circular linked list in Java:
Below is the syntax:
public class Course{ float marksscored; Course comingup; public Course(float marksscored) { this.marksscored = marksscored; } }
public Course evaluated = null; public Course notevaluated = null;
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 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(); } }
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.
Given below are the examples of circular linked list in java:
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:
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:
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.
The above is the detailed content of Circular linked list in java. For more information, please follow other related articles on the PHP Chinese website!