Tuesday, August 17, 2010

What is the source code to sort a linked list in C? Strings are what to be sorted.?

It's a linked list of structures with strings. The whole program is like a directory. The string is the name. We must sort it out alphabetically as we enter the data. Kind of frustrating that I have to do ask this.

What is the source code to sort a linked list in C? Strings are what to be sorted.?
Instead of sorting the list after entering the node, you must sort it while inserting the node. Whenever you add a new node compare it with all the nodes already present in the linked list and insert it accordingly. The "head" pointer points to the first node and the successive "next" pointers point to the nodes that are (alphabetically) smaller than the current node.





This is just kind of a rough algorithm. Try to code it, hope you got it.
Reply:When creating linked lists I make sure a prototypical comparison method exists. This method is generally customized for each type making use of the template.





Like strcmp() the method is intended to return less than zero if the left operand is less than the right, zero if equal, and greater than zero if the left operand is greater than the right.





Two sorting methods are provided (as overloads of each other.) One is the general sort which will do the basic ascending or descending values of the list. The other accepts a function pointer that allows for a custom sorting algorithm.





An example of a custom sorting algorithm is a randomizer; it simply returns one of the three expected returns at random. :-)
Reply:That is not easy. May be you can contact a C expert at websites like http://askexpert.info/


No comments:

Post a Comment