循環鍊錶與鍊錶相比略有不同。在循環鍊錶中,最後一個節點指向第一個節點,這樣就完成了一個完整的節點循環。也就是說,我們也可以說,在這個鍊錶中,最後一個元素不為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中文網其他相關文章!