Results 1 to 5 of 5
  1. #1
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default linked list update

    Hi, i do not understand WHY the following code works, it is totally weird for me:
    XML Code:
            name = infile.readLine(); //Read the first line
    // starting list with a string from reader
            start = new HTMLlist(name, null);
            current = start; // this one equals start
            while (true) {
                name = infile.readLine(); // Read the next line
                if (name == null) {
                    break;   // Exit if there is none
                }
                tmp = new HTMLlist(name, null); // create new node
                current.next = tmp; // attach the new node
                current = tmp; // Update the linked list ??????:confused:
    // didn't we here just substitute the value of current with tmp
                
            }
            infile.close(); // Close the file
            return start; 
    //???:confused: what the heck, start is only a node
    // tried to put start = current; return start; but it is not working
    So how the hell 'start' gets the next nodes? There is current = start at the beginning, but I don't see anything like start.next, neither start = current (current is somehow updated)
    And how is current updated at all? First we attach tmp to current(current.next = tmp) then we replace current with tmp (current = tmp), so what is updated?
    I do not understand

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    start is a reference variable and refers to the first node in the list here:
    Java Code:
    start = new HTMLlist(name, null);

    also current, another reference variable refers initally to the same node as start here:
    Java Code:
    current = start; // this one equals start

    Then, as you read through the file, create new nodes and have the tmp reference variable refer to them here:
    Java Code:
    tmp = new HTMLlist(name, null); // create new node

    You want to link the previous node with the new node, and so you make the node referred to by current assign its next reference variable to tmp.
    Java Code:
    current.next = tmp; // attach the new node

    All well and good, but here's where you're having trouble:
    Java Code:
    current = tmp; // Update the linked list
    And I think that your problem is in knowing the difference between an object and a reference variable. current is a reference variable, not an object, and so this line of code above does nothing more than have current refer to a different object, the one currently referred to by tmp and currently the most recently created node on the list. This has absolulely no effect on the node object that was previously referred to by current. This distinction between variable and object is critical.


    Here:
    Java Code:
    return start; 
    //???:confused: what the heck, start is only a node
    Your returning a reference to only a node, yes, but it's the node that's at the start of the list.

    So again, the variables are something like pointers in other languages in that they refer to objects but aren't objects themselves.

    Clear as mud? ;)

  3. #3
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default update

    Ok, but why not just: (btw, not working)

    start.next = tmp
    return start;

    Where it is lost?
    This is some simple (obviously not applicable) logic.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I don't get you here. Sorry.

  5. #5
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default

    declaring 'start' outside the while loop
    then inside:

    tmp = new HTMLlist(name, null) // the new node
    start.next = tmp //just adding the new node

    Why cant 'start' be updated this way and return a list of all nodes created during the loop?

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  2. Linked List help
    By alpdog14 in forum New To Java
    Replies: 3
    Last Post: 10-07-2009, 10:34 PM
  3. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  5. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •