# C program of Radix Sort

## Complete C program of Radix Sort

```#include<stdio.h>
#include<stdlib.h>

struct  node
{
int info ;
}*start=NULL;

int large_dig();
int digit(int number, int k);

main()
{
struct node *tmp,*q;
int i,n,item;

printf("Enter the number of elements in the list : ");
scanf("%d", &n);

for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&item);

/*Inserting elements in the linked list*/
tmp= malloc(sizeof(struct node));
tmp->info=item;

if(start==NULL) /*Inserting first element */
start=tmp;
else
{
q=start;
}
}/*End of for*/

printf("Sorted list is :\n");

q=start;
while( q !=NULL)
{
printf("%d ", q->info);
}
printf("\n");

}/*End of main()*/

{
int i,k,dig,least_sig,most_sig;
struct node *p, *rear[10], *front[10];

least_sig=1;
most_sig=large_dig(start);

for(k=least_sig; k<=most_sig; k++)
{
/*Make all the queues empty at the beginning of each pass*/
for(i=0; i<=9 ; i++)
{
rear[i] = NULL;
front[i] = NULL ;
}

{
/*Find kth digit in the number*/
dig = digit(p->info, k);

/*Add the number to queue of dig*/
if(front[dig] == NULL)
front[dig] = p ;
else
rear[dig] = p;
}

/*Join all the queues to form the new linked list*/

i=0;
while(front[i] == NULL)
i++;
start = front[i];
while(i<9)
{
if(rear[i+1]!=NULL)
else
rear[i+1]=rear[i];
i++;
}
}

/*This function finds number of digits in the largest element of the list */
int large_dig()
{
struct node *p=start;
int large=0,ndig=0;

/*Find largest element*/
while(p != NULL)
{
if(p ->info > large)
large = p->info;
}
/*Find number of digits in largest element*/
while(large != 0)
{
ndig++;
large = large/10 ;
}
return(ndig);
} /*End of large_dig()*/

/*This function returns kth digit of a number*/
int digit(int number, int k)
{
int digit, i ;
for(i=1; i<=k; i++)
{
digit = number % 10 ;
number = number /10 ;
}
return(digit);
}/*End of digit()*/
```