Results 1 to 8 of 8
  1. #1
    nomss is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Java Linked List

    Java Code:
    public class GuestListNode
    {
      private String name;
      private int numGuests;
      private GuestListNode next;
      
      public GuestListNode(String name, int numGuests)
      {
        this.name = name;
        this.numGuests = numGuests;
      }
      
      public String getName()
      {
        return name;
      }
      
      public int getNumGuests()
      {
        return numGuests;
      }
      
      public void setNumGuests(int n)
      {
        numGuests = n;
      }
      
      public GuestListNode getNext()
      {
        return next;
      }
      
      public void setNext(GuestListNode next)
      {
        this.next = next;
      }
    }
    
    public class GuestList
    {
      private GuestListNode head;
      
      public GuestList()
      {
        head = null;
      }
      
      public void addGuest(String name, int numGuests)
      {
        head = new GuestListNode(name, numGuests);
      }
    
    
      
      public String toString()
      {
        while(head != null)
        {
          System.out.println(head.getName() + " " + head.getNumGuests());
          head = head.getNext();
        }
        String s = "";
        return s;
      }  
      
      
      public static void main(String[] args)
      {
        GuestList gl = new GuestList();
        gl.addGuest("Calypso", 1);
        gl.addGuest("Odysseus", 10);
        gl.addGuest("Penelope", 117);
        gl.addGuest("Athena", 2);
        gl.addGuest("Helios", 1);
        gl.addGuest("Cyclops", 5);
        //gl.removeGuest("Cyclops");
        System.out.print(gl);
      } 
    }
    Right now when I hit run in GuestList, the result it:
    Cyclops 5
    How do I make it so all the guests are printed out.

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    I don't know for sure, but maybe you should use an iterator and print each line separate.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nomss View Post
    Right now when I hit run in GuestList, the result it:
    Cyclops 5
    How do I make it so all the guests are printed out.
    First correct your 'addGuest( ... )' method; as it is now it simply creates a node, makes 'head' refer to it and forgets about all the other nodes already in the list. I guess the assignment doesn't allow you to use a standard List?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    the problem is with your addGuest() method, not your toString.

    I would alter it so you pass the constuctor the current head node.

    Java Code:
    public void addGuest(String name, int numGuests) {
        head = new GuestListNode(head, name, numGuests);
    }
    Also, toString() methods should generally not use a println anywhere - thats not their purpose.
    It should do something like this:
    Java Code:
    public String toString() {
       GuestListNode temp = head;
       String s = "";
        while(temp != null){
            s += temp.getName() + " " + temp.getNumGuests() + "\n";
            temp = temp.getNext();
        }
        return s;
    }

  5. #5
    nomss is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by berkeleybross View Post
    the problem is with your addGuest() method, not your toString.
    I understand that when addGuest() is called it only makes new nodes but doesn't point to the next new node that is created. I don't really know how to go about doing this.
    I would alter it so you pass the constuctor the current head node.

    Java Code:
    public void addGuest(String name, int numGuests) {
        head = new GuestListNode(head, name, numGuests);
    }
    Can't do this as you can see in the link I've posted, the constructro from GuestListNode does not take 3 arguments.

    Also, toString() methods should generally not use a println anywhere - thats not their purpose.
    It should do something like this:
    Java Code:
    public String toString() {
       GuestListNode temp = head;
       String s = "";
        while(temp != null){
            s += temp.getName() + " " + temp.getNumGuests() + "\n";
            temp = temp.getNext();
        }
        return s;
    }

    https://docs.google.com/viewer?a=v&p...MjVmMTcy&hl=en

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    @OP: please quote properly; I can't quote your last message now because you've added you reply to yet another quote.

    About the problem: your assignment also says that a node should be inserted at its correct alphabetical position in the list. Your constructor takes two arguments but you can't set the head of the list to just a single new node.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    nomss is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    @OP: please quote properly; I can't quote your last message now because you've added you reply to yet another quote.

    About the problem: your assignment also says that a node should be inserted at its correct alphabetical position in the list. Your constructor takes two arguments but you can't set the head of the list to just a single new node.

    kind regards,

    Jos
    Well I thought I'd tackle one problem at a time. I thought I first I could have the nodes be printed then work on arranging them properly.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nomss View Post
    Well I thought I'd tackle one problem at a time. I thought I first I could have the nodes be printed then work on arranging them properly.
    You can only print the contents of the Nodes if you have put them in that list; you haven't put them in that list so they are not printed. It's not a catch 22, you have to insert the Nodes in your list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Linked List
    By nomss in forum New To Java
    Replies: 8
    Last Post: 12-09-2010, 04:28 PM
  2. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  3. Linked List in java
    By zaydz in forum New To Java
    Replies: 14
    Last Post: 06-28-2010, 12:53 PM
  4. Java double linked list
    By Clown in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 04:04 PM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Posting Permissions

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