鱼C论坛

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

用Java创建链表出错

[复制链接]
发表于 2023-10-7 09:37:56 | 显示全部楼层 |阅读模式
20鱼币
  1. public class Test1 {
  2.     public static void main ( String [] args) {
  3.             Test1 test1 = new Test1(); //创建一个Test1对象
  4.             MyList myList = test1.new MyList(); //用Test1对象来创建MyList对象
  5.             Node node1 = myList.new Node(10); //用MyList对象来创建Node对象
  6.             Node node2 = myList.new Node(4);
  7.             Node node3 = myList.new Node(15);
  8.             Node node4 = myList.new Node(20);
  9.             Node node5 = myList.new Node(22);
  10.             myList.add(node1); //将结点添加到链表中
  11.             myList.add(node2);
  12.             myList.add(node3);
  13.             myList.add(node4);
  14.             myList.add(node5);
  15.             myList.display(); //显示链表中的所有数据
  16.             myList.remove(2); //删除第三个结点
  17.             myList.display(); //显示删除后的链表
  18.             System.out.println(myList.getAt(1).data); //打印第二个结点的数据
  19.     }
  20.   
  21.   class MyList{ //定义为非静态的内部类
  22.       private Node firstNode; //链表的头结点
  23.       private int length; //链表的长度
  24.    
  25.       public MyList(){ //构造方法,初始化一个空链表
  26.               firstNode = null;
  27.               length = 0;
  28.       }
  29.    
  30.       public void clear(){ //清空链表
  31.               firstNode = null;
  32.               length = 0;
  33.       }
  34.    
  35.       public void add(Node d){ //在链表尾部添加一个结点
  36.               if(firstNode == null){ //如果链表为空,直接将新结点作为头结点
  37.                   firstNode = d;
  38.               }else{ //否则,遍历链表,找到最后一个结点,将新结点链接到它后面
  39.                   Node current = firstNode;
  40.                   while(current.next != null){
  41.                           current = current.next;
  42.                   }
  43.                   current.next = d;
  44.               }
  45.               length++; //链表长度加一
  46.       }
  47.    
  48.     public void remove(int i){ //删除指定位置的结点
  49.         if(i < 0 || i >= length){ //如果位置不合法,抛出异常
  50.                 throw new IndexOutOfBoundsException("Invalid index: " + i);
  51.         }else if(i == 0){ //如果位置为0,直接将头结点指向下一个结点
  52.                 firstNode = firstNode.next;
  53.         }else{ //否则,遍历链表,找到要删除的结点的前一个结点,将它的next指向要删除的结点的next
  54.                 Node previous = firstNode;
  55.         for(int j = 0; j < i - 1; j++){
  56.             previous = previous.next;
  57.         }
  58.         Node current = previous.next;
  59.         previous.next = current.next;
  60.         }
  61.         length--; //链表长度减一
  62.     }
  63.    
  64.     public Node getAt(int i){ //获取指定位置的结点
  65.         if(i < 0 || i >= length){ //如果位置不合法,抛出异常
  66.                 throw new IndexOutOfBoundsException("Invalid index: " + i);
  67.         }else{ //否则,遍历链表,找到要返回的结点,并返回它
  68.                 Node current = firstNode;
  69.         for(int j = 0; j < i; j++){
  70.             current = current.next;
  71.         }
  72.           return current;
  73.         }
  74.     }
  75.    
  76.     public void display(){ //显示链表中的所有数据
  77.         Node current = firstNode;
  78.         while(current != null){
  79.                 System.out.print(current.data + " ");
  80.         current = current.next;
  81.         }
  82.         System.out.println();
  83.     }
  84.   

  85.         class Node { //定义为非静态的内部类
  86.                  private int data; //结点存储的数据
  87.                  private Node next; //结点指向的下一个结点
  88.                     
  89.                  private Node(int d){ //构造方法,初始化一个只有数据的结点
  90.               data = d;
  91.               next = null;
  92.             }
  93.                     
  94.             private Node(int d, Node n){ //构造方法,初始化一个有数据和下一个结点的结点
  95.               data = d;
  96.               next = n;
  97.             }
  98.           }
  99.         }
  100. }
复制代码
微信图片_20231007093553.png
如图,在使用Java创建链表时出现如下错误,请问该怎么办呐

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-27 05:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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