Anda di halaman 1dari 78

/* PROGRAM 1 */

/* PROGRAM TO COUNT THE NUMBER OF CHARACTERS,


WORDS AND LINES IN A TEXT ENTERED BY THE USER. */

#include <stdio.h>

#define IN 1
#define OUT 0
void main ()
{
int c, nl, nw, nc, state;
clrscr();

state = OUT;
nl = nc = nw = 0;
printf("\n\n\n\t ENTER THE TEXT THAT TERMINATES WITH A '.'
\n\n");
while ((c = getchar()) != '.')
{
++nc;
if ( c== '\n' )
nl++;
if ( c == ' ' || c == '\n' || c == '\t' )
state = OUT;
else if ( state == OUT )
{
state = IN;
nw++;
}
}
printf("\n\n\t Number of characters in the text : %d",nc);
printf("\n\n\t Number of words in the text : %d",nw);
printf("\n\n\t Number of lines in the text : %d",nl);
printf("\n\n\n\n\n\n\n\t\t Press any key to exit...");
getch();
}
/* PROGRAM 2*/
/* PROGRAM TO PERFORM LINEAR SEARCH IN AN ARRAY */

#include<stdio.h>
int search(int *a, int n, int g)
{
int i;
for(i=0;i<n;i++)
{
if(g==a[i])
return(i+1);
}
return 0;
}
void main()
{
int *a, n,i, g, k;
clrscr();
printf("\n\t Enter the size of the array : ");
scanf("%d",&n);
a=(int *)malloc(n* sizeof(int));
for(i=0;i<n;i++)
{
printf("\n\n Enter element %d : ",i+1);
scanf("%d",&a[i]);
}
printf("\n\n\t The array is:-\n\n ");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n\n\nEnter the element you want to search : ");
scanf("%d",&g);
k=search(a,n,g);
if(!k) printf("\n\nThe element %d is not in the array\n",g);
else
printf("\n\n%d found at position %d",g,k);
printf("\n\n\n\t Press any key to exit...");
getch();
}
/* PROGRAM 3 */
/* PROGRAM TO ADD AND MULTIPLY TWO MATRICES
USING DYNAMIC MEMORY ALLOCATION */

#include<stdio.h>

void create(int **a, int *m, int *n)


{

*a=(int *)malloc( *m);


for(i=0;i< *m; i++)
a[i]=(int *)malloc( (*n) * sizeof(int));
for(i=0;i< *m;i++)
{
for(j=0;j< *n; j++)
{
scanf("%d",&a[i][j]);
}
}
}

void liberate(int **a,int **b, int **c)


{
free(a);
free(b);
free(c);
}

void print(int **a,int m, int n)


{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d ",a[i][j]," ");
}
printf("\n");
}
}

void add(int **a,int **b,int **c, int m,int n, int p, int q)


{
int i,j;
if(m==p && n==q)
{
*c=(int *)malloc( m);
for(i=0;i<m;i++)
c[i]=(int *)malloc(n * sizeof(int));

for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];

printf("\n MATRIX A \n\n");


print(a,m,n);

printf("\n MATRIX B\n\n");


print(b,m,n);

printf("\nResult after the operation\n");


print(c,m,n);
}
else
printf("Can't perform the operation\n");
}

void mult(int **a,int **b,int **c, int m,int n, int p, int q)


{
int i,j,k;
if(n==p)
{
*c=(int *)malloc( m);
for(i=0;i<m;i++)
c[i]=(int *)malloc(q * sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}

printf("\nMATRIX A\n");
print(a,m,n);

printf("\nMATRIX B\n");
print(b,p,q);

printf("\nResult after the operation\n");


print(c,m,q);
}
else
printf("Can't perform the operation\n");
}

void main()
{
int **a,**b,**c;
int m,n,p,q,i,j,ch;
char v;
do
{
clrscr();
printf("\n\t\t <<< MAIN MENU >>>\n");
printf("\n\n\t 1 : Addition");
printf("\n\n\t 2 : Multiplication");
printf("\n\n\t 0 : Exit");
printf("\n\n\t\tEnter your choice : ");
ch=(getche()) - '0';
if(ch==1||ch==2)
{
printf("\n\n ENTER MATRIX 1 : \n");
create(a,&m,&n);
printf("\n\n\n ENTER MATRIX 2");
create(b,&p,&q);
}
clrscr();
switch(ch)
{
case 1 : add(a,b,c,m,n,p,q);
liberate(a,b,c);
break;
case 2 : mult(a,b,c,m,n,p,q);
liberate(a,b,c);
break;
case 0 : exit(0);

default: printf("\n\n\t Invalid Choice, try again !!!");


}
printf("\n Press any key to continue...");
getch();

}
while(1);
}
/* PROGRAM 4*/
/* PROGRAM TO PERFORM SORTING OF NAMES */

# include<stdio.h>

int strcmp(const char*,const char*);


int strcmp_c(const char*,const char*);
char tolower(char);
void swap(char**,char**);
void sort(char**,int,int);
void input(char**,int);
void output(char**,int);

void main()
{
char **s1;
int ch,n,x;
clrscr();
printf("\n\n Enter the no. of Strings : ");
scanf("%d",&n);
s1=(char**)malloc(sizeof(char)*n);
input(s1,n);
clrscr();
do
{
printf("\n\n\n 0. Case Sensitive Sort. ");
printf("\n\n 1. Case Independent Sort. ");
printf("\n\n\n Enter Choice : ");
scanf("%d",&ch);
if(ch<0||ch>1)
printf("\n\n\n\t\t Invalid Choice , try again...");
}
while(ch<0||ch>1);
printf("\n\n Before Sorting : \n\n");
output(s1,n);
sort(s1,n,ch);
printf("\n\n After Sorting : \n\n");
output(s1,n);
printf("\n\n\n\n\t\t\t Press any key to exit...");
getch();
}

void sort(char **s,int n,int ch)


{
int i=0,z,j;
while(i<n)
{
j=i+1;
while(j<n)
{
if(ch)
z=strcmp_c(s[i],s[j]);
else
z=strcmp(s[i],s[j]);
if(z>0)
swap(&(s[i]),&(s[j]));

j++;
}
i++;
}
}

int strcmp(const char*s1,const char*s2)


{
while(*s1==*s2)
{
if(*s1=='\0')
return 0;
s1++;
s2++;
}
return(*s1-*s2);
}
char tolower(char c)
{
if(c>=65&&c<=90)
return c+32;
else
return c;
}

int strcmp_c(const char*s1,const char*s2)


{
while(tolower(*s1)==tolower(*s2))
{
if(*s1=='\0')
return 0;
s1++;
s2++;
}
return(tolower(*s1)-tolower(*s2));
}

void swap(char** s1,char**s2)


{
char *t;
t=*s1;
*s1=*s2;
*s2=t;
}

void input(char**s,int n)
{
int x=0;
while(x<n)
{
printf("\n Enter String %d : ",x+1);
scanf("%s",s[x]);
x++;
}
}

void output(char**s,int n)
{
int x=0;
while(x<n)
{
printf("\n\t String %d : ",x+1);
puts(s[x]);
x++;
}
}
/* PROGRAM 5*/
/* PROGRAM TO PERFORM SPARSE POLYNOMIAL ADDITION */

# include<stdio.h>

typedef struct
{
int exp;
int coff;
}poly;

void input(poly*,int);
void output(poly*,int);
void sort(poly*,int*);
int add(poly*,int,poly*,int,poly*);

void main()
{
int n1,n2,n3;
poly *p1,*p2,*sum;
clrscr();
printf("\n\n Enter the no. of terms in polynomial 1 : ");
scanf("%d",&n1);
p1=(poly*)malloc((sizeof(poly))*n1);
input(p1,n1);
clrscr();
printf("\n\n Enter the no. of terms in Polynomial 2 : ");
scanf("%d",&n2);
p2=(poly*)malloc((sizeof(poly))*n2);
sum=(poly*)malloc((sizeof(poly))*(n1+n2));
input(p2,n2);

sort(p1,&n1);
sort(p2,&n2);
n3=add(p1,n1,p2,n2,sum);
sort(sum,&n3);
printf("\n\n 1st Polynomial : ");
output(p1,n1);
printf("\n\n 2nd Polynomial : ");
output(p2,n2);
printf("\n\n Sum polynomial : ");
output(sum,n3);
free(p1); free(p2); free(sum);
printf("\n\n\n\n\n\n\t\t\t Press any key to exit...");
getch();

void input(poly*p,int s)
{
int i=0;
while(i<s)
{
printf("\n\n Enter exponent value : ");
scanf("%d",&(p[i].exp));
printf("\n\n Enter coffecient value : ");
scanf("%d",&(p[i].coff));
i++;
}
}

void output(poly*p,int s)
{
int i=0,flag=0;
while(i<s)
{
if(p[i].coff)
{
flag=1;
if(p[i].coff>0&&i)
printf(" + ");
printf("%d",p[i].coff);
if(p[i].exp)
printf("X^%d",p[i].exp);
}
i++;
}
if(!flag)printf("0");
}

void sort(poly*p,int *n)


{
int i=0,j,k,temp;
poly te;

while(i<*n)
{
for(j=i+1;j<*n;j++)
{
if(p[j].exp<p[i].exp)
{

te=p[i];
p[i]=p[j];
p[j]=te;

}
else
{
if(p[i].exp==p[j].exp)
{
p[i].coff+=p[j].coff;
for(k=j;k<*n-1;k++)
{
p[k].coff=p[k+1].coff;
p[k].exp=p[k+1].exp;
}
(*n)--;
}
}
}
i++;
}
}

int add(poly*p1,int n1,poly*p2,int n2,poly*sum)


{

int temp,i=0,j=0,n3=0;

while(i<n1&&j<n2)
{
if(p1[i].exp==p2[j].exp)
{
sum[n3].exp=p1[i].exp;
sum[n3++].coff=p1[i].coff+p2[j].coff;
i++;
j++;
}
else
{
if(p1[i].exp>p2[j].exp)
{
sum[n3].exp=p2[j].exp;
sum[n3++].coff=p2[j].coff;
j++;
}
if(p1[i].exp<p2[j].exp)
{
sum[n3].exp=p1[i].exp;
sum[n3++].coff=p1[i].coff;
i++;
}
}
}
if(j==n2)
{
while(i<n1)
{
sum[n3].exp=p1[i].exp;
sum[n3++].coff=p1[i].coff;
i++;
}
}
else if(j<n2)
{
while(j<n2)
{
sum[n3].exp=p2[j].exp;
sum[n3++].coff=p2[j].coff;
j++;
}
}
return n3;

}
/* PROGRAM 6*/
/* PROGRAM TO PERFORM LINEAR SPARSE MATRIX ADDITION */

#include<stdio.h>

struct mat
{
int row,col,val;
};

void main()
{
int i,j,k,p,q,r,s,terms_a,terms_b;
struct mat *a,*b,*c;
clrscr();
printf("Enter the rows and columns for first matrix ");
scanf("%d%d",&p,&q);
printf("Enter the number of non-zero terms in the first matrix ");
scanf("%d",&terms_a);
a=(struct mat*)malloc((terms_a+1)*sizeof(struct mat));
printf("Enter non-zero elements in the order=>
row,column,value\n\n");
for(i=1;i<=terms_a;i++)
{
scanf("%d",&a[i].row);
scanf("%d",&a[i].col);
scanf("%d",&a[i].val);
}
printf("\n\nEnter the rows and columns for second matrix ");
scanf("%d%d",&r,&s);
if((p==r)&&(q==s))
{
printf("Enter the number of non-zero terms in the second
matrix ");
scanf("%d",&terms_b);
b=(struct mat*)malloc((terms_b+1)*sizeof(struct mat));
printf("Enter non-zero elements in the order=>
row,column,value\n\n");
for(i=1;i<=terms_b;i++)
{
scanf("%d",&b[i].row);
scanf("%d",&b[i].col);
scanf("%d",&b[i].val);
}
c=(struct mat*)malloc((terms_a+terms_b+1)*sizeof(struct
mat));

a[0].row=b[0].row=c[0].row=p;
a[0].col=b[0].col=c[0].col=q;
a[0].val=terms_a;
b[0].val=terms_b;

i=j=k=1;

while((i<=terms_a)&&(j<=terms_b))
{
if((a[i].row==b[j].row)&&(a[i].col==b[j].col))
{
c[k].row=a[i].row;
c[k].col=a[i].col;
c[k].val=a[i].val+b[j].val;
i++;
j++;
k++;
}
else
if((a[i].row<b[j].row)||
((a[i].row==b[j].row)&&(a[i].col<b[j].col)))
c[k++]=a[i++];
else
c[k++]=b[j++];
}
while(i<=terms_a)
c[k++]=a[i++];
while(j<=terms_b)
c[k++]=b[j++];

c[0].val=k-1;
printf("\n\nThe sum of the 2 matrices is :- \n\n");
for(i=1;i<=c[0].val;i++)
{
printf("%d\t",c[i].row);
printf("%d\t",c[i].col);
printf("%d\n",c[i].val);
}
free(b,c);
}
else
printf("\n\nThe given matrices can't be added");
free(a);
getch();
}
/* PROGRAM 7*/
/* PROGRAM TO PERFORM SPARSE MATRIX MULTIPLICATION*/

#include<stdio.h>
struct mat
{
int row,col,val;
};
void main()
{
int *count,*count_a,*count_b,*pos,*pos_a,*pos_b,**c;
int i,j,tmp,p,q,terms_a,rows_b,r,terms_b,ka,kb;
struct mat *a,*b,*bt;
printf("Enter the rows and columns of the matrix A ");
scanf("%d%d",&p,&q);
printf("Enter the number of non-zero terms in the matrix A ");
scanf("%d",&terms_a);
a=(struct mat*)malloc((terms_a+1)*sizeof(struct mat));
printf("Enter non-zero elements in the order=>
row,column,value\n\n");
for(i=1;i<=terms_a;i++)
{
scanf("%d",&a[i].row);
scanf("%d",&a[i].col);
scanf("%d",&a[i].val);
}
printf("\nEnter the rows and columns of the matrix B ");
scanf("%d%d",&rows_b,&r);
if(rows_b!=q)
printf("\nMultiplication not possible ");
else
{
printf("Enter the number of non-zero terms in the matrix B ");
scanf("%d",&terms_b);
b=(struct mat*)malloc((terms_b+1)*sizeof(struct mat));
printf("Enter non-zero elements in the order=>
row,column,value\n\n");
for(i=1;i<=terms_b;i++)
{
scanf("%d",&b[i].row);
scanf("%d",&b[i].col);
scanf("%d",&b[i].val);
}
bt=(struct mat*)malloc((terms_b+1)*sizeof(struct mat));
c=(int **)malloc(p*r*sizeof(int));
count=(int *)malloc((terms_b+1)*sizeof(int));
pos=(int *)malloc((terms_b+1)*sizeof(int));

a[0].row=p;
a[0].col=q;
a[0].val=terms_a;

b[0].row=q;
b[0].col=r;
b[0].val=terms_b;

//Starting transpose of B
for(i=0;i<=r;i++)
count[i]=0;
for(i=1;i<=terms_b;i++)
count[b[i].col]++;
pos[0]=1;
for(i=1;i<=r;i++)
pos[i]=pos[i-1]+count[i-1];
for(i=1;i<=terms_b;i++)
{
tmp=b[i].col;
bt[pos[tmp]].row=tmp;
bt[pos[tmp]].col=b[i].row;
bt[pos[tmp]].val=b[i].val;
pos[tmp]++;
}
bt[0].row=b[0].col;
bt[0].col=b[0].row;
bt[0].val=b[0].val;
//Transpose of B finishes

pos_a=(int *)malloc((terms_a+1)*sizeof(int));
pos_b=(int *)malloc((terms_b+1)*sizeof(int));
count_a=(int *)malloc((terms_a+1)*sizeof(int));
count_b=(int *)malloc((terms_b+1)*sizeof(int));

for(i=0;i<p;i++)
for(j=0;j<r;j++)
c[i][j]=0;

pos_a[0]=1;
for(i=0;i<p;i++)
count_a[i]=0;
for(i=1;i<=terms_a;i++)
count_a[a[i].row]++;
for(i=1;i<=p;i++)
pos_a[i]=pos_a[i-1]+count_a[i-1];

pos_b[0]=1;
for(i=0;i<r;i++)
count_b[i]=0;
for(i=1;i<=terms_b;i++)
count_b[bt[i].row]++;
for(i=1;i<=r;i++)
pos_b[i]=pos_b[i-1]+count_b[i-1];

for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
ka=pos_a[i];
kb=pos_b[j];
while((ka<pos_a[i+1])&&(kb<pos_b[j+1]))
{
if(a[ka].col==bt[kb].col)
{
c[i][j]+=(a[ka].val*bt[kb].val);
ka++;
kb++;
}
else if(a[ka].col<bt[kb].col)
ka++;
else
kb++;
}
}
}
printf("\nThe product of the 2 matrices is :-\n\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
free(count,count_a,count_b,pos,pos_a,pos_b,a,b,bt,c);
}
/* PROGRAM 8 */
/* PROGRAM TO FIND THE TRANSPOSE OF A SPARSE MATRIX */

#include<stdio.h>

struct mat
{
int row,col,val;
};

void main()
{
int *count,*pos,i,tmp,terms,rows,cols;
struct mat *a,*b;
printf("\n\n Enter no. of rows and columns in the matrix : ");
scanf("%d%d",&rows,&cols);
printf("\n\n Enter no. of non-zero terms in the matrix : ");
scanf("%d",&terms);
printf("\n\n Enter non-zero elements in the prescribed order =>\n\n
ROW\tCOLUMN\tVALUE\n\n");
a=(struct mat*)malloc((terms+1)*sizeof(struct mat));
b=(struct mat*)malloc((terms+1)*sizeof(struct mat));
count=(int *)malloc((terms+1)*sizeof(int));
pos=(int *)malloc((terms+1)*sizeof(int));
a[0].row=rows;
a[0].col=cols;
a[0].val=terms;
for(i=1;i<=terms;i++)
{
scanf("%d",&a[i].row);
scanf("%d",&a[i].col);
scanf("%d",&a[i].val);
}
for(i=0;i<=cols;i++)
count[i]=0;
for(i=1;i<=terms;i++)
count[a[i].col]++;
pos[0]=1;
for(i=1;i<=cols;i++)
pos[i]=pos[i-1]+count[i-1];
for(i=1;i<=terms;i++)
{
tmp=a[i].col;
b[pos[tmp]].row=tmp;
b[pos[tmp]].col=a[i].row;
b[pos[tmp]].val=a[i].val;
pos[tmp]++;
}
b[0].row=a[0].col;
b[0].col=a[0].row;
b[0].val=a[0].val;
printf("\n\n The transpose of the matrix in the prescribed order
is :-\n\n");
printf(" ROW COLUMN VALUE \n\n");
for(i=0;i<=terms;i++)
printf("%d \t %d \t %d \n",b[i].row,b[i].col,b[i].val);
getch();
}
/* PROGRAM 9*/
/* PROGRAM TO PERFORM OPERATIONS ON A SINGLY LINKED LIST */

# include<stdio.h>

typedef struct list* listptr;

typedef struct list


{
int data;
struct list* next;
}lnode;

void disp(listptr);
void insert(int,listptr);
void delete_pos(int,listptr);
void delete_val(int,listptr);
int search(int,listptr);

void main()
{
int ch,val;
listptr head;
listptr temp;

temp=(listptr)(malloc(sizeof(lnode)));
temp->data=0;
temp->next=NULL;
head->next=temp;

while(1)
{
clrscr();
printf("\n\n\t\t << MAIN MENU >> ");
printf("\n\n 1. Insert value.");
printf("\n\n 2. Delete Value at Position.");
printf("\n\n 3. Delete Value. ");
printf("\n\n 4. Search Value.");
printf("\n\n 0. Exit.");
printf("\n\n Enter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\n Enter data value : ");
scanf("%d",&val);
insert(val,head);
disp(head);
break;
case 2: printf("\n\n Enter Position : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_pos(val,head);
disp(head);
break;
case 3:
printf("\n\n Enter Value : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_val(val,head);
disp(head);
break;
case 4: printf("\n\n Enter Value : ");
scanf("%d",&val);
ch=search(val,head);
if(ch)
printf("\n\n %d found at position
%d .",val,ch);
else
printf("\n\n %d not in the list.",val);
break;
case 0: exit(0);
}
printf("\n\n Press any key to continue...");
getch();
}
}

void disp(listptr beg)


{
printf("\n\n No. of elements in list : %d ",beg->next->data);
printf("\n\n Linked List : ");
beg=beg->next->next;
while(beg)
{
printf("%d -> ",beg->data);
beg=beg->next;
}
printf("/");
}

void insert(int v,listptr head)


{
listptr temp,curr,prev;
temp=(listptr)malloc(sizeof(lnode));
temp->data=v;
temp->next=NULL;

prev=head->next;
curr=prev->next;

while(curr!=NULL&&curr->data<v)
{
prev=curr;
curr=curr->next;
}
prev->next=temp;
temp->next=curr;
head->next->data++;

}
void delete_pos(int i,listptr head)
{
int a=1;
listptr prev=head->next,curr=prev->next;
if(!(head->next->data))
printf("\n\n List is empty !!!");

else if(i<1||i>head->next->data)
{
printf("\n\n Invalid Position !!!");
}
else
{
while(a<i)
{
prev=curr;
curr=curr->next;
a++;
}
prev->next=curr->next;
free(curr);
printf("\n\n Deleted !!!");
head->next->data--;
}
}

void delete_val(int v,listptr head)


{
listptr prev=head->next,curr=prev->next;

while(curr!=NULL)
{
if(curr->data==v)
{
printf("\n\n %d deleted",v);
prev->next=curr->next;
free(curr);
curr=prev->next;
head->next->data--;
continue;
}
else if(curr->data>v)
break;
prev=curr;
curr=curr->next;
}
}

int search(int val,listptr head)


{
listptr temp=head->next->next;
int pos=0;
while(temp!=head)
{
pos++;
if(temp->data==val)
return pos;
temp=temp->next;
}
return 0;
}
/* PROGRAM 10*/
/* PROGRAM TO PERFORM OPERATIONS ON A DOUBLY LINK LIST */

# include<stdio.h>

typedef struct list* listptr;

typedef struct list


{
int data;
listptr next;
listptr prev;
}lnode;

void disp(listptr);
void insert(int,listptr*);
void delete_val(int,listptr*);
int search(int,listptr);

void main()
{
int ch,val;
listptr head=NULL;

while(1)
{
clrscr();
printf("\n\n\t\t << MAIN MENU >> ");
printf("\n\n 1. Insert value.");
printf("\n\n 2. Delete Value. ");
printf("\n\n 3. Search Value.");
printf("\n\n 0. Exit.");
printf("\n\n Enter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\n Enter data value : ");
scanf("%d",&val);
insert(val,&head);
disp(head);
break;
case 2:
printf("\n\n Enter Value : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_val(val,&head);
printf("\n\n After Deletion : ");
disp(head);
break;
case 3: printf("\n\n Enter Value : ");
scanf("%d",&val);
ch=search(val,head);
if(ch)
printf("\n\n %d found at position
%d .",val,ch);
else
printf("\n\n %d is not in the list.",val);
break;
case 0: exit(0);
}
printf("\n\n Press any key to continue...");
getch();
}
}

void disp(listptr beg)


{
if(!beg)
printf(" Empty");
else
{
while(beg!=NULL)
{
printf("%d -> ",beg->data);
beg=beg->next;
}
printf("/");
}
}

void insert(int v,listptr *head)


{
listptr temp,curr;
temp=(listptr)malloc(sizeof(lnode));
temp->data=v;
temp->next=NULL;
temp->prev=NULL;
if(!(*head))
*head=temp;
else if(v<(*head)->data)
{
temp->next=*head;
(*head)->prev=temp;
*head=temp;
}
else
{
curr=*head;
while(curr->next&&curr->data<=v)
curr=curr->next;
if(curr->data>v)
{
temp->next=curr;
curr->prev->next=temp;
temp->prev=curr->prev;
curr->prev=temp;
}
else
{
temp->prev=curr;
curr->next=temp;
}
}

void delete_val(int v,listptr *head)


{
listptr curr=*head,temp;

while(curr->next!=NULL)
{
if(curr->data==v)
{
temp=curr->next;
if(curr==*head)
*head=temp;
printf("\n\n %d deleted",v);
if(curr->prev)
curr->prev->next=curr->next;
if(curr->next)
curr->next->prev=curr->prev;
free(curr);
curr=temp;
continue;
}
else if(curr->data>v)
break;
curr=curr->next;
}
}

int search(int val,listptr head)


{
listptr temp=head->next->next;
int pos=0;
while(temp)
{
pos++;
if(temp->data==val)
return pos;
temp=temp->next;
}
return 0;
}
/* PROGRAM 11*/
/* PROGRAM TO PERFORM OPERATIONS ON A CIRCULAR SINGLY LINK
LIST */

# include<stdio.h>

typedef struct list* listptr;

typedef struct list


{
int data;
struct list* next;
}lnode;

void disp(listptr);
void insert(int,listptr);
void delete_pos(int,listptr);
void delete_val(int,listptr);
int search(int,listptr);

void main()
{
int ch,val;
listptr head;
listptr temp;

temp=(listptr)(malloc(sizeof(lnode)));
temp->data=0;
temp->next=head;
head->next=temp;

while(1)
{
clrscr();
printf("\n\n\t\t << MAIN MENU >> ");
printf("\n\n 1. Insert value.");
printf("\n\n 2. Delete Value at Position.");
printf("\n\n 3. Delete Value. ");
printf("\n\n 4. Search Value.");
printf("\n\n 0. Exit.");
printf("\n\n Enter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\n Enter data value : ");
scanf("%d",&val);
insert(val,head);
disp(head);
break;
case 2: printf("\n\n Enter Position : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_pos(val,head);
disp(head);
break;
case 3:
printf("\n\n Enter Value : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_val(val,head);
disp(head);
break;
case 4: printf("\n\n Enter Value : ");
scanf("%d",&val);
ch=search(val,head);
if(ch)
printf("\n\n %d found at position
%d .",val,ch);
else
printf("\n\n %d not in the list.",val);
break;
case 0: exit(0);
}
printf("\n\n Press any key to continue...");
getch();
}
}

void disp(listptr beg)


{
listptr head=beg;
printf("\n\n No. of elements in list : %d ",beg->next->data);
printf("\n\n Linked List : ");
beg=beg->next->next;
while(beg!=head)
{
printf("%d -> ",beg->data);
beg=beg->next;
}
printf("/");
}

void insert(int v,listptr head)


{
listptr temp,curr,prev;
temp=(listptr)malloc(sizeof(lnode));
temp->data=v;
temp->next=NULL;

prev=head->next;
curr=prev->next;

while(curr!=head&&curr->data<v)
{

prev=curr;
curr=curr->next;
}
prev->next=temp;
temp->next=curr;
head->next->data++;
}

void delete_pos(int i,listptr head)


{
int a=1;
listptr prev=head->next,curr=prev->next;
if(!(head->next->data))
printf("\n\n List is empty !!!");

else if(i<1||i>head->next->data)
{
printf("\n\n Invalid Position !!!");
}
else
{
while(a<i)
{
prev=curr;
curr=curr->next;
a++;
}
prev->next=curr->next;
free(curr);
printf("\n\n Deleted !!!");
head->next->data--;
}
}

void delete_val(int v,listptr head)


{
listptr prev=head->next,curr=prev->next;

while(curr!=head)
{
if(curr->data==v)
{
printf("\n\n %d deleted",v);
prev->next=curr->next;
free(curr);
curr=prev->next;
head->next->data--;
continue;
}
else if(curr->data>v)
break;
prev=curr;
curr=curr->next;
}
}

int search(int val,listptr head)


{
listptr temp=head->next->next;
int pos=0;
while(temp!=head)
{
pos++;
if(temp->data==val)
return pos;
temp=temp->next;
}
return 0;
}
/* PROGRAM 12*/
/* PROGRAM TO PERFORM OPERATIONS ON A CIRCULAR DOUBLY LINK
LIST */

# include<stdio.h>

typedef struct list* listptr;

typedef struct list


{
int data;
listptr next;
listptr prev;
}lnode;

void disp(listptr);
void insert(int,listptr*);
void delete_val(int,listptr*);
int search(int,listptr);

void main()
{
int ch,val;
listptr head=NULL;

while(1)
{
clrscr();
printf("\n\n\t\t << MAIN MENU >> ");
printf("\n\n 1. Insert value.");
printf("\n\n 2. Delete Value. ");
printf("\n\n 3. Search Value.");
printf("\n\n 0. Exit.");
printf("\n\n Enter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n\n Enter data value : ");
scanf("%d",&val);
insert(val,&head);
disp(head);
break;
case 2:
printf("\n\n Enter Value : ");
scanf("%d",&val);
printf("\n\n Before Deletion : ");
disp(head);
delete_val(val,&head);
printf("\n\n After Deletion : ");
disp(head);
break;
case 3: printf("\n\n Enter Value : ");
scanf("%d",&val);
ch=search(val,head);
if(ch)
printf("\n\n %d found at position
%d .",val,ch);
else
printf("\n\n %d is not in the list.",val);
break;
case 0: exit(0);
}
printf("\n\n Press any key to continue...");
getch();
}
}

void disp(listptr beg)


{
listptr head=beg;
if(!beg)
printf(" Empty");
else
{
do
{
printf("%d -> ",beg->data);
beg=beg->next;
}
while(beg!=head);
printf("/");
}
}

void insert(int v,listptr *head)


{
listptr temp,curr;
temp=(listptr)malloc(sizeof(lnode));
temp->data=v;
temp->next=NULL;
temp->prev=NULL;
if(!(*head))
{
temp->next=temp;
temp->prev=temp;
*head=temp;
}
else if(v<(*head)->data)
{
temp->next=*head;
temp->prev=(*head)->prev;
(*head)->prev->next=temp;
(*head)->prev=temp;
*head=temp;
}
else
{
curr=*head;
while(curr->next!=*head&&curr->data<=v)
curr=curr->next;
if(curr->data>v)
{
temp->next=curr;
curr->prev->next=temp;
temp->prev=curr->prev;
curr->prev=temp;
}
else
{
temp->prev=curr;
curr->next=temp;
temp->next=*head;
(*head)->prev=temp;

}
}

void delete_val(int v,listptr *head)


{
listptr curr=*head,temp;

while(curr->next!=*head)
{
if(curr->data==v)
{
temp=curr->next;
if(curr==*head)
*head=temp;
printf("\n\n %d deleted",v);
curr->prev->next=curr->next;
curr->next->prev=curr->prev;
free(curr);
curr=temp;
continue;
}
else if(curr->data>v)
break;
curr=curr->next;
}
}

int search(int val,listptr head)


{
listptr temp=head->next->next;
int pos=0;
while(temp)
{
pos++;
if(temp->data==val)
return pos;
temp=temp->next;
}
return 0;
}
/* PROGRAM 13 */
/* PROGRAM TO REVERSE A SINGLY LINEAR LINKED LIST */

#include<stdio.h>

typedef struct nodes * nodeptr;

typedef struct nodes


{
int data;
nodeptr next;
}node;

void insert(int *data)


{
printf("\n\n Enter the value: ");
scanf("%d",(data));
}

void display(nodeptr head)


{
while(head != NULL)
{
printf(" %d ->",head->data);
head=head->next;
}
printf(" / ");
}

void insbeg(nodeptr *head)


{
int value;
nodeptr temp;
insert(&value);
temp=(nodeptr)malloc(sizeof(node));
if(temp !=NULL)
{
temp->data=value;
temp->next = NULL;
if(*head == NULL)
*head = temp;
else
{
temp->next=(*head);
*head = temp;
}
}
printf("\n\n Link List :- \n");
display(*head);
}

void rev(nodeptr *head)


{
nodeptr ptr=NULL;
nodeptr q=*head;
while(*head!=NULL)
{
(*head)=(*head)->next;
q->next=ptr;
ptr=q;
q=*head;
}
*head=ptr;
printf("\n\n List after Reversal :- \n");
display(*head);
}

void main()
{
int val,choice;
node *head = NULL;
do
{
clrscr();
printf("\n\n\t\t<<< Main menu >>>");
printf("\n\n 1. Insertion of node.");
printf("\n\n 2. Reversing the list.");
printf("\n\n 3. Print the list");
printf("\n\n 0. Exit");
printf("\n\nEnter your choice : ");
choice=getche();
choice=choice-'0';
switch(choice)
{
case 1 : insbeg(&head);
break;
case 2 : rev(&head);
break;
case 3 : display(&head);
break;
case 0 : exit(0);
default: printf("Invalid Choice, try again !!!");
}
printf("\n\n Press any key to continue...");
getch();
}
while(1);
}
/* PROGRAM 14 */
/* PROGRAM TO IMPLEMENT STACK USING ARRAY */

#include<stdio.h>
#define MAX 20

typedef struct st
{
int arr[MAX];
int top;
}stack;

void input(int *i)


{
printf("\n\n\t Enter element to be pushed : ");
scanf("%d",i);
}

void push(stack *s)


{
int data;
if(s->top==(MAX-1))
printf("\n\n Overflow !!!");
else
{
input(&data);
s->arr[++(s->top)]=data;
printf("\n\n %d Pushed ",data);
}
}

int pop(stack *s)


{
int t;
if(s->top == -1)
{
printf("\n\n Underflow !!!");
return -1;
}
else
{
t=s->arr[s->top];
(s->top)--;
}
return t;
}

void disp(stack s)
{
printf("\nTOP->\n");
while(s.top != -1)
printf(" -> %d \n",s.arr[s.top--]);
}

void main()
{
stack s;
int c,v;
s.top=-1;
clrscr();

do
{
clrscr();
printf("\n\n\t\t !! MAIN MENU !!\n");
printf("\n1 : Push into the stack\n");
printf("\n2 : Pop from the stack\n");
printf("\n3 : Display the contents of the stack\n");
printf("\n0 : EXIT\n");
printf("\n\n\t\t Enter your choice : ");
c=getche();
c = c-'0';
printf("\n");
switch(c)
{
case 1 : push(&s);
break;
case 2 : v=pop(&s);
if(v!=-1)
printf("\nValue popped from the stack : %d",v);
break;
case 3 : disp(s);
break;
case 0 : exit(0);
default: printf("\n\n Invalid Choice, Try again !!!");
}
printf("\n\n\n\n\n\t Press any key to continue...");
getch();
}while(1);
getch();
}
/* PROGRAM 15 */
/* PROGRAM TO IM[PLEMENT STACK AS A LINKED LIST */

#include<stdio.h>

typedef struct nodes * nodeptr;


typedef struct nodes
{
int data;
nodeptr next;
}node;

void push(nodeptr *head,int x)


{
node *ptr;
ptr=(nodeptr)malloc(sizeof(node));
ptr->data=x;
ptr->next=NULL;
if(*head == NULL)
{
*head=ptr;
}
else
{
ptr->next = *head;
*head = ptr;
}
printf("\n Data Pushed is : %d",x);
}

int pop(nodeptr *head)


{
node *ptr;
int t;
if(*head==NULL)
{
printf("\nThe List is Empty\n");
return -1;
}
else
{
ptr= *head;
*head = (*head)->next;
t = ptr->data;
free(ptr);
return t;
}
}

void display(nodeptr head)


{
nodeptr np= head;
if(head == NULL)
printf("\nEmpty");
else
{
printf("\n TOP->\n");
while(head!=NULL)
{
printf("\t %d \n",head->data);
head=head->next;
}
}
}

void main()
{
node *head=NULL;
int c,x,y;
clrscr();

while(1)
{
printf("\n\n\t\t <<< Main Menu >>>\n");
printf("\n\n\t\t 1 : PUSH");
printf("\n\n\t\t 2 : POP");
printf("\n\n\t\t 3 : DISPLAY Stack");
printf("\n\n\t\t 0 : EXIT");
printf("\n\n\t\t Enter your choice : ");
c = (getche() - '0');
printf("\n\n");
switch(c)
{
case 1 : printf("\n Enter Value : ");
scanf("%d",&x);
push(&head,x);
break;
case 2 : y=pop(&head);
if(y!=-1)
printf("\n Data Popped out is : %d",y);
break;
case 3 : printf("\n The Stack is :-\n");
display(head);
break;
case 0 : exit(0);
default: printf("\nInvalid Choice, try again !!!");
}
printf("\n\n\n\n\n\n\t\t Press any key to continue...");
getch();
}
}
/* PROGRAM 16 */
/* PROGRAM TO IMPLEMENT QUEUE USING ARRAYS */

#include<stdio.h>

#define MAX 10

typedef struct qnode


{
int front;
int rear;
int arr[MAX];
}queue;

void insert(queue *qarr,int data)


{
if(qarr->rear == (MAX - 1))
printf("\n OVERFLOW !!!");
else
if(qarr->rear == -1)
qarr->front = qarr->rear = 0;
else
(qarr->rear)++;
qarr->arr[qarr->rear]=data;

void delet(queue *qarr)


{
int y;
if(qarr->front == -1)
printf("\n UNDERFLOW !!!");
else
{
if(qarr->front == qarr->rear)
qarr->front = qarr->rear = -1;
else
(qarr->front)++;
}
}

void display(queue qarr)


{
int i;
if(qarr.front==-1)
printf(" Empty ");
else
{
printf("FRONT->");

for(i=(qarr.front); i <= (qarr.rear); i++)


printf(" %d->",qarr.arr[i]);
printf(" REAR.\n");
}
}

void main()
{
queue qarr;
int data,ch;
char v;
qarr.front=qarr.rear=-1;
do
{
clrscr();
printf("\n\n\t\t <<< MAIN MENU >>>");
printf("\n\n\t 1. Insert ");
printf("\n\n\t 2. Delete ");
printf("\n\n\t 0. Exit ");
printf("\n\n\t\t Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter data to be inserted : ");
scanf("%d",&data);
insert(&qarr,data);
printf("\n\nThe queue is :-\n\n");
display(qarr);
break;

case 2 : printf("\n\nBefore Deletion queue is :-\n\n");


display(qarr);
delet(&qarr);
printf("\n\nThe new queue is :-\n\n");
display(qarr);
break;

case 0 :exit(0);

default: printf("\n Invalid Choice, try Again !!!");


}
printf("\n\n\n\n\t\t Press any key to continue...");
getch();
}
while(1);
}
/* PROGRAM 17 */
/* PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST*/

#include<stdio.h>

typedef struct nodes *nodeptr;

typedef struct nodes


{
int data;
nodeptr next;
}node;

void insert(nodeptr *front, nodeptr *rear, int x)


{
node *ptr;
ptr=(nodeptr)malloc(sizeof(node));
ptr->data=x;
ptr->next=NULL;
if(*front == NULL)
{
*front = ptr;
*rear = ptr;
}
else
{
(*rear)->next=ptr;
*rear=ptr;
}
}

void deletenode(nodeptr *front)


{
node *ptr;
if(*front == NULL)
printf("\n UNDERFLOW !!!");
else
{
ptr = *front;
*front = (*front)->next;
free(ptr);
}
}

void display(nodeptr front)


{
if(!front)
printf(" Empty ");
else
{
printf(" FRONT -> ");
while(front!=NULL)
{
printf(" %d ->",front->data);
front=front->next;
}
printf(" REAR");

}
}

void main()
{
node *front=NULL,*rear=NULL;
int i;
int info;
int ch;
char v;
do
{
clrscr();
printf("\n\n\t\t<<< MENU >>>");
printf("\n\n\t1. Insert a node ");
printf("\n\n\t2. Delete a node ");
printf("\n\n\t3. Display the queue ");
printf("\n\n\t0.EXIT \n");
printf("\n\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter Value : ");
scanf("%d",&info);
insert(&front, &rear,info);
break;
case 3 : printf("The Queue is : \n\n");
display(front);
break;
case 2 : deletenode(&front);
break;
case 0 : exit(0);
default : printf("\n\n\t Invalid Choice, try again !!!");
}
printf("\n\n\n\n\t\t Press any key to continue...");
getch();
}
while(1);
}
/* PROGRAM 18 */
/* PROGRAM TO IMPLEMENT A CIRCULAR QUEUE USING ARRAYS */

#include<stdio.h>

#define max 3

typedef struct circ


{
int arr[max];
int front;
int rear;
}
cq;

void enqueue(cq *q,int data)


{
if((q->front==0&&q->rear==max-1)||(q->front==q->rear+1))
printf("\n\n\t QUEUE IS FULL");
else
{
if(q->front==-1)
q->front=q->rear=0;
else
if(q->rear==max-1)
q->rear=0;
else
q->rear+=1;
q->arr[q->rear]=data;
}
}

void dequeue(cq *q)


{
if(q->front==-1)
printf("\n\n\t QUEUE IS EMPTY");
else
{
printf("\n\n %d deleted ",q->arr[q->front]);
if(q->front==q->rear)
q->front=q->rear=-1;
else
q->front=(q->front+1)%max;
}

void disp(cq *q)


{
int i;
printf(" FRONT ->");
for(i=(q->front); i != (q->rear) ; i = (i + 1) % max)
printf(" %d ->",q->arr[i]);
printf(" REAR\n");
}

void main()
{
int c,a;
cq q;
q.front=q.rear=-1;
do
{
clrscr();
printf("\n\n\t\t <<< MAIN MENU >>>");
printf("\n\n\t 1 : Insert into Queue");
printf("\n\n\t 2 : Delete from Queue");
printf("\n\n\t 3 : Display Queue");
printf("\n\n\t 0 : EXIT\n");
printf("\n\n\t\t Enter your choice : ");

c=getche();
c = c-'0';
switch(c)
{
case 1 : printf("\n\n\t Enter Value : ");
scanf("%d",&a);
enqueue(&q,a);
break;

case 2 : dequeue(&q);
break;
case 3 :printf("\n\n The queue is : ");
disp(&q);
break;
case 0 : exit(0);
default: printf("\n\n Invalid Choice,try again !!!");
}
printf("\n\n\n\n\t Press any key to continue...");
getch();
}
while(1);
}
/* PROGRAM 19 */
/* PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING LINKED LIST*/

#include<stdio.h>

typedef struct nodes *nodeptr;

typedef struct nodes


{
int data;
nodeptr next;
}node;

void insert(nodeptr *front, nodeptr *rear, int x)


{
node *ptr;
ptr=(nodeptr)malloc(sizeof(node));
ptr->data=x;
if(*front == NULL)
{
ptr->next=ptr;
*front = ptr;
*rear = ptr;
}
else
{
(*rear)->next=ptr;
*rear=ptr;
ptr->next=*front;
}
}

void deletenode(nodeptr *front,nodeptr *rear)


{
node *ptr;
if(*front == NULL)
printf("\n UNDERFLOW !!!");
else
{
ptr = *front;
*front = (*front)->next;
free(ptr);
if(*front==NULL)
*rear=NULL;
else
(*rear)->next=*front;
}
}

void display(nodeptr front)


{
nodeptr tmp;
if(!front)
printf(" Empty ");
else
{
printf(" FRONT -> ");
tmp=front->next;
printf(" %d -> ",front->data);
while(tmp!=front)
{
printf(" %d ->",tmp->data);
tmp=tmp->next;
}
printf(" REAR");

}
}

void main()
{
node *front=NULL,*rear=NULL;
int i;
int info;
int ch;
char v;
do
{
clrscr();
printf("\n\n\t\t<<< MENU >>>");
printf("\n\n\t1. Insert a node ");
printf("\n\n\t2. Delete a node ");
printf("\n\n\t3. Display the queue ");
printf("\n\n\t0.EXIT \n");
printf("\n\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter Value : ");
scanf("%d",&info);
insert(&front, &rear,info);
break;
case 3 : printf("The Queue is : \n\n");
display(front);
break;
case 2 : deletenode(&front,&rear);
break;
case 0 : exit(0);
default : printf("\n\n\t Invalid Choice, try again !!!");
}
printf("\n\n\n\n\t\t Press any key to continue...");
getch();
}
while(1);
}
/* PROGRAM 20 */
/* PROGRAM TO CONVERT AN INFIX EXPRESSION TO POSTFIX
EXPRESSION */

#include<stdio.h>

#define size 50

typedef struct st
{
char arr[size];
int top;
}stack;

void push(stack *s,char x)


{
s->arr[s->top]=x;
(s->top)++;
}

char pop(stack *s)


{
(s->top)--;
return(s->arr[s->top]);
}

int prec(char c)
{
if(c=='+' || c=='-')
return 1;

if(c=='*' || c=='/')
return 2;

if(c=='^' || c=='$')
return 3;
return 0;
}

void main()
{
stack s;
int i,j,length;
char temp,v;
char infix[30],output[30];
do
{
s.top=0;
i=j=0;

clrscr();
printf("\n Enter an infix expression \n\t");

fflush(stdin);
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
if(s.top==0) //means stack is empty
{
push(&s,infix[i]);
}
else //Operators or pushed or poped based on the order
of precedence
{
while( prec(infix[i]) <= prec (s.arr[(s.top)-1]) )
{
temp=pop(&s);
output[j++]=temp;
}
push(&s,infix[i]);
}
break;

case '(': push(&s,infix[i]);


break;

case ')': temp=pop(&s);


while(temp!='(')
{
output[j++]=temp;
temp=pop(&s);
}
break;
default : output[j++]=infix[i];
}
}

while(s.top != 0)
{
output[j++]=pop(&s);
}
printf("\n\n\nThe Postfix Expression is \n\t %s",output);
printf("\n\n\nDo you want to continue ?(press y for yes) : ");
v=getchar();
}while(v == 'y' || v=='Y');
getch();
}
/* PROGRAM 21 */
/* PROGRAM TO EVALUATE A POSTFIX EXPRESSION */

#include<stdio.h>

#define size 50

typedef struct st
{
int arr[size];
int top;
}stack;

void push(stack *s,int x)


{
s->arr[s->top]=x;
(s->top)++;
}

int pop(stack *s)


{
(s->top)--;
return (s->arr[s->top]);
}

void main()
{
int n1, n2, n3,i;
stack s;
char str[30],v;
do
{
s.top=0;
clrscr();
printf("\nEnter the postfix expression \n");
fflush(stdin);
gets(str);
for(i=0;str[i]!='\0';i++)
{
switch(str[i])
{
case '+' : n1 = pop ( &s ) ;
n2 = pop ( &s ) ;
n3 = n2 + n1 ;
break ;

case '-' : n1 = pop ( &s ) ;


n2 = pop ( &s ) ;
n3 = n2 - n1 ;
break ;

case '/' : n1 = pop ( &s ) ;


n2 = pop ( &s ) ;
n3 = n2 / n1 ;
break ;

case '*' : n1 = pop ( &s ) ;


n2 = pop ( &s ) ;
n3 = n2 * n1 ;
break ;

case '%' : n1 = pop(&s) ;


n2 = pop(&s) ;
n3 = n2 % n1 ;
break ;

case '$' : n1 = pop(&s) ;


n2 = pop(&s) ;
n3 = n2;
break ;

default : n3 = str[i] - '0';


}
push(&s,n3) ;
}
printf("\n The Value after evaluating the postfix expression is
%d\n",n3);
printf("\n Evaluate more postfix expressions(y/n)........");
v=getchar();
}while(v == 'y' || v=='Y');
getch();
}
/* PROGRAM 22 */
/* PROGRAM TO PERFORM OPERATIONS ON A BINARY TREE */

#include <stdio.h>

#define MAX 100

typedef struct btreenode * nodeptr;


typedef struct btreenode
{
nodeptr left;
int data ;
nodeptr right;
}node;

// inserts a new node in a binary search tree


void buildtree( nodeptr *root, int num )
{

if ( *root == NULL )
{
*root = (nodeptr)malloc(sizeof(node));

( *root ) -> left = NULL ;


( *root ) -> data = num ;
( *root ) -> right= NULL ;
return ;
}
else // search the node to which new node will be attached
{
// if new data is less, traverse to left
if ( num < ( *root )->data )
buildtree( &((*root)->left), num ) ;
else
// else traverse to right
buildtree( &((*root)->right), num ) ;
}
return ;
}

// traverses btree
void inorder ( nodeptr *root )
{
if ( *root != NULL )
{
inorder ( (*root) -> left) ;

// print the data of the node whose


// leftchild is NULL or the path
// has already been traversed
printf("\t%d",(*root)->data);

inorder ((*root) -> right) ;


}
else
return ;
}

// traverse a binary search tree in a DLR (Data-Left-right) fashion


void preorder ( nodeptr *root )
{
if ( *root != NULL )
{
// print the data of a node
printf("\t,%d",((*root)->data));
// traverse till leftchild is not NULL
preorder ( (*root) -> left) ;
// traverse till rightchild is not NULL
preorder ( (*root) -> right) ;
}
else
return ;
}
// traverse a binary search tree in LRD (Left-Right-Data) fashion
void postorder ( nodeptr *root )
{
if ( *root != NULL )
{
postorder ( (*root) -> left ) ;
postorder ( (*root) -> right) ;
printf("\t,%d", (*root) -> data) ;
}
else
return ;
}

void traverse(nodeptr *root)


{
printf("\nIn-order Traversal: ") ;
inorder (root) ;

printf("\nPre-order Traversal: ") ;


preorder (root) ;

printf("\nPost-order Traversal: ") ;


postorder ( root ) ;

printf("\nBreadth first Traversal: ") ;


level_trav ( root ) ;
}

void search(nodeptr *root,int num)


{
nodeptr p = *root;
int flag = 0;

while ( p != NULL )
{
if(p->data == num)
{
flag = 1;
break;
}
else
if(p->data < num)
p = p->right;
else
p = p->left;
}
if(flag == 0)
printf("\nNumber not found");
else
printf("\nNumber found");
}

void count(nodeptr *root)


{
int c1,c2;

c1 = leafcount(root);
printf("\n\nTotal leaves are = %d",c1);

c2 = nonleafcount(root);
printf("\n\nTotal non-leaves are = %d",c2);
}

int leafcount(nodeptr *p)


{
static int count = 0;

if( *p != NULL)
{
if((*p)->left == NULL && (*p)->right == NULL)
count++;
leafcount((*p)->left);
leafcount((*p)->right);
}
return(count);
}

int nonleafcount(nodeptr *p)


{
static int count = 0;

if( *p != NULL)
{
if((*p)->left != NULL || (*p)->right != NULL)
count++;
nonleafcount((*p)->left);
nonleafcount((*p)->right);
}
return(count);
}

int TreeDepth(nodeptr *q)


{
int left = 0;
int right = 0;
if((*q)->left != NULL)
left = TreeDepth((*q)->left); //get the depth of the left sub-tree
if((*q)->right != NULL)
right = TreeDepth((*q)->right); //get the depth of the right sub-tree
if( left > right) //check to see which sub-tree is deeper
return left+1; //return the depth of the left sub-tree plus 1
else
return right+1; //return the depth of the right sub-tree plus 1
}

int LeftDepth(nodeptr *root)


{
int leftx=0;
if((*root)->left != NULL)
leftx = TreeDepth((*root)->left); //measure the left sub-tree
return leftx;
}

int RightDepth(nodeptr *root)


{ int rightx=0;
if((*root)->right != NULL)
rightx = TreeDepth((*root)->right); //measure the right sub-tree

return rightx;
}
void main()
{ int val,choice,ch,a;
node *root = NULL;

do
{
clrscr();
printf("\n\n ! Main menu !\n");
printf("1. INSERTION in TREE\n");
printf("2. SEARCHING AN ELEMENT IN TREE\n");
printf("3. TREE TRAVERSAL\n");
printf("4. COUNT THE NUMBER OF NODES\n");
printf("5. DEPTH OF LEFT SUBTREE\n");
printf("6. DEPTH OF RIGHT SUBTREE\n");
printf("7. DEPTH OF THE TREE\n");
printf("0. Exit!!\n");

printf("\n\n Enter your choice : ");


choice=(getche()) - '0';
getch();
printf("\n");
switch(choice)
{
case 1 : printf("\n\n\n \t (enter -1 to stop insertion)\n");
do
{
printf("\n\tEnter data : ");
scanf("%d",&a);
if(a != -1)
buildtree(&root,a);
}while(a!=-1);
break;

case 2 : printf("\nEnter the number to be searched in the


tree\n");
scanf("%D",&a);
search(&root,a);
break;

case 3 : printf("\n!!!TREE TRAVERSAL!!!");


traversal(&root);
break;

case 4 : count(&root);
break;

case 5: printf("\nDepth of left subtree=


%d",LeftDepth(&root));
break;

case 6: printf("\nDepth of right subtree=


%d",RightDepth(&root));
break;
case 7: printf("\nDepth of the Tree =
%d",TreeDepth(&root));
break;
case 0 : exit(0);
default: printf("invalid choice!!Please enter a valid choice\n");
}
getch();
}while(!(choice < 0 && choice > 7));
getch();
}

Anda mungkin juga menyukai