/* Exp11-8.cpp */
# Include <stdio. h>
# Include <malloc. h>
# Include <string. h>
# Define MaxE 20/* Maximum number of elements in a linear table */
# Define maxr 10/* maximum value of the base */
# Define maxd 8/* maximum value of the keyword multiple */
Typedef struct Node
{
Char data [maxd];/* string defined by the keyword of the record */
Struct node * next;
} Rectype;
Void crealink (rectype * & P, char * A [], int N );
Void displink (rectype * P );
Void radixsort (rectype * & P, int R, int d)/* implement base sorting. * P is the pointer to the List table to be sorted, r is the base, and D is the number of keywords */
{
Rectype * head [maxr], * tail [maxr], * t;/* define the start and end pointers of each chain */
Int I, J, K;
For (I = D-1; I> = 0; I --)/* cycle from low to high */
{
For (j = 0; j <r; j ++)/* initialize the first and last pointers of each chain */
Head [J] = tail [J] = NULL;
While (P! = NULL)/* for each node loop in the original linked list */
{
K = p-> data [I]-'0';/* Find the K leader */
If (head [k] = NULL)/* allocate */
{
Head [k] = P;
Tail [k] = P;
}
Else
{
Tail [k]-> next = P;
Tail [k] = P;
}
P = p-> next;/* Get the next element to be sorted */
}
P = NULL;
For (j = 0; j <r; j ++)/* for each chain loop */
If (head [J]! = NULL)/* collect */
{
If (P = NULL)
{
P = head [J];
T = tail [J];
}
Else
{
T-> next = head [J];
T = tail [J];
}
}
T-> next = NULL;/* set the next field of the last node to null */
Printf ("sort by % d bits", d-i-1); displink (P );
}
}
Void crealink (rectype * & P, char * A [], int N)/* generate a linked list using the post-Insertion Method */
{
Int I;
Rectype * s, * t;
For (I = 0; I <n; I ++)
{
S = (rectype *) malloc (sizeof (rectype ));
Strcpy (S-> data, a [I]);
If (I = 0)
{
P = s;
T = s;
}
Else
{
T-> next = s;
T = s;
}
}
T-> next = NULL;
}
Void displink (rectype * P)/* output linked list */
{
While (P! = NULL)
{
Printf ("% s", p-> data );
P = p-> next;
}
Printf ("\ n ");
}
Void main ()
{
Int n = 10;
Rectype * P;
Char * A [] = {"75", "23", "98", "44", "57", "12", "29", "64 ", "38", "82 "};
Crealink (P, A, N );
Printf ("\ n ");
Printf ("Initial keyword ");
Displink (p);/* output initial keyword */
Radixsort (p, 10, 2 );
Printf ("final result ");
Displink (p);/* output the final result */
Printf ("\ n ");
}