Simple sort implementation of C-way linked list

Source: Internet
Author: User

Today, I occasionally see a single necklace list of C structures.

So re-review the next two-way list, rewrite the next two-way list of simple sorting implementation, as a review summary.

Define a doubly linked list first

1 struct student{2     int StudentID; 3     Char * name; 4     Student *next, * last; 5 };

Then there is the key sorting method:

intSortbyname (Student *p) {Student*head =p; //start sorting from the head of the list (you can also get rid of the words that are sorted from the incoming node)     while(Head->last! =NULL) {Head= head->Last ; }     while(Head! =NULL) {Student*headnext = head->Next;  while(Headnext! =NULL) {            if(strcmp (Head->name, Headnext->name) >0) {swapstudent (head, Headnext); //Note that this must be exchanged because the location of the linked list node is changedStudent *tmp =Head; Head=Headnext; Headnext=tmp; } Headnext= headnext->Next; } head= head->Next; }    return 1;}

It also involves a swapstudent method, which realizes the function of exchanging two nodes.

The code is as follows:

voidSwapstudent (Student *s1, Student *S2) {    if(S1->next = =S2) {        if(S2->next! =NULL) {S2->next->last =S1; }        if(S1->last! =NULL) {S1->last->next =S2; } S1->next = s2->Next; S2->last = s1->Last ; S1->last =S2; S2->next =S1; }Else if(S1->last = =S2) {        if(S1->next! =NULL) {S1->next->last =S2; }        if(S2->last! =NULL) {S2->last->next =S1; } S2->next = s1->Next; S1->last = s2->Last ; S2->last =S1; S1->next =S2; }Else{        //It's important to note that the value of the upper and lower nodes in the node adjacent node is updated//I've been in this hole for a long time to find out.//The above two cycle bodies also need to be aware of        if(S1->next! =NULL) {S1->next->last =S2; }        if(S1->last! =NULL) {S1->last->next =S2; }        if(S2->next! =NULL) {S2->next->last =S1; }        if(S2->last! =NULL) {S2->last->next =S1; } Student*s1next = s1->Next; Student*s1last = s1->Last ; S1->next = s2->Next; S1->last = s2->Last ; S2->next =S1next; S2->last =S1last; }}

Above is the core implementation of the simple sort implementation.

If you look carefully, you will notice a problem.

If you use bubble sort, the swap method above can be implemented simply because only the adjacent two nodes are exchanged.

I write this to be more generalized, the node operation requirements are higher, to facilitate their understanding.

Simple sort implementation of C-way linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.