循环链表与链表相比略有不同。在循环链表中,最后一个节点指向第一个节点,这样就完成了一个完整的节点循环。也就是说,我们也可以说,在这个链表中,最后一个元素不为null。在这种类型的链表中,任何节点都可以作为起点。这意味着即使我们从任何节点开始,也可以遍历完整的列表。由于最后一个节点指向第一个节点,因此在循环链表中执行入队和出队操作非常容易。在本文中,我们将通过示例来了解循环链表。在本主题中,我们将学习 Java 中的循环链表。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
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(); } }
正如我们之前所读到的,循环链表只不过是一个包含节点集合的列表,其中最后一个节点指向第一个节点。下图解释了这一点。这里节点1是头节点,节点4是最后一个节点。所以,在这里我们可以看到节点 1 指向节点 2,然后节点 2 指向节点 3,节点 3 指向节点 4,最后节点 4 又指向节点 1。
下面是java中循环链表的例子:
在下面的示例中,显示了一个基于字符串的循环列表。首先,我们定义了一个节点类Course。然后我们定义了另一个类来创建循环链表,并且这个类被添加并显示在循环链表中。
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(); } }
输出:
在下面的示例中,显示了一个基于字符串的循环列表。首先,我们定义了一个节点类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中循环链表的概念以及它与链表的区别。我们了解了循环链表的工作原理以及演示的示例。这些例子可以帮助初学者很容易地理解循环链表的概念。
以上是java中的循环链表的详细内容。更多信息请关注PHP中文网其他相关文章!