|
기수정렬입니다. 임의의 레코드를 생성하여 레코드의 크기를 입력받아 데이터를 기수정렬을 이용해서 정렬시킨다음에 출력하는 소스입니다.
문제는 비주얼씨에서는 실행이되지만 볼랜드에서는 에러가 납니다.
에러메세지는 Declaration is not allowed here라는 에러메세지이구요 두군데에서 에러가 납니다. 왜 이런 에러가 날까요?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include <time.h>
#define BINS 10
#define DATA 249-DIGIT
#define DIGIT 3
clock_t a, b;
typedef struct record
{
int key[DIGIT];
int data[DATA] ;
char character[30];
struct record *link;
} record;
record *first=NULL;
record *radix_sort(record *ptr) ;
int main()
{
int i;
int num ;
record * temp = NULL ;
printf ("input number:") ;
scanf ("%d", &num) ;
srand((unsigned)time(NULL));
while (num--)
{
temp = (record *) malloc (sizeof (record)) ;
for ( i = 0; i < DIGIT; i++)
temp->key[i] =rand () % 10 ;
for (i = 0; i < 30; i++)
temp->character[i] = rand () % 26 + 'a' ;
temp->link = NULL ;
if (!first)
first = temp ;
else
{
record * m = first ;
while (m->link)
m = m->link ;
m->link = temp ;
}
}
printf ("not sorted\n") ;
a=clock();
이부분에서 에러---->record *p = first ;
while (p)
{
// printf ("%d%d%d\n", p->key[0], p->key[1], p->key[2]) ;
printf ("%d%d%d%s\n", p->key[0], p->key[1], p->key[2], p->character) ;
p = p->link ;
}
printf("\n\n");
p = radix_sort (first) ;
printf ("sorted\n") ;
b=clock();
while (p)
{
// printf ("%d%d%d\n", p->key[0], p->key[1], p->key[2]) ;
printf ("%d%d%d%s\n", p->key[0], p->key[1], p->key[2], p->character) ;
p = p->link ;
}
printf("\n");
printf("%f", ((double)(b-a)/CLK_TCK));
printf("\n");
return 0;
}
record *radix_sort(record *ptr)
{
int i,j,digit;
record *bottom[BINS], *up[BINS];
for(i=DIGIT-1; i>=0; i--)
{
for(j=0; j<BINS; j++)
bottom[j]=up[j]=NULL;
while(ptr)
{
digit=ptr->key[i];
if(!bottom[digit])
bottom[digit]=ptr;
else
up[digit]->link=ptr;
up[digit]=ptr;
ptr=ptr->link;
}
ptr=NULL;
for(j=BINS-1; j>=0; j--)
{
if(bottom[j])
{
up[j]->link=ptr;
ptr=bottom[j];
}
}
이 부분---->record * t = ptr ;
/* while (t)
{
printf ("%d%d%d, ", t->key[0], t->key[1], t->key[2]) ;
t = t->link ;
}
getchar () ;*/
printf ("\n") ;
}
return ptr;
}
|