tc4.0 编译通过,但是运行时会自动跳出,我测试了应该是array_to_list和lsd_sort这两个函数有问题,但不知道问题在哪,请教各位大虾,急着要交!!!
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CLOCKS_PER_SEC ((clock_t)1000)
#define SIZE 10000
#define MAX_R 10
#define MAX_D 5
unsigned int data[SIZE];
struct node { unsigned int data[MAX_D];
struct node *link;
};
typedef struct node NODE;
void generate_num()
{
int i;
unsigned int num[SIZE];
FILE *fp;
srand((unsigned)time(NULL));
for(i=0;i<SIZE;i++)
num[i]=(unsigned) rand();
if((fp=fopen("num.txt","w+"))==NULL)
{
printf("Can not save data !\n");
return;
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&num[i],sizeof(unsigned int),1,fp)!=1)
{
printf("File write error !\n");
exit(1);
}
}
fclose(fp);
}
void load_num()
{
int i;
FILE *fp;
if((fp=fopen("num.txt","rb"))==NULL)
{
printf("Can not load data !\n");
exit(1);
}
for(i=0;i<SIZE;i++)
{
if(fread(&data[i],sizeof(unsigned int),1,fp)!=1)
{
printf("File read error !\n");
exit(1);
}
}
fclose(fp);
}
void copy_array(unsigned int num[])
{
int i;
for(i=0;i<SIZE;i++)
num[i]=data[i];
}
NODE *array_to_list(unsigned int a[],int n) /*使用数组产生链表*/
{
NODE *p,*q,*r;
int i,j;
if(n==0) return(NULL);
r=(NODE *)malloc(sizeof(NODE));
p=r;
for(i=1;i<n;i++)
{
for(j=0;j<5;j++)
p->data[j]=a[i-1]/jiecheng(j)%10;
q=(NODE *)malloc(sizeof(NODE));
p->link=q;
p=q;
}
for(j=0;j<5;j++)
p->data[j]=a[n-1]/jiecheng(j)%10;
p->link=NULL;
return(r);
}
void *lsd_sort(p,r,d) /*最低位优先排序*/
NODE *p;
int r,d;
{
NODE *head[MAX_R],*tail[MAX_R];
int i,j,k;
time_t start,end;
start=clock();
for(i=d-1;i>=0;i--)
{
for(j=0;j<r;j++)
head[j]=NULL;
while (p!=NULL)
{
k=p->data[i];
if(head[k]==NULL)
head[k]=p;
else tail[k]->link=p;
tail[k]=p;
p=p->link;
}
p=NULL;
for(j=r-1;j>=0;j--)
if(head[j]!=NULL)
{
tail[j]->link=p;
p=head[j];
}
}
end=clock();
printf("Lsd Sort cost %.3lfs\n",(end-start)*1.0/CLOCKS_PER_SEC);
}
main()
{
int i;
unsigned int temp[SIZE];
NODE *r;
generate_num(); /*随机产生10000个数并保存在num.txt文件中*/
load_num(); /*将数据从num.txt文件中读取出来,并赋给data[]*/
copy_array(temp); /*将data[]赋给temp[],用于每一次排序*/
r=array_to_list(temp,SIZE);
lsd_sort(r,MAX_R,MAX_D);
getch();
}