简体   繁体   中英

Add node to end of Linked List

Having a bit of trouble adding a node to the end of my linked list. It only seems to display the very last one I added before I call my addFirst method. To me it looks like on the addLast method I'm trying to first create the node to assign it 5, then for the following numbers use a while loop to assign them to the last node on the linked list. Little stuck on why I can't get my output to display 5 and 6.

class LinkedList
{
    private class Node          
    {
    private Node link;
    private int x;
    }
    //----------------------------------
    private Node first = null;    
    //----------------------------------
    public void addFirst(int d)
    {
        Node newNode = new Node();
        newNode.x = d;            
        newNode.link = first;    
        first = newNode;           
    }
    //----------------------------------
    public void addLast(int d)
    {
        first = new Node();
        if (first == null)
        {
            first = first.link;
        }
        Node newLast = new Node();
        while (first.link != null)
        {
            first = first.link;
        }
        newLast.x = d;
        first.link = newLast;
        first = newLast;
    }
    //----------------------------------
    public void traverse()
    {
        Node p = first;
        while (p != null)           
        {
            System.out.println(p.x);  
            p = p.link;               
        }
    }
}
//==============================================
class test123
{
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.addLast(5);
        list.addLast(6);
        list.addLast(7);
        list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        System.out.println("Numbers on list");
        list.traverse();
    }
}

I've also tried creating a last Node and in the traverse method using a separate loop to traverse the last node. I end up with the same output!

   public void addLast(int d)
   {
       Node newLast = new Node();
       while (last.link != null)
       {
          last = newLast.link;
       }    
       newLast.x = d; 
       newLast.link = last;
       last = newLast;       
   }

The logic of your addLast method was wrong. Your method was reassigning first with every call the logic falls apart from that point forward. This method will create the Node for last and if the list is empty simply assign first to the new node last . If first is not null it will traverse the list until it finds a Node with a null link and make the assignment for that Nodes link.

public void addLast(int d) {
    Node last = new Node();
    last.x = d;

    Node node = first;
    if (first == null) {
        first = last;
    } else {
        while (node.link != null) {
            node = node.link;
        }
        node.link = last;
    }
}

Your addLast() method displays the value of the last node because every time you append a node to the end of your list, you are overwriting the reference to "first". You are also doing this when you assign a new reference to first in the following line:

first = new Node();

Try the following:

public void addLast(int d)
{
    Node newLast = new Node();
    if (first == null)
    {
        newLast.x = d;
        first = newLast;
        return;
    }
    Node curr = first;
    while (curr.link != null)
    {
        curr = curr.link;
    }
    newLast.x = d;
    curr.link = newLast;
}

The method creates a new node and it is added to the end of the list after checking two conditions: 1.) If first is null: in this case the list is empty and the first node should be initialized to the new node you are creating, then it will return (or you could do an if-else). 2.) If first is not null: If the list is not empty you'll loop through your list until you end up with a reference to the last node, after which you will set its next node to the one you just created.

If you wanted to keep track of the tail of your list called "last", like you mentioned above, then add:

last = newLast

at the end of your method. Hope this helps!

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