Results 1 to 8 of 8
- 05-22-2009, 10:19 AM #1
[SOLVED] Problem with Linked List
Hello, I have problem with LinkedList implementation,
How can I insert a newItem on last position of linked list without adding "private Node last;" at class Node?Java Code:public class MyLinkedList { private static class Node{ private String line; private Node next; private Node(String l){ this.line = l; } } private Node ListHead; public MyLinkedList(String l){ this.ListHead = nnew Node (l); // Insert first item } public void insertLast(String l){ Node newItem = new Node(l); // and now? } }
any help will be appreciated!Ubuntu, Linux for human Beings!
- 05-22-2009, 10:53 AM #2
search through list until next==null
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-22-2009, 03:23 PM #3
I have already tried,
After "while", my object ListHead has always null, and every time when I try to add new item no List, I only have present object on that list, previous objects has already gone.Java Code:while(this.ListHead != null){ this.ListHead = this.ListHead.next; } ListHead = newItem;
is this correct way to search for null Node?
Java Code:this.ListHead = this.ListHead.next;
Ubuntu, Linux for human Beings!
- 05-22-2009, 03:34 PM #4
No, you need to use another variable to hold the current node you're looking at. Using the current method you destroy your linked structure as you go.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-22-2009, 03:48 PM #5
- 05-22-2009, 03:58 PM #6
Java Code:Node curr = listHead; while(curr.next != null) { curr = curr.next; } // curr is now the last element in the listDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-22-2009, 04:17 PM #7
It solved my problem. Can you explain me a difference between (curr.next != null and curr != null) in while condition?
Thank you, I appreciate you help :)Ubuntu, Linux for human Beings!
- 05-22-2009, 04:49 PM #8
(curr.next != null) checks to see if the next one is null
(curr != null) checks to see if the current one is null
If you keep going until the current one is null, all you end up with is null, which isn't very helpful.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Linked list
By rosh72851 in forum New To JavaReplies: 1Last Post: 02-05-2009, 07:21 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 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