Results 1 to 8 of 8
Thread: help with linked list
- 01-24-2011, 12:26 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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?
and there is the node classJava 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); } }
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..
- 01-24-2011, 12:45 AM #2
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);
- 01-24-2011, 03:06 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
no that wasn't it..
can anyone see a problem with the code somewhere?
- 01-24-2011, 04:00 PM #4
Didn't travishein just point out something wrong with the code?
- 01-24-2011, 04:33 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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.
- 01-24-2011, 04:42 PM #6
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:
and a dynamic array object type might have a toString method like this:Java Code://in the Node class public String toString(){ return "Data: "+this.data; }
Then, when you use System.out.println(someNode), you get a nice printout, same with System.out.println(someDynamicArray).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; }
You can also just print fields from inside the object you're trying to print, for example:
My samples may not match your method/object names but you should understand the logic. Does this help?Java Code://instead of System.out.println(list0); //try System.out.println(list0.peek()); //or System.out.println(list0.peek().data);
- 01-24-2011, 05:07 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
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);
- 01-24-2011, 06:40 PM #8
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.I'm confused about the first system.out of list 0, since it supposed to be empty what the output should look like?
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:
Your output for an empty list would be:Java Code:public String toString(){ return "->"+head; }
->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:
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.Java Code:public String toString(){ return data + "->" + nextNode; }
Edit:
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).The toString() method in the node is recursive.Last edited by quad64bit; 01-24-2011 at 06:43 PM. Reason: Clarification
Similar Threads
-
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks