-
Java Newbie - Sorted Linked List not inserting properly - please help!
Hi everyone
I have no idea what to do after this point (I tried using debugger and I am really new to this.. :( ) i think the issue is in the else if statements but I have no idea how to correct them
what am i doing wrong and if you need more code for reference pls let me know i will PM it to you
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Can you post code that compiles and executes that shows the problem?
Remember to add code tags: BB Code List - Java Forums
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
the iterative version or my recursive version?
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
The one that shows the problem.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
ok i will pm you that one because i am not comfortable putting it fully here xD
edit: sent
its the iterative one. you can just copy and paste my recursive code for the insert function from above into it and you'll see my output. thnx.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Could you send the one I'm to test. Not edits needed.
The one you sent was unformatted. I don't like working with unformatted code.
Be sure the code is formatted.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
all right sorry. i'll just post it here then
edit: ugh lemme try to make it better. sorry i'm really new to this please forgive me.
edit2: ahh this is better :D
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
I don't know how you are debugging. I use printlns.
For good debug output you need to add a toString method to Node class:
Code :
public String toString() {
return data.toString();
}
Then add a println to the insert() method to show all the variables and the results of the compareTo() calls.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
i am using eclipse to write this probram so it shows all the data for variables when i use the debugger.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
hmm i think i found my mistake but i don't understand how i can fix it. it keeps messing up the curr values and puts them at the front.
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Time to get out the paper and pencil and work thru how the links should be set and when.
I notice that there are no comments in the code as to what it needs to do at each point in the logic.
For example what does this test mean and what should be done next:
if (node.data.compareTo(curr.data) < 0){
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
its messing up after the 3rd iteration but i have still not been able to figure out how to write the logic correctly so it inserts in order before curr =/
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Quote:
how to write the logic correctly
What is the logic? Your code has no comments so I don't know what your logic is.
When I added these two comments at the appropriate places in the code, I saw what was wrong and was able to fix it.
Code :
// If node is less than curr, then node.next should point to curr
....
........ //<<<<<< return head of new list
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
hi norm!
thanks for that I was able to get my insert running properly :)
however I am having another issue now with search...
when i put Sytem.out.println(list.search()); into my main i want to test out whether a number i put into it is in the list or not.
i'm trying to write this down on paper but i can't figure out what my base case should be. should it be that if curr is null return false? but that isnt working because curr is always null =/ i dont understand how i can work around this. what steps do you recommend i take because now im feeling utterly lost
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Quote:
curr is always null
Sorry I don't understand where you are referencing curr that it is null. I assume that curr is the value passed to a method. Can you show the code and the debug output that shows when it is null?
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
Where is the value of curr set? It's an argument to a method. What is its value when the call is made?
Is null a valid value? What would it be if the list is empty?
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
wow norm i am an idiot
i had put the system.out.println stuff in the wrong place... so ofc it was going to be null... wow...
i put it between the insert and remove's for loops and now its working properly. i had the methods right i was just not calilng them properly... god i really hate hwo bad i am at thislol
yay now my search works
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
lol thnx for ur help norm :D
this is my first time learning programming and im glad there's ppl like you here who steer ppl in right direction xDDDDDDDd
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
many thanks for the kind words
-
Re: Java Newbie - Sorted Linked List not inserting properly - please help!
If you're going to post a question up on the public forum, please post up the relevant code - as it is the whole thread is meaningless because nobody can see the code that's being discussed.
If you posted the code and deleted it afterwards, just ... don't!