#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
typedef PtrToNode List;
struct Node{
int value;
PtrToNode Next;
};
enum Index {Gewei=1,Shiwei=10,Baiwei=100};
typedef enum Index Index;
void Delete(PtrToNode p[]);
void PrintList(PtrToNode p[]);
void CopyNode2Arr(PtrToNode p[],int a[]);
void LoadArr2NodeList(PtrToNode p[],int a[],int length,Index nIndex);
int *Sort(PtrToNode p[],int a[],int length, int iterNum);
void LoadArr2NodeList(PtrToNode p[],int a[],int length,Index nIndex)
{
int i;
int bitNum;
PtrToNode tmpCell,yun;
for(i=0;i<length;i++){
bitNum = a[i] /nIndex % 10;
tmpCell = malloc(sizeof(struct Node));
tmpCell->value = a[i];
tmpCell->Next = NULL;
if(p[bitNum]->Next == NULL)
p[bitNum]->Next = tmpCell;
else
{
yun = p[bitNum];
while(yun->Next != NULL)
yun = yun->Next;
yun->Next = tmpCell;
}
}
}
void CopyNode2Arr(PtrToNode p[],int a[]){
PtrToNode yun;
int i=0;
int j;
for(j=0;j<10;j++){
if(p[j]->Next != NULL){
yun = p[j];
while(yun->Next != NULL){
yun = yun->Next;
a[i] = yun->value;
i++;
}
}
}
}
void PrintList(PtrToNode p[])
{
int i;
PtrToNode yun;
for (i=0;i<10;i++){
printf("%d:",i);
if(p[i]->Next != NULL){
yun = p[i];
while(yun->Next != NULL){
yun = yun->Next;
printf("%d\t",yun->value);
}
}
printf("\n");
}
printf("\n");
}
void Delete(PtrToNode p[])
{
PtrToNode yun,tmpCell;
int i;
for(i=0;i<10;i++){
yun = p[i]->Next;
p[i]->Next = NULL;
while(yun != NULL){
tmpCell = yun->Next;
free(yun);
yun = tmpCell;
}
}
}
/*
* @ length: length of a
* @ iterNum: wei num of max
*/
int *Sort(PtrToNode p[],int a[],int length, int iterNum)
{
LoadArr2NodeList(p,a,length,Gewei);
PrintList(p);
CopyNode2Arr(p,a);
Delete(p);
LoadArr2NodeList(p,a,length,Shiwei);
PrintList(p);
CopyNode2Arr(p,a);
Delete(p);
LoadArr2NodeList(p,a,length,Baiwei);
PrintList(p);
CopyNode2Arr(p,a);
Delete(p);
return a;
}
int
main(int argc, char *argv[])
{
PtrToNode p[10];
int i;
PtrToNode tmp;
int *result;
int a[12] = {2,512,34,315,256,65,108,99,11,234,78,89};
for(i=0;i<10;i++){
tmp = malloc(sizeof(PtrToNode));
tmp->Next = NULL;
p[i] = tmp;
}
result = Sort(p,a,12,3);
for(i=0;i<12;i++)
printf("%d\t",*(result+i));
printf("\n");
}
结果如下:
0:
1:11
2:2 512
3:
4:34 234
5:315 65
6:256
7:
8:108 78
9:99 89
0:2 108
1:11 512 315
2:
3:34 234
4:
5:256
6:65
7:78
8:89
9:99
0:2 11 34 65 78 89 99
1:108
2:234 256
3:315
4:
5:512
6:
7:
8:
9:
2 11 34 65 78 89 99 108 234 256 315 512