与链表相比,循环链表略有不同。在循环链表中,最后一个节点指向第一个节点,从而完成一整圈节点。换句话说,我们也可以说在这个链表中,最后一个元素不是空的。在这种类型的链表中,任何节点都可以作为起点。这意味着可以遍历整个列表,即使我们从任何节点开始。当最后一个节点指向第一个节点时,在循环链表中执行排队和出列操作非常容易。在本文中,我们将通过示例了解循环链表。
Java中循环链表的语法:
以下是语法:
节点类定义语法-
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();
}
}
Java中循环链表的处理
正如我们前面读到的,循环链表只是一个列表,其中包含一组节点,其中最后一个节点指向第一个节点。下图解释了这一点。这里节点1是头部节点,节点4是最后一个节点。这里我们可以看到节点1指向节点2,然后节点2指向节点3,节点3指向节点4,最后节点4指向节点1。
- 首先,我们必须定义一个节点类,它将是特定列表中的一个节点。它将有两个属性数据,下一个属性数据将指向即将到来的节点。
- 必须定义另一个类,该类将创建具有两个节点的循环链表,其中一个节点为
head
,另一个为tail
。其中有两个方法,分别是add()
和display()
。 add()
方法将节点添加到列表中。该方法首先检查头部是否为空,然后插入节点作为头部。- 现在,头部和尾部都指向新添加的节点。
- 如果
head
不为null
,那么新节点将充当新的尾部,其尾部将指向head
,因为此列表是一个循环链接列表。
Java中的循环链表示例
以下是java中循环链表的示例:
示例1
在下面的示例中,将显示一个基于字符串的循环列表。首先,我们定义了一个节点类。然后我们定义了另一个类来创建循环链表,这个类被添加并显示在循环链表中。
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();
}
}
输出:
示例2
在下面的示例中,将显示一个基于字符串的循环列表。首先,我们定义了一个节点类Coursename
。然后我们定义了另一个类来创建循环链表,这个类被添加并显示在循环链表中。
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();
}
}
输出:
小结
在本文的基础上,我们理解了Java中循环链表的概念,以及它与链表的区别。我们完成了循环链表的工作,以及演示的示例。这些例子将帮助初学者非常容易地理解循环链表的概念。
原文链接:https://javakk.com/2552.html?utm_source=tuicool&utm_medium=referral