• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting linked lists via consequtive nodes  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code that supposed to perfrom sorting on the linked list using method sort in order usind node concept of Linked List but seems to give inlogic results. the following code tests only the values lower than the first value but i can't manage to sort the data higher than the first entered value;
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, your Comparable is a raw type. Don't use raw types. It should be class OrderedList<T extends Comparable<? super T>>. Secondly the T type parameter in your insertInOrder method is overriding the type parameter declared at the class level. Leave it out.

Stop staring at the code and grab a piece of paper and pencil. you should think in broad lines about what a method is supposed to do.

So, to insert in order, you would first need to find the correct node where you're going to insert the element, right? Then you need to make room for the new element, and make sure the nodes are connected correctly. So:
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this. If you try to sort a linked list in situ, you are liable to get very poor performance.
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally i could solve the probem with inserting in ordered linked list and the link is below
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!