• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Help With Recursively Sorted Linked List

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method I have right now is sorting the list but doing it backwards. I want it to print alphabetically. I've tried changing parts of the code but I end up just making it unable to compile.

Current code:



Current output:

The members of the list are:
Amy
Bobby
Charlie
Danielle
Edward

Reversed linked list:
Edward
Danielle
Charlie
Bobby
Amy

Sorted linked list:
Edward
Danielle
Charlie
Bobby
Amy
 
Rachel Green
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UPDATE: I just realized the sort method isn't actually doing anything. The print call below is just printing the list as it appears after the reverse method (which does work). So I'm back to square 1 with the sorting. I want to take what's in the linked list and recursively add those names to a new, sorted list.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's kind of hard to read your code and figure out what you're trying to do with sorting.  Can you describe in plain English this recursive sorting algorithm you are trying to implement? Does it have a name?
 
Marshal
Posts: 80061
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this, which will tell you there are problems with sorting linked lists.
 
Rachel Green
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like I wasn't very clear in what I'm trying to do, so I'm very sorry about that. Basically, I was given the code you see above and I have been tasked with creating a method that takes the linked list and sorts it into another list, using recursion. I need to create a method that goes through the existing linked list and copies the elements into a new linked list, in alphabetical order. I hope that helps clear up the confusion!
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use recursion?  Or was that just a suggestion?  Either way, it would be best to write out an algorithm that will loop through the unsorted list and create a new sorted list.

Algorithms (in case you don't know) are step by step instructions that given a certain input will always produce the correct output.  You input is the unsorted list.  Your output is the sorted list.  Each step should be simple, unambiguous, and not use any Java terms.
 
Rachel Green
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:You have to use recursion?  Or was that just a suggestion?  Either way, it would be best to write out an algorithm that will loop through the unsorted list and create a new sorted list.

Algorithms (in case you don't know) are step by step instructions that given a certain input will always produce the correct output.  You input is the unsorted list.  Your output is the sorted list.  Each step should be simple, unambiguous, and not use any Java terms.



This is exactly what I need. I do have to use recursion and this is very new to me. I just can't figure out how to get started.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple way to do this is to interpret the sort() method as "sort all the nodes after this one, and then insert this one at the right location in the following nodes". It's essentially an insertion sort, but with a linked list.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A recursive method/function has the following basic elements:

1. The base case, also known as the degenerate case, of which there should be at least one. There could be more than one. A degenerate case defines the condition(s) under which the recursion stops.

2. The recursive cases or the conditions under which recursion occurs.

You need to identify all possible cases/scenarios that your method must operate under and determine whether each scenario is a degenerate case or a recursive case. Then you need to structure your logic so that each one is handled appropriately.  

Your implementation doesn't clearly indicate to me that you understand whether or not you've covered all the possible cases. If you do, then you're still not understanding what to do in each particular case.

Hint: From what I can tell, your code does cover all cases, both degenerate and recursive. You're not doing the right thing in the degenerate cases though.

We'll try to help you think this through. List out the degenerate and recursive cases and match each case to the line number(s) in your implementation. Then, explain in plain English what you're doing to handle each case. Don't explain the code you wrote. In fact, ignore the code you wrote because it's incorrect. Explain the algorithm at a high level. What should you do in each case. Try to explain it in terms of other list operations.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic