简体   繁体   中英

Implementing my own LinkedList, insertion of a new element to the Tail doesn't work correctly

I`m trying to implement my own linkedlist and I tackled with this question.

This is add method implementation for linkedlist , supposed to add specified elements to the end of the list.

Below you see a code I found and that works, and you see my code which only shows last 2 elements added no matter how many you add to the list.

The only difference 2 codes have is that he stores rootNode ( head ) inside currentNode and does his traversing using currentNode . I directly used rootNode to do the same. Can someone explain my what is wrong with my code ?

This is his code that works:

public void add (E val) {       
    Node newNode = new Node(val, null); 
    Node currentNode = rootNode;
    
    if (rootNode == null) {          
        rootNode = newNode;
    } else {                         
        while (currentNode.nextNode != null) {
            currentNode = currentNode.nextNode;   
        } 
        currentNode.setNextNode(newNode);
    }
}

And this is my code that only shows last 2 elements added :

public void add (E val) {       
    Node newNode = new Node(val, null);
    
    if (rootNode == null) {          
        rootNode = newNode;
    } else {                        
        while (rootNode.nextNode != null) {
            rootNode = rootNode.nextNode;   
        } 
        rootNode.setNextNode(newNode);
    }
}

That happens because you're resigning the rootNode while iterating.

By making this, you are erasing all previous state of the list, which you can access only via root. That's way the only node that remain in the list are root and its next node.

In order to iterate over the list, you should introduce the local variable like in the first version. There's no other way around.

And I suggest to simplify the method a bit by returning after checking whether the root is null , there will be no in nesting the loop into the additional block of curly braces:

public void add (E val) {
    Node newNode = new Node(val,null);
    
    if(rootNode == null) {
        rootNode = newNode;
        return;
    }

    Node currentNode = rootNode;
    while (rootNode.nextNode != null) {
        rootNode = rootNode.nextNode;
    } 
    rootNode.setNextNode(newNode);
}

Because you are changing root node, after add method ends, you have only the root node and the next node of it, do not change root node if it is not null. For instance, you call add Method for nodes A, B, and C. For the first time root is A, then when you call add(B), as the root node is not null already, loop will not work in the else statement and the next of A will be B, then when you call add(C), while loop will work one iteration and B will become root node(you lost A) and the next of B will be C node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM