C單鏈表排序

來源:互聯網
上載者:User

標籤:null   success   ddr   auth   short   鏈表   ext   lis   bsp   

/* ============================================================================ 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 inttypedef 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單鏈表排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.