C语言 链表数据的排序
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C语言 链表数据的排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                C語言使用鏈表時,有些時候會對鏈表中的數(shù)據(jù)進行排序。下邊介紹使用鏈表時可用的排序方法,冒泡排序和選擇排序。
此鏈表排序僅對鏈表中的數(shù)據(jù)進行排序,如果想進行對整個結(jié)構(gòu)體的排序,也就是利用數(shù)據(jù)順序來調(diào)整節(jié)點中的信息,需要對節(jié)點進行交換,但與此方法不同,望讀者周知。
測試排序代碼請先參考下邊完整的測試代碼。
 編程環(huán)境:Visual C++ 6.0.
冒泡排序
NODE* bubblesort(NODE* Head) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL;pfirst=Head;psecond=Head;int temp;while(pfirst != pend) //外循環(huán){ //pfirst != pend 很有意思while(pfirst->next != pend)//內(nèi)循環(huán){if(pfirst->date > pfirst->next->date){temp=pfirst->date; pfirst->date=pfirst->next->date;pfirst->next->date=temp;}pfirst=pfirst->next;}pend=pfirst;//減少最后的已排好的循環(huán)pfirst=Head;}return Head; }選擇排序
/*鏈表的選擇排序*/NODE* selectsort(NODE* head) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL;pfirst=head;psecond=head;int temp;while(pfirst != pend) //外循環(huán){while(pfirst->next != pend)//內(nèi)循環(huán){if(psecond->date > pfirst->next->date){temp=psecond->date; psecond->date=pfirst->next->date;pfirst->next->date=temp;}pfirst=pfirst->next;}psecond=psecond->next;pfirst=psecond;}return head; }測試代碼
/**鏈表排序 Writen by YU*/ #include<stdio.h> #include<stdlib.h>/*結(jié)果輸出查看*/ void endscan() {printf("\n點擊回車繼續(xù)...");fflush(stdin);getchar(); }/*結(jié)構(gòu)體*/ struct node {int date;struct node *next; };typedef struct node NODE; //把struct node 定義為 NODE int count = 0;/*創(chuàng)建鏈表并輸入數(shù)據(jù)*/ NODE* creat() {NODE *pHead=NULL,*pNew,*pEnd;printf("輸入數(shù)據(jù),當輸入0時停止\n");pNew=(NODE*)malloc(sizeof(NODE));scanf("%d",&pNew->date);while(pNew->date != 0){count++;if(count == 1) //如果為頭節(jié)點{pNew->next = NULL;pEnd = pNew;pHead = pNew;}else //如果不是頭結(jié)點{pNew->next=NULL;pEnd->next=pNew;pEnd=pNew;}pNew=(NODE*)malloc(sizeof(NODE));scanf("%d",&pNew->date);}free(pNew);return pHead; }/*輸出鏈表*/ void print(NODE* Head) {NODE* p=Head;while(p!=NULL){printf("%d ",p->date);p=p->next;} }/*鏈表的冒泡排序*/ NODE* bubblesort(NODE* Head) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL;pfirst=Head;psecond=Head;int temp;while(pfirst != pend) //外循環(huán){ //pfirst != pend 很有意思while(pfirst->next != pend)//內(nèi)循環(huán){if(pfirst->date > pfirst->next->date){temp=pfirst->date; pfirst->date=pfirst->next->date;pfirst->next->date=temp;}pfirst=pfirst->next;}pend=pfirst;//減少最后的已排好的循環(huán)pfirst=Head;}return Head; }/*鏈表的選擇排序*/NODE* selectsort(NODE* head) {NODE *pfirst=NULL,*psecond=NULL,*pend=NULL;pfirst=head;psecond=head;int temp;while(pfirst != pend) //外循環(huán){while(pfirst->next != pend)//內(nèi)循環(huán){if(psecond->date > pfirst->next->date){temp=psecond->date; psecond->date=pfirst->next->date;pfirst->next->date=temp;}pfirst=pfirst->next;}psecond=psecond->next;pfirst=psecond;}return head; } int main() {NODE* pHead=NULL;pHead=creat();printf("排序前:\n");print(pHead); // pHead=bubblesort(pHead); //冒泡排序pHead=selectsort(pHead); //選擇排序printf("\n排序后:\n");print(pHead);endscan();return 0; }總結(jié)
以上是生活随笔為你收集整理的C语言 链表数据的排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Centos 7, Torque 单节
- 下一篇: Python--猜水果游戏
