在链表中添加节点时陷入无限循环
Stuck inside an infinite loop when adding a node in linked list
我一直在努力弄清楚为什么这段代码会陷入死循环。背后的故事是我找到了解决方案,我更改了构造函数以分配 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;
}
}
错误是,它永远不会终止,因此我认为它会永远循环。
我认为在你的 add(Node node) 代码中。当您两次添加相同的节点时,它会将下一个指向自己。因此这将是无限循环。
由于 LinkedList 的 toString() 方法中的 while 循环,它进入无限循环 class。
您正在根据条件进行验证
while(current.next != null) { .....}
到达最后一个节点后,您没有将最后一个节点的下一个设置为空,因此条件永远不会终止。
要在添加节点的位置解决此问题 node.next = null;
current.next = node;
node.next = null;
++length;
return;
它将终止并且不会进入无限循环
您代码中的行不正确是在添加方法‘current.next=node’中。尝试将其更改为‘current.next=new Node(node.data)’
我一直在努力弄清楚为什么这段代码会陷入死循环。背后的故事是我找到了解决方案,我更改了构造函数以分配 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;
}
}
错误是,它永远不会终止,因此我认为它会永远循环。
我认为在你的 add(Node node) 代码中。当您两次添加相同的节点时,它会将下一个指向自己。因此这将是无限循环。
由于 LinkedList 的 toString() 方法中的 while 循环,它进入无限循环 class。
您正在根据条件进行验证
while(current.next != null) { .....}
到达最后一个节点后,您没有将最后一个节点的下一个设置为空,因此条件永远不会终止。
要在添加节点的位置解决此问题 node.next = null;
current.next = node;
node.next = null;
++length;
return;
它将终止并且不会进入无限循环
您代码中的行不正确是在添加方法‘current.next=node’中。尝试将其更改为‘current.next=new Node(node.data)’