Anda di halaman 1dari 45

DATA STRUCTURE

DATA STRUCTURE

1.MATRIX....................................................................................................................................................2
1.1.MATRIX REPRESENTATION USING 1-D ARRAY..............................................................................2
1.2.SPARSE MATRIX.................................................................................................................................4
1.3.INVERT SPARSE MATRIX...................................................................................................................7
1.4.LONG INTEGER...................................................................................................................................9
2.STACK IMPLEMENTATION.................................................................................................................11
2.1.STACK................................................................................................................................................11
2.2.DYNAMIC STACK...............................................................................................................................12
2.3.QUEUES.............................................................................................................................................13
2.4.DYNAMIC QUEUE..............................................................................................................................15
2.5.CIRCULAR QUEUE............................................................................................................................17
2.6.PRIORITY QUEUE.............................................................................................................................18
2.7.INFIX TO POSTFIX CONVERSION...................................................................................................20
2.8.POSTFIX EVAULATION.....................................................................................................................22
3.LINKED LIST IMPLEMENTATION........................................................................................................23
3.1.SINGLY LINKED LIST........................................................................................................................23
3.2.CIRCULAR LINKED LIST...................................................................................................................26
3.3.DOUBLY LINKED LIST.......................................................................................................................28
3.4.DOUBLY CIRCULAR LINKED LIST...................................................................................................28
3.5.TREE...................................................................................................................................................30
3.6.BINARY TREE....................................................................................................................................31
3.7.THREADED BINARY TREE...............................................................................................................34
4.SORTING TECHNIQUES......................................................................................................................36
4.1.BUBBLE SORT...................................................................................................................................36
4.2.SELECTION SORT.............................................................................................................................37
4.3.INSERTION SORT..............................................................................................................................38
4.4.MERGE SORT....................................................................................................................................39
4.5.HEAP SORT.......................................................................................................................................40
4.6.QUICK SORT......................................................................................................................................41
4.7.SHELL SORT......................................................................................................................................42
4.8.RADIX SORT......................................................................................................................................43

Nirender Prakash Singh

Page no:-1

DATA STRUCTURE
1. MATRIX
1.1. MATRIX REPRESENTATION USING 1-D ARRAY
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct data
{
int row,col;
int m[max];
}mat;
void init(mat **m1,int r,int c)
{
(*m1)->row=r;
(*m1)->col=c;
}
void putvalue(mat **m1,int r,int c,int value)
{
*((*m1)->m + r*(*m1)->col + c)=value;
}
int getvalue(mat **m1,int r,int c)
{
return(*((*m1)->m + r*(*m1)->col + c));
}
void input(mat *m1)
{
int i,j,value,row,col;
printf("\n Enter size of matrix [row][col] : ");
scanf("%d %d",&row,&col);
init(&m1,row,col);
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
printf("\n enter value at pos [%d][%d] : ",i,j);
scanf("%d",&value);
putvalue(&m1,i,j,value);
}
}
int check_add(mat *m1,mat *m2,mat *m3)
{
int value=1;
if (m1->row==m2->row && m1->col==m2->col)
init(&m3,m1->row,m1->col);
else
value=0;
return(value);
}
int check_multi(mat *m1,mat *m2,mat *m3)
{
int value=1;
if (m1->row==m2->col)
Nirender Prakash Singh

Page no:-2

init(&m3,m1->row,m2->col);
else
value=0;
return(value);
}
void addmat(mat *m1,mat *m2,mat *m3) //Addition
{
int i,j;
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
putvalue(&m3,i,j,(getvalue(&m1,i,j) + getvalue(&m2,i,j)));
}
void submat(mat *m1,mat *m2,mat *m3) //Subtraction
{
int i,j;
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
putvalue(&m3,i,j,(getvalue(&m1,i,j) - getvalue(&m2,i,j)));
}
void multimat(mat *m1,mat *m2,mat *m3) //Multiplication
{
int i,j,k,value;
for(k=0;k<m2->col;k++)
for(i=0;i<m1->row;i++)
{
for(j=0,value=0;j<m1->col;j++)
value +=((getvalue(&m1,k,j) * getvalue(&m2,j,i)));
putvalue(&m3,k,i,value);
}
}
void display(mat *m1)
{
int i,j;
printf("\n");
for(i=0;i<m1->row;i++)
{
for(j=0;j<m1->col;j++)
printf("%5d",getvalue(&m1,i,j));
printf("\n\n");
}
}
void main()
{
mat m1,m2,m3;
clrscr();
input(&m1);
input(&m2);
if(check_add(&m1,&m2,&m3))
{
clrscr();
addmat(&m1,&m2,&m3);
printf("\n 1st MATRIX\n");
display(&m1);
printf("\n 2nd MATRIX\n");
Nirender Prakash Singh
Page no:-3

display(&m2);
printf("\n ADDITION OF BOTH MATRIX\n");
display(&m3);
printf("\n SUBTRACTION OF BOTH MATRIX\n");
submat(&m1,&m2,&m3);
display(&m3);
}
else
{
printf("\n\n Matrix addition is not possible ...");
printf("\n Please enter correct matrix size\b");
}
if(check_multi(&m1,&m2,&m3))
{
printf("\n MULTIPACTION OF BOTH MATRIX\n");
multimat(&m1,&m2,&m3);
display(&m3);
}
getch();
}

1.2. SPARSE MATRIX


#include <stdio.h>
#include <conio.h>
#define max 100
typedef struct data
{
int r;
int c;
int v;
}data;
typedef struct spmat
{
data d[max];
int row,col,no;
}spmat;
void init(spmat **m1,int r,int c,int no)
{
(*m1)->row=r;
(*m1)->col=c;
(*m1)->no=no;
}
void putvalue(spmat **m1,int row,int col,int no,int value)
{
((*m1)->d[no].r)=row;
((*m1)->d[no].c)=col;
((*m1)->d[no].v)=value;
}
int getvalue(spmat *m1,int row,int col)
{
int i,value=0;
for(i=0;i< m1->no && (m1)->d[i].r!=row || (m1)->d[i].c!=col;i++);
if(i<m1->no)
Nirender Prakash Singh

Page no:-4

value=(m1)->d[i].v;
return(value);
}
void input(spmat *m1)
{
int i,row,col,value;
printf("\n Enter size of matrix [Row][Col] : ");
scanf("%d %d",&row,&col);
printf("\n Enter no. for no. of value to be enter : ");
scanf("%d",&value);
init(&m1,row,col,value);
printf("\n Enter only value i.e >0 or <0\n");
for(i=0;i<m1->no;i++)
{
printf("\n Enter position [Row][Col] : ");
scanf("%d %d",&row,&col);
printf("\n Enter value : ");
scanf("%d",&value);
putvalue(&m1,row,col,i,value);
}
}
void display(spmat *m1)
{
int i,j,row,col,count,value;
printf("\n Value of [%d] by [%d] matrix : \n\n",(m1)->row,(m1)->col);
for(i=0;i<(m1)->row;i++, printf("\n"))
{
for(j=0;j<(m1)->col;j++)
printf("%5d",(getvalue(m1,i,j)));
}
}
void addspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,add,pos;
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
add=getvalue(m1,i,j)+getvalue(m2,i,j);
if(add!=0)
{
putvalue(&m3,i,j,pos,add);
m3->no +=1;
pos++;
}
}
display(m3);
}
void subspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,sub,pos;
if(m1->row==m2->row &&m1->col==m2->col)
{
Nirender Prakash Singh
Page no:-5

m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
sub=getvalue(m1,i,j)-getvalue(m2,i,j);
if(sub!=0)
{
putvalue(&m3,i,j,pos,sub);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void multispmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,k,multi,pos;
if(m1->col==m2->row)
{
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(k=0;k<m2->col;k++)
for(i=0,pos=0;i<m1->row;i++)
for(j=0,multi=0;j<m1->col;j++)
{
multi +=getvalue(m1,k,j)*getvalue(m2,j,i);
if(multi!=0)
{
putvalue(&m3,k,i,pos,multi);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void main()
{
spmat m1,m2,m3;
clrscr();
input(&m1);input(&m2);
display(&m1);display(&m2);
addspmat(&m1,&m2,&m3);
subspmat(&m1,&m2,&m3);
getch();
}
Nirender Prakash Singh

Page no:-6

1.3. INVERT SPARSE MATRIX


#include <stdio.h>
#include <conio.h>
#define max 100
typedef struct data
{
int r;
int c;
int v;
}data;
typedef struct spmat
{
data d[max];
int row,col,no;
}spmat;
void init(spmat **m1,int r,int c,int no)
{
(*m1)->row=r;
(*m1)->col=c;
(*m1)->no=no;
}
void putvalue(spmat **m1,int row,int col,int no,int value)
{
((*m1)->d[no].r)=row;
((*m1)->d[no].c)=col;
((*m1)->d[no].v)=value;
}
void invert(spmat *m1,spmat *m2)
{
int i=0;
for(init(&m2,(m1)->row,(m1)->col,(m1)->no);i<(m1)->no;
putvalue(&m2,(m1)->d[i].c,(m1)->d[i].r,i,(m1)->d[i].v),i++);
}
int getvalue(spmat *m1,int row,int col)
{
int i,value=0;
for(i=0;i< m1->no && (m1)->d[i].r!=row || (m1)->d[i].c!=col;i++);
if(i<m1->no)
value=(m1)->d[i].v;
return(value);
}
void input(spmat *m1)
{
int i,row,col,value;
printf("\n Enter size of matrix [Row][Col] : ");
scanf("%d %d",&row,&col);
printf("\n Enter no. for no. of value to be enter : ");
scanf("%d",&value);
init(&m1,row,col,value);
printf("\n Enter only value i.e >0 or <0\n");
for(i=0;i<m1->no;i++)
{
printf("\n Enter position [Row][Col] : ");
scanf("%d %d",&row,&col);
Nirender Prakash Singh

Page no:-7

printf("\n Enter value : ");


scanf("%d",&value);
putvalue(&m1,row,col,i,value);
}
}
void display(spmat *m1)
{
int i,j,row,col,count,value;
printf("\n Value of [%d] by [%d] matrix : \n\n",(m1)->row,(m1)->col);
for(i=0;i<(m1)->row;i++)
{
for(j=0;j<(m1)->col;j++)
{
printf("%5d",(getvalue(m1,i,j)));
}
printf("\n");
}
}
void addspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,add,pos;
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
add=getvalue(m1,i,j)+getvalue(m2,i,j);
if(add!=0)
{
putvalue(&m3,i,j,pos,add);
m3->no +=1;
pos++;
}
}
display(m3);
}
void subspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,sub,pos;
if(m1->row==m2->row &&m1->col==m2->col)
{
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
sub=getvalue(m1,i,j)-getvalue(m2,i,j);
if(sub!=0)
{
putvalue(&m3,i,j,pos,sub);
m3->no +=1;
pos++;
}
Nirender Prakash Singh
Page no:-8

}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void multispmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,k,multi,pos;
if(m1->col==m2->row)
{
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(k=0;k<m2->col;k++)
for(i=0,pos=0;i<m1->row;i++)
for(j=0,multi=0;j<m1->col;j++)
{
multi +=getvalue(m1,k,j)*getvalue(m2,j,i);
if(multi!=0)
{
putvalue(&m3,k,i,pos,multi);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void main()
{
spmat m1,im1,m2,im2,m3,im3;
clrscr();
input(&m1);input(&m2);
invert(&m1,&im1);invert(&m2,&im2);
clrscr();
display(&im1);display(&im2);
printf("\nADDITION OF TWO MATRIX \n" );
addspmat(&im1,&im2,&im3);
getch();
}

1.4. LONG INTEGER


#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct lint
{
int v[max];
int size;
}lint;
Nirender Prakash Singh

Page no:-9

void init(lint **l)


{
(*l)->size=0;
}
void input(lint *l)
{
char ch;
int i;
init(&l);
for(i=0;(ch=getche())!=13;l->v[i]=ch-48,i++);
l->size=i;
}
void display(lint *l)
{
int i;
for(i=0;i<l->size;i++)
printf("%d",l->v[i]);
}
void add(lint l1,lint l2,lint *l3)
//Addition of too long integer
{
int k,c;
init(&l3);
if(l1.size>l2.size)
k=l1.size+1;
else
k=l2.size+1;
l3->size=k;
for(c=0;l1.size>=0 && l2.size>=0;(l1.size)--,(l2.size)--,k--)
{
l3->v[k]=((((l1.v[l1.size])+(l2.v[l2.size]))+c)%10);
c=((((l1.v[l1.size])+(l2.v[l2.size]))+c)/10);
}
if(l1.size>=0)
{
for(;l1.size>=0;(l1.size)--,k--)
{
l3->v[k]=(((l1.v[l1.size])+c)%10);
c=(((l1.v[l1.size])+c)/10);
}
}
else if(l2.size>=0)
{
for(;l2.size>=0;(l2.size)--,k--)
{
l3->v[k]=(((l2.v[l2.size])+c)%10);
c=(((l2.v[l2.size])+c)/10);
}
}
if(k>=0)
for(;k>=0;l3->v[k]=c,c /=10,k--);
}
void main()
{
lint l1,l2,l3;
clrscr();
Nirender Prakash Singh

Page no:-10

printf("\n Enter value : ");


input(&l1);
printf("\n Enter value : ");
input(&l2);
add(l1,l2,&l3);
printf("\n See value : ");
display(&l3);
getch();
}

2. STACK IMPLEMENTATION
2.1. STACK
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct stack
{
int top;
int data[max];
}stack;
int check_empty(stack s)
{
if(s.top==-1)
return(1);
return(0);
}
int check_full(stack s)
{
if(s.top<max-1)
return(0);
return(1);
}
void push(stack *s,int value)
{
(((s)->data[++((s)->top)]))=value;
}
int pop(stack *s)
{
return(((s)->data[((s)->top)--]));
}
void show_stack(stack s)
{
int i;
for(i=0;i<=s.top;i++)
printf("%3d",s.data[i]);
}
void main()
{
stack s;
int choice=5,value;
clrscr();
s.top=-1;
printf("\n\t\t STACK IMPLIMENT USING ARRAY");
Nirender Prakash Singh

Page no:-11

printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");


while(choice!=4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
if(!check_full(s))
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&s,value);
}
else
printf("\n\t\t\tSORRY STACK FULL ...");
}
else if(choice==2)
{
if(!check_empty(s))
{
pop(&s);
printf("\n Stack poped");
}
else
printf("\n\t\t\tSORRY STACK EMPTY ...");
}
else if(choice==3)
{
if(!check_empty(s))
{
printf("\n See Stack value ");
show_stack(s);
}
else
printf("\n\t\t\tSORRY STACK EMPTY ...");
}
}
}

2.2. DYNAMIC STACK


#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct stack
{
node *top;
}stack;
void init(stack *s)
{
Nirender Prakash Singh

Page no:-12

s->top=0;
}
void push(stack *s,int value) //Insertion
{
node *n;
n=(node *) malloc(sizeof(node));
n->next=s->top;
n->data=value;
s->top=n;
}
void pop(stack *s)
//Deletion
{
node *n;
n=s->top;
s->top=s->top->next;
free(n);
}
void show(node **top)
{
for(;*top;top=&(*top)->next)
printf("\t%d",(*top)->data);
}
void main()
{
stack s;
init(&s);
clrscr();
push(&s,10);
push(&s,20);
push(&s,30);
push(&s,40);
printf("\n");
show(&s.top);
getch();
printf("\n");
pop(&s);
show(&s.top);
getch();
}

2.3. QUEUES
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct queu
{
int front;
int rear;
int data[max];
}queu;
int q_empty(queu q)
{
Nirender Prakash Singh

Page no:-13

if(q.rear==q.front)
return(1);
return(0);
}
int q_full(queu q)
{
if(q.rear<max-1)
return(0);
return(1);
}
void init(queu *q)
{
q->rear=-1;
q->front=-1;
}
void push(queu *q,int value)
{
q->data[++(q->rear)]=value;
}
void pop(queu *q)
{
(q->front)++;
}
void show(queu q)
{
int i;
for(i=q.front+1;i<=q.rear;i++)
printf("%3d",q.data[i]);
}
void main()
{
queu q;
int choice=5,value;
clrscr();
init(&q);
printf("\n\t\t Queu IMPLIMENT USING ARRAY");
printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");
while(choice!=4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
if(!q_full(q))
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else
{
printf("\n Queu is full");
}
}
else if(choice==2)
Nirender Prakash Singh

Page no:-14

{
if(!q_empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
{
printf("\n Queu is Empty");
}
}
else if(choice==3)
{
if(!q_empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
{
printf("\n Queu is Empty");
}
}
}
}

2.4. DYNAMIC QUEUE


#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct queue
{
node *front,*rear;
}queue;
void init(queue *q)
{
q->front=q->rear=0;
}
int empty(queue q)
{
if(q.front==0)
return(1);
return(0);
}
void push(queue *q,int value)
{
node *n;
n=(node *)malloc(sizeof(node));
n->data=value;
n->next=0;
Nirender Prakash Singh

Page no:-15

if(q->front==0)
q->rear=q->front=n;
else
q->rear=q->rear->next=n;
}
void pop(queue *q)
{
node *n;
n=q->front;
q->front=q->front->next;
free(n);
}
void show(queue q)
{
for(;q.front;q.front=q.front->next)
printf("%4d",q.front->data);
}
void main()
{
queue q;
int choice=5,value;
clrscr();
init(&q);
printf("\n\t\t\t DYNAMIC QUEUE");
printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");
while(choice!=4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else if(choice==2)
{
if(!empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
printf("\n\t\tQueue is Empty...");
}
else if(choice==3)
{
if(!empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
printf("\n\t\tQueue is Empty...");
}
Nirender Prakash Singh

Page no:-16

}
}

2.5. CIRCULAR QUEUE


#include<stdio.h>
#include<conio.h>
#define max 10
typedef struct cqueue
{
int data[max];
int front,rear;
}cqueue;
void init(cqueue *q)
{
q->front=q->rear=-1;
}
int empty(cqueue q)
{
if(q.rear==q.front)
return(1);
return(0);
}
int full(cqueue q)
{
if((q.rear+1)%max==q.front)
return(1);
return(0);
}
void push(cqueue *q,int value)
{
q->front=(q->rear==0 && q->front==-1)?0:q->front;
q->rear=(q->rear+1)%max;
q->data[q->rear]=value;
}
void pop(cqueue *q)
{
q->front=(q->front+1)%max;
}
void show(cqueue q)
{
for(;q.front!=q.rear;q.front=(q.front+1)%max)
printf("%5d",q.data[q.front]);
printf("%5d",q.data[q.front]);
}
void main()
{
cqueue q;
int choice=5,value;
clrscr();
init(&q);
printf("\n\t\t Queu IMPLIMENT USING ARRAY");
printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");
while(choice!=4)
Nirender Prakash Singh

Page no:-17

{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
if(!full(q))
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else
{
printf("\n\t\t Queu is full");
}
}
else if(choice==2)
{
if(!empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
{
printf("\n\t\t Queu is Empty");
}
}
else if(choice==3)
{
if(!empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
{
printf("\n\t\t Queu is Empty");
}
}
}
}

2.6. PRIORITY QUEUE


#include<stdio.h>
#include<conio.h>
#define max 5
typedef struct queue
{
int a[max];
int f,r;
}pqueue;
Nirender Prakash Singh

Page no:-18

void init(pqueue *q)


{
q->r=q->f=-1;
}
int full(pqueue *q)
{
if((q->r+1)%max==q->f)
return(1);
return(0);
}
int empty(pqueue *q)
{
if(q->f==-1)
return(1);
return(0);
}
void insert(pqueue *q,int v)
{
int i;
if(q->f==-1)
{
q->f=q->r=0;
q->a[q->r]=v;
}
else
{
q->r=(q->r+1)%max;
for(i=q->r;q->a[(i-1+max)%max]<v&&i!=q->f;i=(i-1+max)%max)
q->a[i]=q->a[(i-1+max)%max];
q->a[i]=v;
}
}
int delet(pqueue *q)
{
int v;
v=q->a[q->f];
if(q->f==q->r)
q->f=q->r=-1;
else
q->f=(q->f+1)%max;
return(v);
}
void printqueue(pqueue *q)
{
int i;
printf("\n");
for(i=q->f;i!=(q->r+1)%max;i=(i+1)%max)
printf("%c",q->a[i]);
}
void cirprintstr(char *str)
{
Nirender Prakash Singh

Page no:-19

char ch;
pqueue q;
int i,c=0,l;
init(&q);
while((ch=*str)!='\0'&&!full(&q))
{
str++;
insert(&q,ch);
c++;
}
for(i=0;i<c&&!full(&q);i++)
{
l=delet(&q);
insert(&q,l);
printqueue(&q);
}
}
void main()
{
clrscr();
cirprintstr("ramu");
getch();
}

2.7. INFIX TO POSTFIX CONVERSION


#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct stack
{
int top;
char op[max];
}stack;
void init(stack *s)
{
s->top=-1;
}
int empty(stack s)
{
if(s.top==-1)
return(1);
return(0);
}
char top(stack s)
{
return(s.op[s.top]);
}
Nirender Prakash Singh

Page no:-20

void pop(stack *s)


{
s->top--;
}
void push(stack *s,char ch)
{
s->op[++s->top]=ch;
}
int prcd(char ch)
{
if(ch=='(')
return(0);
else if(ch=='-')
return(1);
else if(ch=='+')
return(2);
else if(ch=='*')
return(3);
else if(ch=='/')
return(4);
else if(ch=='^')
return(5);
return(-1);
}
void to_postfix(char str1[],char str2[])
{
int i,j=0;
stack s;
init(&s);
for(i=0;str1[i];i++)
{
if(isalpha(str1[i]))
str2[j++]=str1[i];
else
{
while(!empty(s) && prcd(top(s)) > prcd(str1[i]))
{
str2[j++]=top(s);
pop(&s);
}
push(&s,str1[i]);
}
}
while(!empty(s))
{
str2[j++]=top(s);
pop(&s);
}
str2[j]=0;
}
void main()
{
stack s;
Nirender Prakash Singh

Page no:-21

char str1[40],str2[40];
clrscr();
init(&s);
printf("\n Enter infix expression : ");
gets(str1);
to_postfix(str1,str2);
printf("\n See postfix expression : ");
puts(str2);
getch();
}

2.8. POSTFIX EVAULATION


#include<stdio.h>
#include<conio.h>
#define max 100
int camput(int op1,int op2,char ope);
int power(int base,int expo);
typedef struct stack
{
int top;
int data[max];
}stack;
void push(stack *s,int value)
{
s->data[++s->top]=value;
}
void pop(stack *s)
{
(s->top)--;
}
int camput(int op1,int op2,char ope)
{
if(ope=='+')
return(op1+op2);
else if(ope=='-')
return(op1-op2);
else if(ope=='*')
return(op1*op2);
else if(ope=='/')
return(op1/op2);
else if(ope=='^')
return(power(op1,op2));
return(0);
}
int power(int base,int expo)
{
int i,b;
for(i=1,b=base;i<expo;base *=b,i++);
return(base);
}
void main()
{
stack s;
Nirender Prakash Singh

Page no:-22

char str[50];
int op1,op2,op3,i;
clrscr();
printf("\n Enter postfix expression : ");
for(i=0;(str[i]=getchar())!='\n';i++)
{
if(isdigit(str[i]))
{
push(&s,str[i]-48);
}
else
{
op2=s.data[s.top];
pop(&s);
op1=s.data[s.top];
pop(&s);
op3=camput(op1,op2,str[i]);
push(&s,op3);
}
}
printf("\n Result : %d",op3);
getch();
}

3. LINKED LIST IMPLEMENTATION


3.1. SINGLY LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *next;
}list;
void creat(list **head,list *node)
{
for(;*head;head=&(*head)->next);
(*head)=node;
}
void display(list **head)
{
for( ;*head;head=&(*head)->next)
{
printf("\n%d",(*head)->data);
printf("\t\t%d",(*head)->next);
}
}
void ins_beg(list **head,list *node)
{
list **t;
node->next=*head;
*head=node;
Nirender Prakash Singh

Page no:-23

}
void ins_end(list **head,list *node)
{
for(;*head;head=&(*head)->next);
(*head)=node;
}
void del_npos_beg(list **head,int pos)
{
list **h;
for(h=head;pos>0;head=&(*head)->next,pos--);
*h=*head;
}
void del_npos_end(list **head,int pos)
{
list **h;
int c;
for(c=0,h=head;*head;head=&(*head)->next,c++);
for(c=c-pos;c>0;h=&(*h)->next,c--);
*h=*head;
}
void ins_pos(list **head,list *node,int pos)
{
list **h;
for(;pos>0&&*head;head=&(*head)->next,pos--)
h=head;
if(pos<1)
{
node->next=*h;
*h=node;
}
else
printf("\n Position is out of range");
}
void del_pos(list **head,int pos)
{
list **h;
for(;pos>0&&*head;head=&(*head)->next,pos--)
h=head;
if(pos<1)
*h=*head;
else
printf("\n Position is out of range");
}
void del_value(list **head,int value)
{
list **h;
for(;*head;head=&(*head)->next)
if((*head)->data==value)
*head=(*head)->next;
}
void reverse(list *head,list **h1)
Nirender Prakash Singh

Page no:-24

{
list **h2,**h3;
int n,c,i;
for(n=0,h2=h3=h1;*h1;h1=&(*h1)->next,n++);
for(i=1,h1=h2,h2=&(*h2)->next;n>1;n--,i++)
{
for(c=n;c>i;c--,h1=&(*h1)->next);
*h3=*h1;
h3=&(*h3)->next;
h1=h2;
}
*h3=head;
(*h3)->next=0;
}
void split(list **head,list **odd,list **even)
{
for(;*head;head=&(*head)->next)
{
if(((*head)->data)%2)
{
*odd=*head;
odd=&(*odd)->next;
}
else
{
*even=*head;
even=&(*even)->next;
}
}
if(((*head)->data)%2)
{
(*odd)->next=0;
}
else
{
(*even)->next=0;
}
(*odd)->next=0;
(*even)->next=0;
}
void main()
{
list *head=0,*odd=0,*even=0,*node;
int n,pos,value;
clrscr();
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(4);
node->data=n;
node->next=0;
Nirender Prakash Singh

Page no:-25

creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

//

}
display(&head);
printf("\n Enter value to be insert : ");
scanf("%d",&n);
node=(list *)malloc(4);
node->data=n;
node->next=0;
printf("\n Enter pos : ");
scanf("%d",&pos);
printf("\n Enter value : ");
scanf("%d",&value);
del_pos(&head,pos);
del_npos_beg(&head,pos);
del_npos_end(&head,pos);
del_value(&head,value);
ins_pos(&head,node,pos);
ins_beg(&head,node);
ins_end(&head,node);
reverse(head,&head);
display(&head);
/*
printf("\n Enter value to be insert : ");
scanf("%d",&n);
node=(list *)malloc(4);
node->data=n;
node->next=0;
ins_end(&head,node);
display(&head);
printf("\n Enter pos to be delete : ");
scanf("%d",&pos);
del_npos(&head,node,pos);
*/
reverse(&head);
split(&head,&odd,&even);
printf("\n");
display(&odd);
printf("\n");
display(&even);
getch();

3.2. CIRCULAR LINKED LIST


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *next;
}list;
Nirender Prakash Singh

Page no:-26

void creat(list **head,list *node)


{
list *h=0;
for(h=*head;h && (*head)->next !=h;head=&(*head)->next);
if(h)
{
(*head)->next=node;
node->next=h;
}
else
*head=node->next=node;
}
void display(list **head)
{
list *h=0;
if(*head)
{
for(h=*head;h && (*head)->next !=h;head=&(*head)->next)
printf("\n %d\t\t %u",(*head)->data,(*head)->next);
printf("\n %d\t\t %u",(*head)->data,(*head)->next);
}
else
printf("\n Circular linked list is empty ... ");
}
/*
void display(list **head)
{
list *h=0;
printf("\n\t %d\t\t %u",(*head)->data,(*head)->next);
for(h=*head,head=&(*head)->next;(*head)!=h;head=&(*head)->next)
printf("\n\t %d\t\t %u",(*head)->data,(*head)->next);
}
*/
void main()
{
list *head=0,*node;
int n,pos,value;
clrscr();
printf("\n CIRCULAR LINKED LIST ... ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(4);
node->data=n;
node->next=0;
creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
display(&head);
getch();
}
Nirender Prakash Singh
Page no:-27

3.3. DOUBLY LINKED LIST


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *pre,*next;
}list;
void creat(list **head,list *node)
{
list *h=0;
for(h=*head; *head ;h=*head,head=&(*head)->next);
node->pre=h;
*head=node;
}
void display(list **head)
{
if(*head)
{
for( ; *head ;head=&(*head)->next)
printf("\n %d\t %u \t %u",(*head)->data,(*head)->pre,(*head)->next);
}
else
printf("\n Double linked list is empty ... ");
}
void main()
{
list *head=0,*node;
int n,pos,value;
clrscr();
printf("\n CIRCULAR LINKED LIST ... ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(sizeof(list));
node->data=n;
node->pre=node->next=0;
creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
display(&head);
getch();
}

3.4. DOUBLY CIRCULAR LINKED LIST


#include<stdio.h>
Nirender Prakash Singh

Page no:-28

#include<stdlib.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *pre,*next;
}list;
void creat(list **head,list *node)
{
list *h=0,*pre=0;
for(h=pre=*head;h && (*head)->next !=h;pre=pre->next,head=&(*head)->next);
if(h)
{
(*head)->next=h->pre=node;
node->next=h;
node->pre=pre;
}
else
(*head)=node->pre=node->next=node;
}
void display(list **head)
{
list *h=0;
if(*head)
{
for(h=*head;h && (*head)->next !=h;head=&(*head)->next)
printf("\n %d\t%u\t%u",(*head)->data,(*head)->pre,(*head)->next);
printf("\n %d\t%u\t%u",(*head)->data,(*head)->pre,(*head)->next);
}
else
printf("\n Doubly Circular linked list is empty ... ");
}
void main()
{
list *head=0,*node;
int n,pos,value;
clrscr();
printf("\n DOUBLY CIRCULAR LINKED LIST ... ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(sizeof(list));
node->data=n;
node->pre=node->next=0;
creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
display(&head);
getch();
}
Nirender Prakash Singh

Page no:-29

3.5. TREE
#include<stdio.h>
#include<conio.h>
typedef struct tree
{
int data;
struct tree *left,*right;
}tree;
void init(tree *node)
{
node->left=node->right=0;
}
void maketree(tree **root,tree *node)
{
for(;*root && (*root)->data !=node->data;
root=((node->data)<((*root)->data))?&(*root)->left:&(*root)->right);
*root=node;
}
void preorder(tree **root)
{
if(*root)
{
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
preorder(&(*root)->left);
preorder(&(*root)->right);
}
}
void inorder(tree **root)
{
if(*root)
{
inorder(&(*root)->left);
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
inorder(&(*root)->right);
}
}
void postorder(tree **root)
{
if(*root)
{
postorder(&(*root)->left);
postorder(&(*root)->right);
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
}
}
Nirender Prakash Singh

Page no:-30

void main()
{
tree *root=0,*node;
int n;
clrscr();
printf("\n\t\t\t Tree Implementation \n ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(tree *)malloc(sizeof(tree));
init(node);
node->data=n;
maketree(&root,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
printf("\n\n\t\tPreorder Traversal \n");
preorder(&root);
printf("\n\n\t\tInorder Traversal \n");
inorder(&root);
printf("\n\n\t\tpostorder Traversal \n");
postorder(&root);
getch();
}

3.6. BINARY TREE


#include<conio.h>
#include<stdio.h>
#define max 10
typedef struct binarythread
{
int d,f;
struct binarythread *l;
struct binarythread *r;
}*BT,bt;
BT root;

//////preorder//////
void preorder()
{
BT t,p;
t=root;
if(!root)
{
printf("Tree does not exist ");
return;
}
do
Nirender Prakash Singh

Page no:-31

{
p=NULL;
while(t!=NULL)
{
printf("%d\t",t->d);
p=t;
t=t->l;
}
if(p!=NULL)
t=p->r;
while(p->f==1&&t!=NULL)
{
p=t;
t=t->r;
}
}while(t!=NULL);
return;
}

//////inorder traverse//////
void inorder()
{
BT t,p;
t=root;
if(!root)
{
printf("\n\tTree doesn't exist");
return;
}
do
{
p=NULL;
while(t!=NULL)
{
p=t;
t=t->l;
}
if(p!=NULL)
{
printf("%d\t",p->d);
t=p->r;
while(p->f==1&&t!=NULL)
{
printf("%d\t",t->d);
p=t;
t=t->r;
}
}
}while(p!=NULL);
return;
}
Nirender Prakash Singh

Page no:-32

void addnode(int x)
{
BT t,p;
int v;
t=root;
if(!root)
{
t=root=(BT)malloc(sizeof (bt));
t->f=0;
t->d=x;
t->l=t->r=NULL;
return;
}
while(t!=NULL)
{
p=t;
if(x<=t->d)
t=t->l;
else
{
if(t->f!=0)
break;
t=t->r;
}
}
if(x<=p->d)
{
t=(BT)malloc(sizeof (bt));
t->d=x;
t->f=1;
t->l=NULL;
p->l=t;
t->r=p;
}
else
{
t=(BT)malloc(sizeof(bt));
if(p->r==NULL)
t->f=0;
else
t->f=1;
t->d=x;
t->l=NULL;
t->r=p->r;
p->r=t;
p->f=0;
}
}

///////////////////
void main()
{
int ch,v;
clrscr();
root=NULL;
Nirender Prakash Singh

Page no:-33

clrscr();
while(1)
{clrscr();
printf("1.create \n2.Inorder\n3.Preorder\n4.Postorder\n5.Mirror\n6.Delete\n7.Exit");
printf("\n\nEnter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value to add:");
scanf("%d",&v);
addnode(v);
break;
case 2:
inorder();
break;
case 3:
preorder(root);
break;
case 7:
exit(0);
default :
printf("INVALID choice");
}
getch();
}
}

3.7. THREADED BINARY TREE


#include<conio.h>
#include<stdio.h>
typedef struct tbt
{
int d;
struct tbt *left,*right;
int lt,rt;
}tbt;
void insert_left(tbt *h,tbt *p)
{
p->right=h;
p->left=h->left;
p->rt=p->lt=1;
h->lt=0;
h->left=p;
}
void insert_right(tbt *h,tbt *p)
{
p->left=h;
p->right=h->right;
p->rt=p->lt=1;
Nirender Prakash Singh

Page no:-34

h->rt=0;
h->right=p;
}
void insert(tbt **h,tbt *p)
{
tbt *q;
int f;
if(*h==0)
{
*h=p;
p->left=p->right=0;
p->lt=p->rt=1;
}
else
{
q=*h;
f=0;
while(f!=1)
{
if(q->rt==0&&p->d>q->d)
q=q->right;
else
if(q->lt==0&&p->d<=q->d)
q=q->left;
else
f=1;
}
if(p->d>q->d)
insert_right(q,p);
else
insert_left(q,p);
}
}
tbt*in_succ(tbt *t)
{
if(t->rt==1)
return(t->right);
else
t=t->right;
while(t->lt==0)
t=t->left;
return(t);
}
void inorder(tbt *t)
{
while(t->left)
t=t->left;
while(t)
{
printf("% 5d",t->d);
t=in_succ(t);
}
}
Nirender Prakash Singh

Page no:-35

tbt* pre_succ(tbt *t)


{
if(t->lt==0)
return(t->left);
while(t->rt)
t=t->right;
return(t->right);
}
void preorder(tbt * t )
{
while(t)
{
printf("%5d",t->d);
t=pre_succ(t);
}
}
void main()
{
tbt *t=0,*node;
int d,m,a;
clrscr();
printf("\nenter node to insert/-99 to stop;-");
scanf("%d",&d);
while(d!=-99)
{
node=(tbt*)malloc(sizeof(tbt));
node->d=d;
node->left=node->right=0;
insert(&t,node);
scanf("%d",&d);
}
printf("\ntheinordertraversalis:-");
inorder(t);
printf("\nthe preordertraversalis:-");
preorder(t);
getch();
}

4. SORTING TECHNIQUES
4.1. BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void putmax(int a[],int n)
{
int t;
if(n>1)
{
if(a[0]>a[1])
{
t=a[0];
Nirender Prakash Singh

Page no:-36

a[0]=a[1];
a[1]=t;
}
putmax(a+1,n-1);
}
}
void bubblesort(int a[],int n)
{
if(n>1)
{
putmax(a,n);
bubblesort(a,n-1);
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubblesort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.2. SELECTION SORT


#include<stdio.h>
#include<conio.h>
void putmin(int a[],int n)
{
int t;
if(n>1)
{
if(a[0]>a[n-1])
{
t=a[0];
a[0]=a[n-1];
a[n-1]=t;
}
putmin(a,n-1);
}
}
void selectionsort(int a[],int n)
{
if(n>1)
{
Nirender Prakash Singh

Page no:-37

putmin(a,n);
selectionsort(a+1,n-1);
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.3. INSERTION SORT


#include<stdio.h>
#include<conio.h>
void insertvalue(int a[],int i,int v)
{
if(i>0)
{
if(a[i-1]>v)
{
a[i]=a[i-1];
insertvalue(a,i-1,v);
}
else
a[i]=v;
}
else
a[i]=v;
}
void insertsort(int a[],int i,int n)
{
if(n>i)
{
insertvalue(a,i,a[i]);
insertsort(a,i+1,n);
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
Nirender Prakash Singh

Page no:-38

printf("\n enter the no of elements");


scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertsort(a,1,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.4. MERGE SORT


#include<conio.h>
#include<stdio.h>
void merge(int a[],int m,int n)
{
int i,j,k,t[20];
for(i=0,j=m,k=0;i<m&&j<n;k++)
{
if(a[i]<a[j])
t[k]=a[i++];
else
t[k]=a[j++];
}
while(i<m)
{
t[k]=a[i];
i++;k++;
}
while(j<n)
{
t[k]=a[j];
j++;
k++;
}
i=0;
while(i<k)
a[i]=t[i];
}
void mergesort(int a[],int n)
{
if(n>0)
{
mergesort(a,n/2);
mergesort(a+n/2,n-n/2);
merge(a,n/2,n);
}
}
void main()
{
Nirender Prakash Singh

Page no:-39

int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.5. HEAP SORT


#include<stdio.h>
#include<conio.h>
#define max 100
void upadjust(int a[],int i)
{
int t;
while(i>1&&a[i]>a[i/2])
{
t=a[i];
a[i]=a[i/2];
a[i/2]=t;
i=i/2;
}
}
void create_heap(int a[])
{
int i;
i=1;
printf("\n enter the number");
while(i<=a[0])
{
scanf("%d",&a[i]);
upadjust(a,i);
i++;
}
}
void downadjust(int a[],int i)
{
int j,t,f=0;
while((i*2)<=a[0]&&f==0)
{
j=i;
i=i*2;
if(a[i]<a[i+1]&&(i+1)<=a[0])
i++;
Nirender Prakash Singh

Page no:-40

if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
f=1;
}
}
void heapsort(int a[])
{
int i,t;
i=a[0];
do{
t=a[1];
a[1]=a[a[0]];
a[a[0]]=t;
a[0]--;
downadjust(a,1);
}while(a[0]>1);
a[0]=i;
}
void display(int a[])
{
int i;
printf("\n the sorted array:-");
for(i=1;i<=a[0];i++)
printf("%5d",a[i]);
}
void main()
{
int a[max],n;
clrscr();
printf("\n enter the size of array");
scanf("%d",&a[0]);
create_heap(a);
heapsort(a);
display(a);
getch();
}

4.6. QUICK SORT


#include<conio.h>
#include<stdio.h>
int partition(int a[],int i,int j)
{
int m,v,t;
m=i;
v=a[m];
Nirender Prakash Singh

Page no:-41

do{
do{i++;}while(a[i]<v);
do{j--;}while(a[j]>v);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
a[m]=a[j];
a[j]=v;
return(j);
}
void quicksort(int a[],int i,int j)
{
int p;
if(i<j)
{
p=partition(a,i,j);
quicksort(a,i,p);
quicksort(a,p+1,j);
}
}
void main()
{
int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.7. SHELL SORT


#include<stdio.h>
#include<conio.h>
void shellsort(int a[],int n,int t[],int m)
{
int i,j,k,v,s,l;
for(i=0;i<m;i++)
{
s=t[i];
for(l=1;l<n;l++)
Nirender Prakash Singh

Page no:-42

{
v=a[l];
for(j=l-s;j>=0&&a[j]>v;j=j-s)
{
a[j+s]=a[j];
}
a[j+s]=v;
}
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the no of steps ");
scanf("%d",&d);
printf("\n enter the steps in an array");
for(j=0;j<d;j++)
scanf("%d",&t[j]);
shellsort(a,n,t,d);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

4.8. RADIX SORT


#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct queue
{
int f,r;
int a[max];
}queue;
void init(queue *q)
{
q->r=q->f=-1;
}
int full (queue *q)
{
if(q->r==max-1)
return(1);
return(0);
Nirender Prakash Singh

Page no:-43

}
int empty(queue *q)
{
if(q->f==-1)
return(1);
return(0);
}
void insert(queue *q,int v)
{
if(q->f==-1)
q->f=q->r=0;
else
q->r++;
q->a[q->r]=v;
}
int delet(queue *q)
{
int v;
v=q->a[q->f];
if(q->f==q->r)
q->f=q->r=-1;
else
q->f++;
return(v);
}
void radixsort(int a[],int n,int d)
{
queue q[10];
int k=10,i,j,l,r,c;
for(i=0;i<10;i++)
init(&q[i]);
for(j=0;j<d;j++)
{
for(l=0;l<n;l++)
{
c=(a[l]%k)/(k/10);
insert(&q[c],a[l]);
}
k=k*10;
for(i=0,r=0;i<10;i++)
{
while(!empty(&q[i]))
{
a[r]=delet(&q[i]);
r++;
}
}
}
}
void main()
Nirender Prakash Singh

Page no:-44

{
int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the max no of digits ");
scanf("%d",&d);
radixsort(a,n,d);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

Nirender Prakash Singh

Page no:-45

Anda mungkin juga menyukai