Results 1 to 9 of 9
  1. #1
    neobie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Talking Linked List help

    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.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Java 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;

  3. #3
    neobie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    Java 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.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  5. #5
    neobie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    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:

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Maybe it will help if you line up your brackets.

    Java 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.

  7. #7
    neobie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by ShoeNinja View Post
    Maybe it will help if you line up your brackets.

    Java 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 ?

  8. #8
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  9. #9
    neobie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    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:)

Similar Threads

  1. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 06:10 PM
  2. linked list nodes all refernce same item.
    By yllawwally in forum New To Java
    Replies: 0
    Last Post: 12-18-2007, 09:45 PM
  3. going from vectors to linked list?
    By cbrown08 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 01:55 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 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
  •