Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-21-2007, 05:00 PM
Member
 
Join Date: Dec 2007
Posts: 5
neobie is on a distinguished road
Linked List help
Quote:
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;
}
}
}
The codes is extracted from "Java software solutions 5th edition, pg 648-649"

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-21-2007, 05:06 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Code:
while (current.next != null) { current = current.next; } current.next = node; }
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;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-21-2007, 05:09 PM
Member
 
Join Date: Dec 2007
Posts: 5
neobie is on a distinguished road
Quote:
Originally Posted by ShoeNinja View Post
Code:
while (current.next != null) { current = current.next; } current.next = node; }
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;
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-21-2007, 05:12 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-21-2007, 05:35 PM
Member
 
Join Date: Dec 2007
Posts: 5
neobie is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-21-2007, 06:13 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Maybe it will help if you line up your brackets.

Code:
if (list == null){ list = node; } else{ current = list; while (current.next != null){ current = current.next; } current.next = node; }
The first time through, list.next does not get set to null.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-21-2007, 07:03 PM
Member
 
Join Date: Dec 2007
Posts: 5
neobie is on a distinguished road
Quote:
Originally Posted by ShoeNinja View Post
Maybe it will help if you line up your brackets.

Code:
if (list == null){ list = node; } else{ current = list; while (current.next != null){ current = current.next; } current.next = node; }
The first time through, list.next does not get set to null.
private class MagazineNode
{
public Magazine magazine;
public MagazineNode next;

public MagazineNode (Magazine mag)
{
magazine = mag;
next = null;
}
}

It show that the node.next is a null ?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-21-2007, 07:30 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-22-2007, 05:15 AM
Member
 
Join Date: Dec 2007
Posts: 5
neobie is on a distinguished road
Quote:
Originally Posted by ShoeNinja View Post
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.
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Circular Double Linked List theonly Advanced Java 1 04-04-2008 09:18 AM
linked list nodes all refernce same item. yllawwally New To Java 0 12-18-2007 10:45 PM
going from vectors to linked list? cbrown08 New To Java 3 12-01-2007 02:55 AM
Linked List rnavarro9 New To Java 0 11-29-2007 05:42 AM
Help with linked list trill New To Java 1 08-07-2007 09:29 AM


All times are GMT +3. The time now is 01:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org