| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 343 人关注过本帖
标题:基数排序问题
收藏  订阅  推荐  打印
wangyinshiwo
Rank: 2
等级:注册会员
帖子:72
积分:1142
注册:2007-11-9
基数排序问题

/* Note:Your choice is C IDE */
#include "stdio.h"
#include<malloc.h>
#define MAX 100
#define MAXR 10        /*基数的最大取值*/
#define MAXD 8       /*关键字位数的最大取值*/
typedef char *keytype;
typedef struct node
{
    keytype data;
    struct node *next;
}Rectype;
Rectype *creatlink(char *a[],int n)
{
    int i;
    Rectype *head,*p,*q;
    p=head=(Rectype *)malloc(sizeof(Rectype));
    if(n<1)return NULL;
    for(i=0;i<n;i++)
    {
        q=(Rectype *)malloc(sizeof(Rectype));
        q->data=a[i];
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return head;
}
Rectype *Radixsort(Rectype *head,int r,int d)    /*r为基数,d为关键字位数*/
{
    Rectype *front[MAXR],*rear[MAXR],*p,*q;
    int i,j,k;
    p=head->next;
    for(i=d-1;i>=0;i--)
    {
        for(j=0;j<r;j++)
        front[j]=rear[j]=NULL;
        while(p)
        {
            k=p->data[i]-'0';
            if(!front[k])
            front[k]=p;
            else
            rear[k]->next=p;
            rear[k]=p;
            p=p->next;
        }
        for(j=0;j<r;j++)
        if(front[j])
        {
            if(!p)
            p=front[j];
            else
            q->next=front[j];   /*各链表首尾顺序链接*/
            q=rear[j];
        }
        q->next=NULL;
    }
    head->next=p;
    return head;
}
void display(Rectype *head)
{
    Rectype *p;
    p=head->next;
    while(p)
    {
        printf("%6s",p->data);
        p=p->next;
    }printf("\n");
}
void main()
{
    int n=10;
    Rectype *head;
    char *a[]={"75","23","98","44","57","12","29","64","38","82"};
    printf("初始化关键字:");
    head=creatlink(a,n);display(head);
    printf("最终结果:");
    Radixsort(head,10,2);display(head);   
}
这里的  k=p->data[i]-'0';是什么意思,为什么要减零啊!

[ 本帖最后由 wangyinshiwo 于 2008-8-10 10:47 编辑 ]
2008-8-8 15:09
共有 342 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.045790 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved