Computing.Net > Forums > Programming > Sort a link list

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sort a link list

Reply to Message Icon

Name: coolieplanet
Date: March 4, 2003 at 12:09:02 Pacific
OS: XP
CPU/Ram: Pen4/256
Comment:

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;
}



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: March 4, 2003 at 14:09:24 Pacific
Reply:

"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


0
Reply to Message Icon

Related Posts

See More


enviroment var It is Project time again



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Sort a link list

C++ linked lists - deleting a node www.computing.net/answers/programming/c-linked-lists-deleting-a-node/4709.html

Linked list www.computing.net/answers/programming/linked-list/6985.html

Link list www.computing.net/answers/programming/link-list/13141.html