[英]Linked List infinite loop on first Node
Printing the value of the node during the loop i discovered it infinite loops always on the first node when, for example, searching for a node which is not in the list(if its on the list it works). 在循环期间打印节点的值时,我发现它总是在第一个节点上无限循环,例如,当搜索不在列表中的节点时(如果它在列表中有效)。 Im using a node to point the first element as well as the last one.
我使用节点来指向第一个元素以及最后一个元素。
This is my search method 这是我的搜索方法
public LNode search(int data){
LNode currNode = head;
while(data != currNode.value){
if(currNode.next == null)
return null;
else{
currNode = currNode.next;
System.out.println("currnode " + currNode.value);
}
}
return currNode;
}
the insert method which always inserts elements at the beginning 总是在开始处插入元素的insert方法
public void insert( LNode aNode ) {
if(head==null){
head = aNode;
tail = aNode;
size++;
}
aNode.next = head;
head = aNode;
size++;
}
It is like the first entered element has a next node which is not set to null, but i dont know why it isnt null 就像第一个输入的元素有一个未设置为null的下一个节点,但我不知道为什么它不是null
I think in 我认为
if(head==null){
head = aNode;
tail = aNode;
size++;
}
is a return missing. 是失踪的回报。 you make a loop with head and increase size by 2 with one insert.
您可以制作一个带头的循环,并通过一个插入将尺寸增加2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.