m0_62282054 2022-09-23 14:33 采纳率: 89.6%
浏览 19
已结题

为什么两段代码,只改了一点命名为什么一个可以输出数据,一个却不行



package DateStructuresHomework;

public class S2 {
    public int no;
    public S2 next;

    public S2(int no) {
        this.no = no;
    }
    @Override
    public String toString() {
        return ""+no;

    }
}
class SingleLinkedList3
{
    private S2 head = new S2(0);
    public S2 getHead() {
        return head;
    }

    public void add(S2 S2) {
        //因为head节点不能动,因此我们需要一个辅助遍历 temp
        S2 temp = head;
        //遍历链表,找到最后
        while(true) {
            //找到链表的最后
            if(temp.next == null) {
                break;
            }
            //如果没有找到最后, 将将temp后移
            temp = temp.next;
        }
        //当退出while循环时,temp就指向了链表的最后
        //将最后这个节点的next 指向 新的节点
        temp.next = S2;
    }
    public void list() {
        //判断链表是否为空
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //因为头节点,不能动,因此我们需要一个辅助变量来遍历
        S2 temp = head.next;
        while(true) {
            //判断是否到链表最后
            if(temp == null) {
                break;
            }
            //输出节点的信息
            System.out.println(temp);
            //将temp后移, 一定小心
            temp = temp.next;
            //temp=temp.next是使temp指向下一结点。
            //temp.next=temp是使temp本身的next指针指向自己。
        }
    }


}
class TEST2{
    public static void main(String[] args) {
        S2 a = new S2(1);
        SingleLinkedList3 singleLinkedList = new SingleLinkedList3();
        singleLinkedList.add(a);
        singleLinkedList.list();
    }
}
package DateStructuresHomework;

public class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
    @Override
    public String toString()
    {
        return ""+val;
    }
}


class SingleLinkedList
{
    private Node head=new Node(0);
    public Node getHead(){
        return head;
    }

    public void insert(Node Node)
    {
        Node temp=head;
        while (true)
        {
            if (temp.next==null)
            {
                    break;
            }
            temp=temp.next;

        }
        temp.next=Node;


        }

        public void display()
        {
            if (head.next==null)
            {
                System.out.println("空");
                return;
            }

            Node temp=head.next;
            while (true)
            {
                if (temp==null)
                    break;
            }
            System.out.println(temp);
            temp=temp.next;
        }





    }






    class Test5 {
        public static void main(String[] args) {
            Node node1 = new Node(1);
            SingleLinkedList singleLinkedList = new SingleLinkedList();
            singleLinkedList.insert(node1);
            singleLinkedList.display();


        }
    }



  • 写回答

2条回答 默认 最新

  • 燕少༒江湖 2022-09-23 14:44
    关注
     
    public class Node {
        public int val;
        public Node next;
     
        public Node(int val) {
            this.val = val;
        }
        @Override
        public String toString()
        {
            return ""+val;
        }
    }
     
     
    class SingleLinkedList
    {
        private Node head=new Node(0);
        public Node getHead(){
            return head;
        }
     
        public void insert(Node Node)
        {
            Node temp=head;
            while (true)
            {
                if (temp.next==null)
                {
                        break;
                }
                temp=temp.next;
     
            }
            temp.next=Node;
     
     
            }
     
            public void display()
            {
                if (head.next==null)
                {
                    System.out.println("空");
                    return;
                }
     
                Node temp=head.next;
                while (true)
                {
                    if (temp == null){
                        break;
                    } else {
                        System.out.println(temp);
                    }
                    temp=temp.next;
                }
    
            }
        }
     
     
     
     
     
     
        class Test5 {
            public static void main(String[] args) {
                Node node1 = new Node(1);
                SingleLinkedList singleLinkedList = new SingleLinkedList();
                singleLinkedList.insert(node1);
                singleLinkedList.display();
     
     
            }
        }
     
     
     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月1日
  • 已采纳回答 9月23日
  • 修改了问题 9月23日
  • 修改了问题 9月23日
  • 展开全部

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线