1 /* 2 单链表实现基数排序 3 */ 4 5 /*接口头文件*/ 6 typedef int ElementType; 7 8 #ifndef _LIST_H 9 #define _LIST_H 10 #include <stdbool.h> 11 12 struct Node; 13 typedef struct Node * PtrToNode; 14 typedef PtrToNode List; 15 typedef PtrToNode Position; 16 17 /*操作集*/ 18 List MakeEmpty( List L ); 19 bool IsEmpty( List L ); 20 bool IsLast( Position P ); 21 Position First( List L ); 22 Position Header( List L ); 23 void Insert( ElementType X, List L, Position P ); 24 void DeleteList( List L ); 25 Position Advance( Position P ); 26 void RadixSortList( ElementType A[], int N, int P ); 27 void PrintfList(List L); 28 29 #endif 30 31 32 /*接口实现*/ 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include "list.h" 36 #define B 10 37 38 /*特定结构声明*/ 39 struct Node 40 { 41 ElementType Element; 42 Position Next; 43 }; 44 45 /*获取n的个位、十位、百位…具体位值由p指定。如,p = 1,取个位;p = 2,取十位。*/ 46 int GetWei( int n, int p ); 47 48 /*获取每个桶的尾部位置*/ 49 Position GetLast( List L ); 50 51 List MakeEmpty( List L ) 52 { 53 if ( L != NULL ) 54 DeleteList( L ); 55 L = ( List )malloc( sizeof( struct Node ) ); 56 if ( L == NULL ) 57 { 58 printf( "No Space!!! " ); 59 exit( 1 ); 60 } 61 L->Next = NULL; 62 63 return L; 64 } 65 66 bool IsEmpty( List L ) 67 { 68 return L->Next == NULL; 69 } 70 71 bool IsLast( Position P ) 72 { 73 return P->Next == NULL; 74 } 75 76 Position Header( List L ) 77 { 78 return L; 79 } 80 81 Position First( List L ) 82 { 83 return L->Next; 84 } 85 86 void Insert( ElementType X, List L, Position P ) 87 { 88 Position Temp; 89 Temp = ( List )malloc( sizeof( struct Node ) ); 90 if ( Temp == NULL ) 91 { 92 printf( "No Space!!! " ); 93 exit( 1 ); 94 } 95 Temp->Element = X; 96 97 Temp->Next = P->Next; 98 P->Next = Temp; 99 } 100 101 void DeleteList( List L ) 102 { 103 Position P; 104 Position Temp; 105 106 P = L->Next; 107 L->Next = NULL; //保留头结点 108 109 while ( P != NULL ) 110 { 111 Temp = P->Next; 112 free( P ); 113 P = Temp; 114 } 115 } 116 117 Position Advance( Position P ) 118 { 119 return P->Next; 120 } 121 122 void RadixSortList( ElementType A[], int N, int P ) 123 { 124 List Bucket[B]; 125 Position Temp; 126 int i, j, k, m, r; 127 128 /*根据待排序数据位数决定排序趟数*/ 129 for ( i = 1; i <= P; i++ ) 130 { 131 /*创建桶*/ 132 for ( j = 0; j < B; j++ ) 133 Bucket[j] = MakeEmpty( NULL ); 134 135 /*分配*/ 136 for ( k = 0; k < N; k++ ) 137 Insert( A[ k ], Bucket[ GetWei( A[ k ], i ) ], GetLast( Bucket[ GetWei( A[ k ], i ) ] ) ); 138 139 /*输入*/ 140 141 for ( m = 0, r = 0; m < B && r < N; m++ ) 142 { 143 Temp = First( Bucket[ m ] ); 144 while ( Temp != NULL ) 145 { 146 A[ r ] = Temp->Element; 147 r++; 148 Temp = Advance( Temp ); 149 } 150 } 151 } 152 } 153 154 Position GetLast( List L ) 155 { 156 Position P; 157 158 if ( L == NULL ) 159 { 160 printf( "No Space!!! " ); 161 exit( 1 ); 162 } 163 else if ( L->Next == NULL ) 164 { 165 166 return L; 167 } 168 else 169 { 170 P = First( L ); 171 while ( ! IsLast( P ) ) 172 P = Advance( P ); 173 174 175 return P; 176 } 177 } 178 179 int GetWei( int n, int p ) 180 { 181 int temp, result; 182 183 for ( int i = 0; i < p; i++ ) 184 { 185 result = n % 10; 186 n = n / 10; 187 } 188 189 return result; 190 } 191 192 void PrintfList(List L) 193 { 194 Position P; 195 196 if (IsEmpty(L)) 197 printf("List is empty! "); 198 else 199 { 200 P = First(L); 201 while (P != NULL) 202 { 203 printf("%4d",P->Element); 204 P = Advance(P); 205 } 206 printf(" "); 207 } 208 }