码迷,mamicode.com
首页 > 编程语言 > 详细

C单链表排序

时间:2017-10-20 21:46:06      阅读:248      评论:0      收藏:0      [点我收藏+]

标签: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 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单链表排序

标签:null   success   ddr   auth   short   链表   ext   lis   bsp   

原文地址:http://www.cnblogs.com/lort/p/7701207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!