Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
i have this code. "head" now contain a pointer to a list. How can i sort the list??
any suggestion will be helpful. Thanks a lot#include
struct Node
{
int data;
.
.
.
Node* next;
}
.
.
.
void displaysortedlist(Node* head);
int main()
{
Node* head=NULL;
.
.
.
display(head);
return 0;
}

"How can I sort this list?" - This has a lot of answers...Consider reading about sorting algorithms and deciding which one to implement, then ask about the specifics. A few of the most popular are insertion sort (fast for only a few elements, easy to understand and implement, but pretty slow for large lists - on the order of n^2) - quicksort, merge sort, and heap sort (tough to implement and understand, but dang fast - on the order of nlogn)
If you're just looking to sort a few elements quick and dirty, learn insertion sort, which means moving each node down towards the head until it encounters a node that is smaller than it is, then inserting it there. Search for "insertion sort" and find lots of resources. Best of luck,
-SN

![]() |
enviroment var
|
It is Project time again
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |