Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Java Newbie - Sorted Linked List not inserting properly - please help!

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by bubbleboy; June 15th, 2011 at 11:38 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    the iterative version or my recursive version?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    The one that shows the problem.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by bubbleboy; June 14th, 2011 at 04:19 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by bubbleboy; June 15th, 2011 at 11:38 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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:
               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.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by bubbleboy; June 15th, 2011 at 11:39 PM.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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){

  12. #12
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 =/

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    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.

                // If node is less than curr, then node.next should point to curr
      ....
                ........   //<<<<<< return head of new list

  14. #14
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    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?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default 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?

  17. #17
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by bubbleboy; June 15th, 2011 at 09:35 PM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    It always did for me.

  19. #19
    Junior Member
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    lol thnx for ur help norm

    this is my first time learning programming and im glad there's ppl like you here who steer ppl in right direction xDDDDDDDd

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Java Newbie - Sorted Linked List not inserting properly - please help!

    many thanks for the kind words

  21. #21
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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!

Similar Threads

  1. need help with Doubly-Linked list programing in java
    By bulutbey in forum Algorithms & Recursion
    Replies: 3
    Last Post: August 22nd, 2011, 02:51 PM
  2. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  3. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 10:52 PM
  4. Replies: 1
    Last Post: March 15th, 2010, 11:03 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:23 PM