Results 1 to 8 of 8
  1. #1
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default [SOLVED] Problem with Linked List

    Hello, I have problem with LinkedList implementation,

    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?
    	}
    }
    How can I insert a newItem on last position of linked list without adding "private Node last;" at class Node?

    any help will be appreciated!
    Ubuntu, Linux for human Beings!

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    search through list until next==null
    I have already tried,
    Java Code:
    while(this.ListHead  != null){
    this.ListHead = this.ListHead.next;
    }
    ListHead = newItem;
    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.

    is this correct way to search for null Node?
    Java Code:
    this.ListHead = this.ListHead.next;
    Ubuntu, Linux for human Beings!

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  5. #5
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by OrangeDog View Post
    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.
    I don't understand how with another variable possible to add newItem to the end of ListHead. Can you provide some example?
    Ubuntu, Linux for human Beings!

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Java Code:
    Node curr = listHead;
    while(curr.next != null) {
        curr = curr.next;
    }
    // curr is now the last element in the list
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    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!

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    (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

  1. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  2. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 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
  •