【问题标题】:Stuck inside an infinite loop when adding a node in linked list在链表中添加节点时陷入无限循环
【发布时间】:2020-02-13 21:36:57
【问题描述】:

我一直在努力弄清楚为什么这段代码会陷入无限循环。背后的故事是我找到了解决方案,我将构造函数更改为分配 head 等于 null 并修复它。

我想知道为什么这段代码不起作用。

当我添加不同的节点时,它正在工作。代码按预期工作。

添加相同节点时会出现问题。

public class Main {
    public static void main(String[] args) {
        Node one = new Node(1);
        Node two = new Node(2);

        LinkedList list = new LinkedList(one);
        // this gives error, if i add the same nodes
        list.add(two);
        list.add(two);

        System.out.println("Printing out:\n" + list.toString() +"\n");
    }
}


public class LinkedList {
    Node head;
    int length = 0;
    public boolean isEmpty() {
        return (head==null);
    }

    public String toString() {
        Node current = head;
        String string = "Head: ";

        while(current.next != null) {
            string += current.toString() + " --> ";
            current = current.next;
        }
        string += current.toString() + " --> " + current.next;
        return string;
    }

    public LinkedList(Node node) {
        // if were to set head to null and no arg, it works fine
        head = node;
        length = 1;
    }

    public void add(Node node) {
        if(isEmpty()) {
            System.out.println("Empty list, adding node...");
            head = new Node(node.data); ++length;
            return;
        }
        else {

        Node current = head;
        while(current.next != null) {
            current = current.next;
        }
        //current.next = new Node(node.data);
        current.next = node;
        ++length;
        return;
        }
    }

错误是,它永远不会终止,因此我认为它永远在循环。

【问题讨论】:

    标签: java methods linked-list infinite-loop singly-linked-list


    【解决方案1】:

    我认为在您的 add(Node node) 代码中。当您添加两次相同的节点时,它会将下一个指向自身。因此这将是无限循环。

    【讨论】:

    • 我不确定这是否正确,请解释一下。当我进行更改以使代码按预期工作时,它不会给我一个无限循环。
    • 其实很有趣。设置 LinkedList 类的构造函数后, -- public LinkedList() { head = null; length = 0;},代码将能够添加两次相同的节点, -- Node two = new Node(2); LinkedList 列表 = 新的 LinkedList(); list.add(二); list.add(二); ...这样可行。它将打印 2--> 2--> null。当我添加第三个相同节点时,它会再次永远循环。
    • 我认为你是对的,我只是无法理解它,但情况可能就是这样。请多解释一下:)
    • 我认为问题还是一样的。如果您添加新的构造函数,我认为这是您的新代码中发生的情况: 1. 第一次添加两个 head = new Node(node.data); 这将创建新节点作为 Head。 2.第二次你加了两个,因为它与头部是不同的对象(头部是新对象)。所以,你在这里很好 3。第三次,你加了两个。您的列表中已经存在两个对象。因此第二个和第三个元素将彼此相邻,实际上是同一个对象
    • 我想确保这不会发生。您可以将 add(Node 节点) 更改为 add(int data)。在此内部,您应该使用提供的数据新建一个节点。
    【解决方案2】:

    由于 LinkedList 类的 toString() 方法中的 while 循环,它进入了无限循环。

    您正在按条件进行验证

    while(current.next != null) { .....}
    

    在到达最后一个节点后,您不会将最后一个节点的 next 设置为 null,因此条件永远不会终止。

    要解决这个问题,您要添加节点点 node.next = null;

            current.next = node;
            node.next = null;
            ++length;
            return;
    

    它将终止并且不会进入无限循环

    【讨论】:

    • 我已经对代码进行了更改,虽然它没有继续执行永久循环,但它没有添加第二个节点。
    【解决方案3】:

    代码中的行不正确是在添加方法'current.next=node'中。尝试将其更改为“current.next=new Node(node.data)”

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 1970-01-01
      • 2013-03-17
      • 2021-02-15
      • 2022-01-23
      • 2021-01-23
      相关资源
      最近更新 更多