Results 1 to 9 of 9
Thread: Linked List help
- 12-21-2007, 03:00 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
Linked List help
The codes is extracted from "Java software solutions 5th edition, pg 648-649"public class MagazineList
{
public MagazineNode list;
public MagazineList()
{
list = null;
}
public void add (Magazine mag)
{
MagazineNode node = new MagazineNode (mag);
MagazineNode current;
if (list == null)
{
list = node;
}
else
{
current = list;
while (current.next != null)
{
current = current.next;
}
current.next = node;
}
}
private class MagazineNode
{
public Magazine magazine;
public MagazineNode next;
public MagazineNode (Magazine mag)
{
magazine = mag;
next = null;
}
}
}
I wonder why the in "add" method, the list is not set to any value anymore, except at the first time "if(list == null)", there the "list = node";
However, the list seem to be growing with each "node" coming in, anyone can explain this for me? Thanks very much.
- 12-21-2007, 03:06 PM #2
This bit of code moves the current pointer to the end of the list (current.next = null). Then it sets the last node's next pointer to the node being added. (current.next = node;Java Code:while (current.next != null) { current = current.next; } current.next = node; }
- 12-21-2007, 03:09 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
Hi there, thanks for your reply. I know that the pointer is being set to the last one, but I wonder how it returns the "current" value back to "list"?
I mean, the codes does not show any return back to the "list", but the "list" is growing each time a magazine is added.
- 12-21-2007, 03:12 PM #4
There doesn't need to be a return to the list. I think of it like this: the list is never actually altered....only the nodes that make up the list. So by setting the new node equal to the last nodes next pointer, you are adding it to the end of the list.
- 12-21-2007, 03:35 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
ShoeNinja, I try to understand that "list" is not altered.
Let's say the first magazine is "A", and the code
list = node;
so list.magazine = "A";
list.next = null;
After that, every time a new magazine is added consequently, the code
current = list;
will take place.
That means, current is reseted to default "list" value, having current.next as null.
That will make while (current.next != null) never run!
That's my understanding from code, please correct me. Sorry my brain is not turning smoothly :confused:
- 12-21-2007, 04:13 PM #6
Maybe it will help if you line up your brackets.
The first time through, list.next does not get set to null.Java Code:if (list == null){ list = node; } else{ current = list; while (current.next != null){ current = current.next; } current.next = node; }
- 12-21-2007, 05:03 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
- 12-21-2007, 05:30 PM #8
Your 'list' variable is set the the first node in the list. Maybe change the variable name to avoid some confusion?
The first node's next value is set to null the first time through. So when you come in the second time and hit the else statement, the while does not need to fire because it is already at the end ('list'). Now on the third add (there are already 2 nodes in the list), 'current' is set to the beginning ('list'). So current.next points to the second node causing the while to fire. The while fires setting current equal to the second node which has a next value of null. This stops the while loop and the new node is set to the second nodes next value, "linking" it.
I wish I could draw you a picture. It would be a lot easier.
- 12-22-2007, 03:15 AM #9
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
I understand what you say. What I don't understand is, current is initialised to list everytime the else statement run, causing current is actually just a list (with first magazine only). How can the current having all other magazines as well since current = list?
I think maybe the code is related to pointer? The node is pointing to each other in memory location therefore no matter how the current is set to list, the node pointer still there, pointing each other with reference to the magazine.
Please correct me. Thank very much ShoeNinja:)
Similar Threads
-
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM -
linked list nodes all refernce same item.
By yllawwally in forum New To JavaReplies: 0Last Post: 12-18-2007, 08:45 PM -
going from vectors to linked list?
By cbrown08 in forum New To JavaReplies: 3Last Post: 12-01-2007, 12:55 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks