#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>
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nMATRIX A\n");
print(a,m,n);
printf("\nMATRIX B\n");
print(b,p,q);
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);
}
while(1);
}
/* PROGRAM 4*/
/* PROGRAM TO PERFORM SORTING OF NAMES */
# include<stdio.h>
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();
}
j++;
}
i++;
}
}
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");
}
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 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>
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();
}
}
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--;
}
}
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;
}
}
# include<stdio.h>
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();
}
}
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;
}
}
# include<stdio.h>
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();
}
}
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++;
}
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--;
}
}
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;
}
}
# include<stdio.h>
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();
}
}
}
}
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;
}
}
#include<stdio.h>
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 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>
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
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 0 :exit(0);
#include<stdio.h>
}
}
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
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>
}
}
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;
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;
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 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 ;
#include <stdio.h>
if ( *root == NULL )
{
*root = (nodeptr)malloc(sizeof(node));
// traverses btree
void inorder ( nodeptr *root )
{
if ( *root != NULL )
{
inorder ( (*root) -> left) ;
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");
}
c1 = leafcount(root);
printf("\n\nTotal leaves are = %d",c1);
c2 = nonleafcount(root);
printf("\n\nTotal non-leaves are = %d",c2);
}
if( *p != NULL)
{
if((*p)->left == NULL && (*p)->right == NULL)
count++;
leafcount((*p)->left);
leafcount((*p)->right);
}
return(count);
}
if( *p != NULL)
{
if((*p)->left != NULL || (*p)->right != NULL)
count++;
nonleafcount((*p)->left);
nonleafcount((*p)->right);
}
return(count);
}
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");
case 4 : count(&root);
break;