鱼C论坛

 找回密码
 立即注册
查看: 1852|回复: 2

[已解决]求一个循环列表的代码,每行带注释的

[复制链接]
发表于 2023-10-31 22:55:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求一个用Java实现的循环列表的代码,每行带注释的
最佳答案
2023-10-31 22:56:24
当我们需要实现一个循环列表时,可以使用链表的方式来实现。下面是一个用Java实现循环列表的示例代码,每行都有详细的注释解释:

  1. // 定义循环列表节点类
  2. class Node<T> {
  3.     T data; // 节点数据
  4.     Node<T> next; // 下一个节点的引用

  5.     // 构造函数
  6.     public Node(T data) {
  7.         this.data = data;
  8.         this.next = null;
  9.     }
  10. }

  11. // 定义循环列表类
  12. class CircularList<T> {
  13.     private Node<T> head; // 头节点

  14.     // 构造函数
  15.     public CircularList() {
  16.         this.head = null;
  17.     }

  18.     // 添加节点到循环列表
  19.     public void add(T data) {
  20.         Node<T> newNode = new Node<>(data);

  21.         // 如果列表为空,则将头节点指向新节点,并使新节点的next指向自身,形成循环
  22.         if (head == null) {
  23.             head = newNode;
  24.             head.next = head;
  25.         } else {
  26.             // 找到最后一个节点
  27.             Node<T> last = head;
  28.             while (last.next != head) {
  29.                 last = last.next;
  30.             }

  31.             // 将最后一个节点的next指向新节点,并使新节点的next指向头节点
  32.             last.next = newNode;
  33.             newNode.next = head;
  34.         }
  35.     }

  36.     // 遍历循环列表并打印节点数据
  37.     public void print() {
  38.         if (head == null) {
  39.             System.out.println("循环列表为空");
  40.             return;
  41.         }

  42.         Node<T> current = head;

  43.         // 遍历循环列表,直到回到头节点
  44.         do {
  45.             System.out.println(current.data);
  46.             current = current.next;
  47.         } while (current != head);
  48.     }
  49. }

  50. // 测试循环列表
  51. public class Main {
  52.     public static void main(String[] args) {
  53.         CircularList<Integer> circularList = new CircularList<>();

  54.         // 添加节点到循环列表
  55.         circularList.add(1);
  56.         circularList.add(2);
  57.         circularList.add(3);
  58.         circularList.add(4);

  59.         // 打印循环列表节点数据
  60.         circularList.print();
  61.     }
  62. }
复制代码


这段代码定义了一个循环列表类 CircularList ,其中包含了添加节点和打印节点的方法。在 main 函数中,我们创建了一个循环列表对象 circularList ,并添加了一些节点。最后,调用 print 方法打印循环列表中的节点数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-31 22:56:24 | 显示全部楼层    本楼为最佳答案   
当我们需要实现一个循环列表时,可以使用链表的方式来实现。下面是一个用Java实现循环列表的示例代码,每行都有详细的注释解释:

  1. // 定义循环列表节点类
  2. class Node<T> {
  3.     T data; // 节点数据
  4.     Node<T> next; // 下一个节点的引用

  5.     // 构造函数
  6.     public Node(T data) {
  7.         this.data = data;
  8.         this.next = null;
  9.     }
  10. }

  11. // 定义循环列表类
  12. class CircularList<T> {
  13.     private Node<T> head; // 头节点

  14.     // 构造函数
  15.     public CircularList() {
  16.         this.head = null;
  17.     }

  18.     // 添加节点到循环列表
  19.     public void add(T data) {
  20.         Node<T> newNode = new Node<>(data);

  21.         // 如果列表为空,则将头节点指向新节点,并使新节点的next指向自身,形成循环
  22.         if (head == null) {
  23.             head = newNode;
  24.             head.next = head;
  25.         } else {
  26.             // 找到最后一个节点
  27.             Node<T> last = head;
  28.             while (last.next != head) {
  29.                 last = last.next;
  30.             }

  31.             // 将最后一个节点的next指向新节点,并使新节点的next指向头节点
  32.             last.next = newNode;
  33.             newNode.next = head;
  34.         }
  35.     }

  36.     // 遍历循环列表并打印节点数据
  37.     public void print() {
  38.         if (head == null) {
  39.             System.out.println("循环列表为空");
  40.             return;
  41.         }

  42.         Node<T> current = head;

  43.         // 遍历循环列表,直到回到头节点
  44.         do {
  45.             System.out.println(current.data);
  46.             current = current.next;
  47.         } while (current != head);
  48.     }
  49. }

  50. // 测试循环列表
  51. public class Main {
  52.     public static void main(String[] args) {
  53.         CircularList<Integer> circularList = new CircularList<>();

  54.         // 添加节点到循环列表
  55.         circularList.add(1);
  56.         circularList.add(2);
  57.         circularList.add(3);
  58.         circularList.add(4);

  59.         // 打印循环列表节点数据
  60.         circularList.print();
  61.     }
  62. }
复制代码


这段代码定义了一个循环列表类 CircularList ,其中包含了添加节点和打印节点的方法。在 main 函数中,我们创建了一个循环列表对象 circularList ,并添加了一些节点。最后,调用 print 方法打印循环列表中的节点数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-6-19 17:59:14 | 显示全部楼层
@fishc
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 21:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表