C单链表排序
Posted lort
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C单链表排序相关的知识,希望对你有一定的参考价值。
/* ============================================================================ Name : test.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #define uint8 unsigned char #define uint16 unsigned short #define uint32 unsigned int typedef struct { void *next; uint8 val; }myDesc_t; static myDesc_t *myDesc = NULL; static void Insert(myDesc_t *pdesc); static void InitDesc(void); static void dumpDesc(void); static void InitDesc(void) { int i; myDesc_t *pcur; for(i=0;i<10;i++) { pcur = (myDesc_t*)malloc(sizeof(myDesc_t)); if(pcur!=NULL) { pcur->val = rand(); pcur->next = NULL; Insert(pcur); } } } static void Insert(myDesc_t *pdesc) { myDesc_t *pcur; if(myDesc==NULL) myDesc = pdesc; else { pcur = myDesc; while(pcur->next!=NULL) pcur = pcur->next; pcur->next = pdesc; } } static void dumpDesc(void) { myDesc_t *pcur; pcur = myDesc; while(pcur!=NULL) { printf("dump: addr:%08x, lqi:%d \r\n", (unsigned int)pcur,pcur->val); pcur = pcur->next; } } static void dumplist(myDesc_t **plist, uint8 num) { myDesc_t *pcur; uint8 i; printf("dumplist:\r\n"); for(i=0;i<num;i++) { pcur = plist[i]; printf("dump: addr:%08x, lqi:%d \r\n", (unsigned int)pcur,pcur->val); } } #include "unistd.h" static myDesc_t **sort_desc(myDesc_t *pDescList, uint8 num); int main(void) { srand(3); InitDesc(); dumpDesc(); myDesc_t **pl = sort_desc(myDesc,10); dumplist(pl,10); free(pl); while(1) sleep(1); puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */ return EXIT_SUCCESS; } static uint8 check_descExist(myDesc_t **plist,uint8 num, myDesc_t *desc) { uint8 i; for(i=0;i<num;i++) { if(plist[i]==desc) return 0x01; } return 0x00; } static myDesc_t **sort_desc(myDesc_t *pDescList, uint8 num) { myDesc_t **pList; myDesc_t *pcur; myDesc_t *pmax; uint8 max; uint8 i; printf("sort_desc:\r\n"); pList = (myDesc_t**)malloc(sizeof(myDesc_t *)*num); if(pList!=NULL) { for(i=0;i<num;i++) { pcur = pDescList; max = 0x00; pmax = NULL; while(pcur!=NULL) { if(check_descExist(pList,i,pcur)==0x00) { if(max<pcur->val) { max = pcur->val; pmax = pcur; } } pcur = pcur->next; } pList[i] = pmax; } } return pList; }
以上是关于C单链表排序的主要内容,如果未能解决你的问题,请参考以下文章