日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言 链表数据的排序

發(fā)布時(shí)間:2024/1/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言 链表数据的排序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C語言使用鏈表時(shí),有些時(shí)候會(huì)對鏈表中的數(shù)據(jù)進(jìn)行排序。下邊介紹使用鏈表時(shí)可用的排序方法,冒泡排序和選擇排序。

此鏈表排序僅對鏈表中的數(shù)據(jù)進(jìn)行排序,如果想進(jìn)行對整個(gè)結(jié)構(gòu)體的排序,也就是利用數(shù)據(jù)順序來調(diào)整節(jié)點(diǎn)中的信息,需要對節(jié)點(diǎn)進(jìn)行交換,但與此方法不同,望讀者周知。

測試排序代碼請先參考下邊完整的測試代碼。
編程環(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點(diǎn)擊回車?yá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ù),當(dāng)輸入0時(shí)停止\n");pNew=(NODE*)malloc(sizeof(NODE));scanf("%d",&pNew->date);while(pNew->date != 0){count++;if(count == 1) //如果為頭節(jié)點(diǎn){pNew->next = NULL;pEnd = pNew;pHead = pNew;}else //如果不是頭結(jié)點(diǎn){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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。