Results 1 to 8 of 8
Thread: Java Linked List
- 12-10-2010, 07:17 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
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); } }
Cyclops 5
How do I make it so all the guests are printed out.
- 12-10-2010, 07:53 AM #2
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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
- 12-10-2010, 07:56 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Build a wall around Donald Trump; I'll pay for it.
- 12-10-2010, 08:04 AM #4
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); }
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; }
- 12-10-2010, 12:35 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
- 12-10-2010, 01:40 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
@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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-10-2010, 01:51 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
- 12-10-2010, 02:02 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Java Linked List
By nomss in forum New To JavaReplies: 8Last Post: 12-09-2010, 05:28 PM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-27-2010, 12:15 AM -
Linked List in java
By zaydz in forum New To JavaReplies: 14Last Post: 06-28-2010, 01:53 PM -
Java double linked list
By Clown in forum New To JavaReplies: 1Last Post: 05-07-2010, 05:04 PM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 09:53 PM
Bookmarks