Reputation: 337
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
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
Upvotes: 0
Views: 703
Reputation: 4914
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.
Upvotes: 2