#include <stdio.h>#include<stdlib.h>#defineContainer_of (PTR, type, mem) (Type *) ((unsigned long) ptr-(unsigned long) & ((type *) NULL)->mem)structPerson {structPerson *Next; structPerson *pre;};structBoy {structPerson p; intAge ;};structPerson *creat (structPerson *head,intAge );structPerson *choose_sort (structPerson *head);structPerson *insert_sort (structPerson *head);voidShowstructPerson *head);intMain () {structPerson *head =NULL; Head= Creat (Head, -); Head= Creat (Head, -); Head= Creat (Head, -); Head= Creat (Head, the); Head= Creat (Head, -); Head= Creat (Head, +); Head=Insert_sort (head); Show (head); }structPerson *creat (structPerson *head,intAge ) { structBoy *tmp =NULL; structPerson *find =NULL; TMP= (structBoy *) malloc (sizeof(structBoy )); TMP->age =Age ; TMP->p.next =NULL; TMP->p.pre =NULL; if(NULL = =head) {Head= &tmp->p; Head->next =Head; Head->pre =Head; returnHead; } Find=Head; while(Find->next! =head) {Find= find->Next; } Find->next = &tmp->p; TMP->p.pre =find; TMP->p.next =Head; Head->pre = &tmp->p; returnhead;}#if0structPerson *choose_sort (structPerson *head) { structPerson *tmp =NULL; structBoy *min =NULL; structPerson *newhead =NULL; structPerson *tail =NULL; structBoy *find =NULL; while(head) {tmp=Head; Min= Container_of (Head,structBoy , p); Do{Find= Container_of (TMP,structBoy , p); if(Find->age < min->Age ) {min=find; } tmp= tmp->Next; } while(TMP! =head); structPerson *min_p = &min->p; if(Min_p = = Head && head->next! =head) {Head= head->Next; Head->pre = min_p->Pre; Min_p->pre->next =Head; Min_p->pre =NULL; Min_p->pre =NULL; } Else { if(Min_p = = Head && Head->next = =head) {Head->next =NULL; Head->pre =NULL; Head= head->Next; } Else{min_p->pre->next = min_p->Next; Min_p->next->pre = min_p->Pre; Min_p->next =NULL; Min_p->pre =NULL; } } if(NULL = =newhead) {Newhead=min_p; Newhead->next =Newhead; Newhead->pre =Newhead; Tail=Newhead; Continue; } Tail->next =min_p; Min_p->pre =tail; Min_p->next =Newhead; Newhead->pre =min_p; Tail=min_p; } returnNewhead;}#elsestructPerson *choose_sort (structPerson *head) { structPerson *tmp =NULL; structBoy *min =NULL; structPerson *newhead =NULL; structPerson *tail =NULL; structBoy *find =NULL; while(head) {tmp=Head; Min= Container_of (Head,structBoy , p); Do{Find= Container_of (TMP,structBoy , p); if(Find->age < min->Age ) {min=find; } tmp= tmp->Next; } while(TMP! =head); if(&min->p = = Head && head->next! =head) {Head= head->Next; Head->pre = min->P.pre; Min->p.pre->next =Head; Min->p.pre =NULL; Min->p.next =NULL; } Else { if(&min->p = = Head && Head->next = =head) {Head->next =NULL; Head->pre =NULL; Head= head->Next; } Else{min->p.pre->next = min->P.next; Min->p.next->pre = min->P.pre; Min->p.next =NULL; Min->p.pre =NULL; } } if(NULL = =newhead) {Newhead= &min->p; Newhead->next =Newhead; Newhead->pre =Newhead; Tail=Newhead; Continue; } Tail->next = &min->p; Min->p.pre =tail; Min->p.next =Newhead; Newhead->pre = &min->p; Tail= &min->p; } returnNewhead;}#endifstructPerson *insert_sort (structPerson *head) { structBoy *tmp =NULL; structBoy *New=NULL; structPerson *find =NULL; structPerson *list =NULL; structPerson *newhead =NULL; structPerson *tail =NULL; structBoy *key =NULL; while(head) {Find=Head; TMP= Container_of (Find,structBoy , p); if(Head->next = =head) {Head->next =NULL; Head= head->Next; Find->pre =NULL; } Else{Head= head->Next; Head->pre = find->Pre; Find->pre->next =Head; Find->pre =NULL; Find->next =NULL; } if(NULL = =newhead) {Newhead=find; Newhead->pre =Newhead; Newhead->next =Newhead; Continue; } New= Container_of (Newhead,structBoy , p); if(Tmp->age <New-Age ) {Find->next =Newhead; Find->pre = newhead->Pre; Newhead->pre->next =find; Newhead->pre =find; Newhead=find; Continue; } list=Newhead; Do{Key= Container_of (list,structBoy , p); if(Key->age > tmp->Age ) Break; List= list->Next; } while(List! =newhead); if(Key->age < tmp->Age ) {List->next =find; Find->pre =list; Find->next =Newhead; Newhead->pre =find; } Else{Find->next =list; Find->pre = list->Pre; List->pre->next =find; List->pre =find; } } returnNewhead;}voidShowstructPerson *head) { structPerson *tmp =NULL; structBoy *find =NULL; TMP=Head; Do{Find= Container_of (TMP,structBoy , p); printf ("Age is %d\n", find->Age ); TMP= tmp->Next; } while(TMP! =head); printf ("-------------------------------------\ n"); Do{Find= Container_of (TMP,structBoy , p); printf ("Age is %d\n", find->Age ); TMP= tmp->Pre; } while(TMP! =head);}
Linux Simple kernel list sorting