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

    Default help with linked list

    hello, for some reason I'm not able to add anything to the list what did go wrond does anyone knows?

    Java Code:
    public class myList{
    private stringNode _head;
     
    //constructor - for creating an empty first node.
    public myList(){_head = null;}
    //constructor - for creating a text.
    public myList (String t){
    stringNode text = new stringNode (t,_head);
    _head = text;
      }
        
    //checks if the first node is empty, designed to be embedded in other methods.  
    public boolean isEmpty(){
    if(_head==null)
    return true;
    else
    return false;
      }  
    
    //adds a word to the database.  
    public void addToData (String word){
    stringNode recWord = new stringNode (word,null);
    if(isEmpty()){
    _head = recWord; 
    }
    else{
        stringNode ptr=_head;
        while (ptr.getNext() !=null)
            ptr = ptr.getNext();
            ptr.setNext (recWord);
        }
      }
    and there is the node class

    Java Code:
    public class WordNode {
    String _word;
    stringNode _next;
    
    //constructor
    public stringNode (String word, stringNode n){
    _word = word;
    _next = n;
    }
    
    public stringNode getNext (){return _next;}
    public void setNext (stringNode next) {_next = next;}
    public String getWord (){return _word;}
    
    
    }

    Appreciate all the help as I'm a bit lost at the moment..

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    i believe the setNext() should not be inside the while loop, instead, after.
    Java Code:
        while (ptr.getNext() !=null)
            ptr = ptr.getNext();
    -        ptr.setNext (recWord);
        }
    +        ptr.setNext (recWord);

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default no that wasn't it..

    can anyone see a problem with the code somewhere?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Didn't travishein just point out something wrong with the code?

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

    Default he did but it wasn't it

    He meant that I need to take this sentence out of the loop:

    while (ptr.getNext() !=null)
    ptr = ptr.getNext();
    ptr.setNext (a);

    I'm great full for the good will but it was already out, sense there are no "{" so the while loop does the first and the after the loop does the second. With the solution it does exactly the same.
    I'm really appreciating the effort and know it's allot to look at but I really hope to grasp the linked list subject.

    With the tester class I receive a fictitious address with an @ sign.
    I have attached my tester class as well here:

    Java Code:
    public static void main(String args[]) {
            System.out.println("************** TESTER ********************");
             TextList list0 = new TextList();
             System.out.println(list0);    
            list0.addToData("hello");
            System.out.println(list0);

    Thanks.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Well, you're getting the real address in memory as it relates to the JVM of where the object resides. To print something other than that, the object you are trying to print needs a public toString() method that returns a string. From within that method, you can return information formatted any way you like. For instance, a single node might have a toString method like this:
    Java Code:
    //in the Node class
    public String toString(){
        return "Data: "+this.data;
    }
    and a dynamic array object type might have a toString method like this:
    Java Code:
    public String toString(){
        String result = "The contents of this DynamicArray are:\n";
        for(int i=0; i<this.size; i++){
            result += i+": "+dataElements[i]+"\n";
        }
        return result;
    }
    Then, when you use System.out.println(someNode), you get a nice printout, same with System.out.println(someDynamicArray).

    You can also just print fields from inside the object you're trying to print, for example:
    Java Code:
    //instead of
    System.out.println(list0);
    //try
    System.out.println(list0.peek());
    //or
    System.out.println(list0.peek().data);
    My samples may not match your method/object names but you should understand the logic. Does this help?

  7. #7
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default Ofcourse! what was I thinking..

    :) Thanks allot I'll try that until the next problem..
    I'm confused about the first system.out of list 0, since it supposed to be empty what the output should look like?

    Java Code:
    TextList list0 = new TextList();
             System.out.println(list0);

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I'm confused about the first system.out of list 0, since it supposed to be empty what the output should look like?
    So, if in your list data structure (if you're building this list from scratch), if you include a recursive version of the toString I gave you, it would still work.

    If your list has the common elements, such as a root node called 'head' which is either null or is a link to the first node, your toString for the list as a while might be something like this:

    Java Code:
    public String toString(){
        return "->"+head;
    }
    Your output for an empty list would be:
    ->null
    meaning that the list is empty. A list with elements might look something like
    ->23->61->44->null
    meaning 23 is the head and 44 is the tail. So to make the entire list print, you will need a recursive style call in your nodes. In the toString() method in the the Node class (which encompasses the head, body and tail elements) you would do something like this code:

    Java Code:
    public String toString(){
        return data + "->" + nextNode;
    }
    where nextNode is the link to the next node in the list. If the nextNode is null, then the word null will be printed automatically. The toString() method in the node is recursive. Its triggered when we attempt to treat an object as a string as in System.out.println(); or assignment.

    Edit:
    The toString() method in the node is recursive.
    Only in this case, not in general. toString is simply a method that returns a string of whatever you wan for an object. But in this case, inside our toString - toString gets called again when we attempt to print the nextNode object (since it too has a toString method, and so on and so forth).
    Last edited by quad64bit; 01-24-2011 at 07:43 PM. Reason: Clarification

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  2. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  3. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  4. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM
  5. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 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
  •