Results 1 to 5 of 5
Thread: linked list update
- 05-22-2010, 05:12 PM #1
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
linked list update
Hi, i do not understand WHY the following code works, it is totally weird for me:
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)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
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
- 05-22-2010, 06:02 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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:
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.Java Code:current = tmp; // Update the linked list
Here:
Your returning a reference to only a node, yes, but it's the node that's at the start of the list.Java Code:return start; //???:confused: what the heck, start is only a node
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? ;)
- 05-22-2010, 08:59 PM #3
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
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.
- 05-22-2010, 09:37 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
I don't get you here. Sorry.
- 05-22-2010, 10:04 PM #5
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Linked List help
By alpdog14 in forum New To JavaReplies: 3Last Post: 10-07-2009, 09:34 PM -
Linked list
By rosh72851 in forum New To JavaReplies: 1Last Post: 02-05-2009, 07:21 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks