Friday 31 May 2013

Important c program part3



Program of Radix Sort

#include <stdio.h>
#include <conio.h>

void radixsort( int a[], int n )
{
int * p, *r, i, j, k, l, q, x;

r = ( int * ) malloc( n * sizeof( int ) );
p = ( int * ) malloc( n * sizeof( int ) );


for( i = 0; i < 4; i++ )
{
for( j = 0; j < n; j++ )
{
x = a[j];

for( k = 0; k <= i; k++ )
{
q = x / 10;
*( r + j ) = x % 10;
x = q;
}
}

for( j = 0, l = 0; j < 10; j++ )
for( k = 0; k < n; k++ )
if( *( r + k) == j )
*(p + l++ ) = a[k];

for( j = 0; j < n; j++ )
a[j] = *( p + j );

}

free( p );
free( r );
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

radixsort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
free( item );

}

............................................................................................................................


Program of Selection Sort

#include <stdio.h>
#include <conio.h>

void selectionsort( int item[], int n )
{
int large, index, pass, count;

for( pass = n - 1; pass > 0; pass-- )
{
large = item[0];
index = 0;

for( count = 1; count <= pass; count++ )
if( item[count] > large )
{
large = item[count];
index = count;
}

item[index] = item[pass];
item[pass] = large;
}
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

selectionsort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}

..............................................................................................................................
Program of Shell sort

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

void shellsort( int a[], int n )
{
int incr[3] = { 5, 3, 1 };
int i, j, k, x, span, totincr = 3;

for( i = 0; i < totincr; i++ )
{
span = incr[i];

for( j = span; j < n; j++ )
{
x = a[j];

for( k = j - span; k >= 0 && x < a[k]; k -= span )
a[k+span] = a[k];
a[k+span] = x;
}
}
}

void main()
{
int *item, n, i;

clrscr();
printf("Enter total number of items: ");
scanf("%d",&n);

item = ( int * ) malloc( n * sizeof( int ) );

printf("Enter the items: ");
for( i = 0; i < n; i++ )
scanf("%d", (item+i) );

shellsort( item, n );

printf("\nThe sorted list => ");
for( i = 0; i < n; i++ )
printf("%d ", *(item+i) );

getch();
}

............................................................................................................................

Program to create a linked list and insert some numbers into
   the linked list such a manner the linked list become a list of numbers
   in ascending order.

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

struct Node
{
int item;
struct Node *next;
};

typedef struct Node *Nodeptr;

Nodeptr getnode()
{
return( (Nodeptr) malloc( sizeof( struct Node ) ) );
}

Nodeptr push( Nodeptr top, int x )
{
Nodeptr temp;
temp = getnode();
if( temp == NULL )
printf("The memory is full.\n");
else
{
temp->item = x;
temp->next = top;
top = temp;
}
return( top );
}

void insafter(  Nodeptr p, int x )
{
Nodeptr temp;
if( p == NULL )
{
printf("Invalid insertion.\n");
return;
}
temp = getnode();
temp->item = x;
temp->next = p->next;
p->next = temp;
}

Nodeptr sort( Nodeptr list, int x )
{
Nodeptr p1, p2;

p1 = NULL;

for( p2 = list; p2 != NULL && x > p2->item; p2 = p2->next )
p1 = p2;

if( p1 == NULL )
list = push(p2, x );
else
insafter( p1, x );
return( list );
}

void display( Nodeptr temp )
{
if( temp != NULL )
{
printf("%d -> ",temp->item );
display( temp->next );
}
else
{
printf("END.");
}
}

void main()
{
Nodeptr list;
int x;

list = NULL;
clrscr();

printf("\nInserting in the linked list:-\n"
  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
  "Enter the numbers to insert ( at end type -999 ): ");
while( 1 )
{
scanf("%d",&x);
if( x == -999 )
break;

list = sort( list, x );
}

printf("\nDisplaying the sorted linked list:-"
  "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  "\nThe linked list: ");
display( list );

getch();
}

...................................................................................................................................
Stack Program

#include <stdio.h>
#include <conio.h>

#define  MAX  6

struct Stack
{
int item[MAX];
int top;
};

int empty( struct Stack *s )
{
/* If stack is empty then return 1 and if not empty then return 0 */
return( (s->top == -1 ) ? 1 : 0 );
}

void push( struct Stack *s, int x )

{

/* s is a pointer variable of struct Stack. it contains two member- item which is linear array 
contains the elements of the stack and top which contains the location of top element of stack.
MAX gives the number of items that can be held by the stack. Initially the top = -1. This algoritham
pushes x into the stack. */
/* Check whether the stack is full or not. */
if( s->top == MAX - 1 )
{
printf("OVERFLOW. The stack is full.\n");
return;
}

/* increament the top by 1 and pushe x into the top position of the stack. */

s->item[ ++s->top ] = x;
}

int pop( struct Stack *s )
{
/* s is a pointer variable of struct Stack. it contains two member- item which is linear array 
contains the elements of the stack nad top which contains the location of top element of stack.
MAX gives the number of items that can be held by the stack. Initially the top = -1. This algorithm 
poped te top item from the stack. */

/* cheak whether the stack is
if( empty( s ) )
{
printf("UNDERFLOW.The stack is empty.\n");
return( -999 );
}

return( s->item[ s->top-- ] );
}




0 comments:

Post a Comment