You are on page 1of 35

Program # 1

/* MENU DRIVEN PROGRAM TO IMPLEMENT LINEAR AND BINARY SEARCH */


#include <iostream.h>
#include <conio.h>
class sa
{
int a[10];
int n;
public:
void read();
void display();
int linear(int x);
int binary(int x);
void sort();
};
//READING ELEMENTS INTO THE ARRAY
void sa::read()
{
cout<<"ENTER NUMBER OF ELEMENTS\n";
cin>>n;
cout<<"ENTER ELEMENTS IN ARRAY\n";
for(int i=0;i<n;i++)
cin>>a[i];
}
//DISPLAYING ELEMENTS IN THE ARRAY
void sa::display()
{
cout<<"\n ELEMENTS IN THE ARRAY\n";
for(int i=0;i<n;i++)
cout<<"\t"<<a[i]; }
//PERFORMING LINEAR SEARCH
int sa::linear(int x)
{
int pos=1;
for(int i=0;i<n;i++)
if(a[i]==x)
{
pos=pos+i;
break;
}
return pos;
}
//PERFORMING BINARY SEARCH
int sa::binary(int x)
{
int pos=0,l=0,h=0,m=0;
l=0;h=n;
while(l<=h)
{
m=(l+h)/2;
if(a[m]==x)
{
pos=m+1;
break;
}
if(a[m]<x)
l=m+1;
else
h=m-1;

}
return pos;
}
//SORTING ARRAY TO IMPLEMENT BINARY SEARCH
void sa::sort()
{
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}}
void main()
{
int item,ch,loc=0;
sa k;
clrscr();
do
{ cout<<"\n *** MENU *** \n";
cout<<"\n~~~~~~~~~~~~~\n";
cout<<"\n1.LINEAR SEARCH\n";
cout<<"\n2.BINARY SEARCH\n";
cout<<"\n3.EXIT\n";
cout<<"\nENTER UR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
k.read();
cout<<"\nENTER ELEMENT TO FIND:";
cin>>item;
loc=k.linear(item);
if(loc==0)
cout<<"\nELEMENT NOT FOUND\n";
else
cout<<"\nELEMENT FOUND AT POSITION"<<loc;
cout<<"\n PRESS ANY KEY \n";
getch(); break;
case 2:
clrscr();
k.read();
cout<<"\nENTER ELEMENT TO FIND:";
cin>>item;
k.sort();
cout<<"\n**** SORTED ARRAY ****\n";
k.display();
loc=k.binary(item);
if(loc==0)
cout<<"\nELEMENT NOT FOUND\n";
else
cout<<"\n\nELEMENT FOUND AT POSITION "<<loc;
break;
} } while(ch==1||ch==2);
getch();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Program # 2
/* MENU DRIVEN PROGRAM TO SORT THE ARRAY IN
ASCENDING ORDER USING a)QUICK SORT b)MERGE SORT*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class data
{
int a[20],b[20];
int n,i;
public:
void
void
void
void
void
};

int read();
display();
msort(int f,int l);
merge(int f,int fe,int le);
qsort(int f,int l);
swap(int *x,int *y);

//READING ELEMENTS INTO THE ARRAY


int data::read()
{
cout<<"\n HOW MANY ELEMENTS DO YOU STORE :";
cin>>n;
cout<<"\n ENTER ELEMENTS \n";
for(int i=0;i<n;i++)
cin>>a[i];
return(n);
}
//DISPLAYING ELEMENTS IN THE ARRAY
void data::display()
{
for(int i=0;i<n;i++)
cout<<"\t"<<a[i];
}
//TO PERFORM MERGING
void data::merge(int f,int fe,int le)
{
int i,j,k,z,l;
i=f;
j=fe+1;
k=f;
do
{
if(a[i]>a[j])
{
b[k]=a[j];
j++;
k++;
}
else
{
b[k]=a[i];
i++;

k++;
}
}while((i<=fe)&&(j<=le));
if(i>fe)
for(z=j;z<=le;z++)
{
b[k]=a[z];
k++;
}
if(j>le)
for(z=i;z<=fe;z++)
{
b[k]=a[z];
k++;
}
for(z=f;z<=le;z++)
a[z]=b[z];
}
//TO PERFORM MERGE SORT
void data::msort(int f,int l)
{
int mid;
if(f<l)
{
mid=(f+l)/2;
msort(f,mid);
msort(mid+1,l);
merge(f,mid,l);
}
}
//TO PERFORM QUICK SORT
void data::qsort(int f,int l)
{
int i,j,m,pe;
i=f;
j=l+1;
m=i;
pe=a[f];
if(f<l)
{
do
{
do
i=i+1;
while(a[i]<pe);
do
j=j-1;
while(a[j]>pe);
if(i<j)
swap(&a[i],&a[j]);
}while(i<j);
swap(&a[j],&a[m]);
qsort(f,j-1);
qsort(j+1,l);
}
}
//TO PERFORM SWAP
void data::swap(int *x,int *y)

{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
void main()
{
clrscr();
data ob;
int ch;
clrscr();
do
{
cout<<"\n ***** MENU ***** \n";
cout<<"\n 1. MERGE SORT \n";
cout<<"\n2. QUICK SORT \n";
cout<<"\n3.EXIT \n";
cout<<"\nENTER YOUR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
int n=ob.read();
cout<<"\n ELEMENTS ENTERED IN THE ARRAY \n";
ob.display();
int f=0;
int l=n-1;
ob.msort(f,l);
cout<<"\n ELEMENTS AFTER PERFORMING MERGE SORT\n";
ob.display();
break;
case 2:
clrscr();
n=ob.read();
cout<<"\n ELEMENTS ENTERED IN THE ARRAY\n";
ob.display();
f=0;
l=n-1;
ob.qsort(f,l);
cout<<"\nELEMENTS AFTER PERFORMING QUICK SORT\n";
ob.display();
break;
}
}
while(ch==1||ch==2);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Program # 3
/* PROGRAM TO CREATE A LINKED LIST AND PERFORM
INSERT AND DELETE OPERATIONS */
#include<iostream.h>
#include<conio.h>
#define null 0
struct node
{
int data;
node *link;
}*first,*second;
class link
{
public:
void create();
void insert(int m);
void delet(int v);
void display();
};
void link::create()
{
struct node *f1,*f2;
int n;
cout<<"\n HOW MANY NODES DO YOU WANT ?\n";
cin>>n;
first=new(struct node);
cout<<"\n ENTER FIRST NODE:";
cin>>first->data;
f1=first;
cout<<"\n ENTER REMAINING NODES \n";
for(int i=1;i<=n-1;i++)
{
f2=new(struct node);
cin>>f2->data;
f1->link=f2;
f1=f2;
}
f1->link=null;
}
void link::insert(int n)
{
struct node *temp,*ptr,*y;
int flag=0;
temp=new(struct node);
temp->data=n;
ptr=first;
if(ptr==null)
{
first=temp;
temp->link=null;
flag=1;
}
else
if(temp->data<ptr->data)
{
temp->link=first;

first=temp;
flag=1;
}
else
{
while(ptr!=null)
{
if(temp->data>ptr->data)
{
y=ptr;
ptr=ptr->link;
flag=1;
}
else
break;
}
}
temp->link=ptr;
y->link=temp;
if(flag==1)
{
cout<<"\n ELEMENT IS SUCCEFULLY INSERTED \n";
}
else
{
cout<<"\n ELEMENT INSERTED FAILURE \n";
}
getch();
}
void link::display()
{
struct node *ptr;
ptr=first;
if(first==null)
cout<<"\n LIST IS EMPTY \n";
else
{
cout<<"\n LIST OF ELEMENTS :";
while(ptr!=null)
{
cout<<" "<<ptr->data<<" ->";
ptr=ptr->link;
}
cout<<"NULL \n";
}
}
void link::delet(int n)
{
struct node *ptr,*y;
int flag=0;
ptr=first;
if(first==null)
cout<<"\n LIST IS EMPTY \n"<<endl;
else
ptr=first;
if(ptr->data==n)
{

first=first->link;
flag=1;
}
while(ptr!=null)
{
if(ptr->data<n)
{
y=ptr;
ptr=ptr->link;
flag=1;
}
else
if(ptr->data==n)
{
flag=1;
break;
}
else
break;
}
if(flag==1)
{
cout<<"\n ELEMENT IS SUCCESFULLY DELETED \n";
y->link=ptr->link;
}
else
{
cout<<"\n ITEM IS NOT DELETED \n";
}
}
void main()
{
clrscr();
int a,b,c;
int ch;
link k;
do
{
clrscr();
cout<<"\n ****** MENU *******\n";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n 1.CREATE \n";
cout<<"\n 2.INSERT \n";
cout<<"\n 3.DISPLAY \n";
cout<<"\n 4.DELETE \n";
cout<<"\n 5.EXIT \n";
cout<<"\n ENTER YOUR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
k.create();
cout<<"\n PRESS ANY KEY \n";
getch();
case 2:
clrscr();
cout<<"\n ENTER AN ELEMENT TO INSERT:";

cin>>a;
k.insert(a);
cout<<"\n PRESS ANY KEY \n";
break;
case 3:
clrscr();
k.display();
cout<<"\n PRESS ANY KEY \n";
getch();
break;
case 4:
clrscr();
cout<<"\n ENTER AN ELEMENT TO BE DELETE:";
cin>>b;
k.delet(b);
cout<<"\n PRESS ANY KEY \n";
getch();
break;
case 5:
break;
}}
while(ch!=5);
getch();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Program # 4
/* MENU DRIVEN PROGRAM TO ADD TWO POLYNOMIALS */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#define null 0
struct node
{
int coef;
int exp;
struct node *link;
}*first,*second,*poly;
void main()
{
struct node *read(int);
void display(struct node *);
struct node *addpoly(struct node *,struct node *);
int n1,n2;
clrscr();
cout<<"\n\n ENTER NO OF TERMS OF FIRST POLYNOMIAL:";
cin>>n1;
first=read(n1);
cout<<"\n\n ENTER NO OF TERMS OF SECOND POLYNOMIAL:";
cin>>n2;
second=read(n2);
cout<<"\n\n THE FIRST POLYNOMIAL:";
display(first);
cout<<"\n\n THE SECOND POLYNOMIAL:";
display(second);
poly=addpoly(first,second);
cout<<"\n\n THE RESULTANT POLYNOMIAL:";
display(poly);
getch();
}
struct node *read(int n)
{
struct node *f1,*f2,*p;
int i,e,c;
cout<<"\n\n ENTER THE COEFFICENT AND EXPONENT:\n";
cin>>c>>e;
f1=new(struct node);
f1->coef=c;
f1->exp=e;
p=f1;
for(i=1;i<n;i++)
{
f2=new(struct node);
cin>>c>>e;
f2->coef=c;
f2->exp=e;
f1->link=f2;
f1=f2;
}

10

f1->link=null;
return p;
}
void display(struct node *p)
{
struct node *ptr;
ptr=p;
while(ptr->link!=null)
{
cout<<ptr->coef<<"x^"<<ptr->exp<<"+";
ptr=ptr->link;
}
cout<<ptr->coef<<"x^"<<ptr->exp;
}
struct node *attach(int c,int e,struct node *x)
{
struct node *temp;
temp=new(struct node);
temp->coef=c;
temp->exp=e;
temp->link=null;
x->link=temp;
x=temp;
return x;
}
struct node *addpoly(struct node *f,struct node *s)
{
struct node *attach(int ,int,struct node*);
struct node *p,*q,*d,*temp;
int x;
p=f;
q=s;
temp=new(struct node);
d=temp;
while((p!=null)&&(q!=null))
{
if(p->exp==q->exp)
{
x=p->coef+q->coef;
if(x!=0)
d=attach(x,p->exp,d);
p=p->link;
q=q->link;
}
else
{
if(p->exp<q->exp)
{
d=attach(p->coef,p->exp,d);
p=p->link;
}
else
{
d=attach(q->coef,q->exp,d);
q=q->link;
}
}

11

}
while(p!=null)
{
d=attach(p->coef,p->exp,d);
p=p->link;
}
while(q!=null)
{
d=attach(q->coef,q->exp,d);
q=q->link;
}
temp=temp->link;
return temp;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

12

Program # 5
/* MENU DRIVEN PROGRAM TO PERFORM INSERT AND
DELETE OPERATIONS IN A CIRCULAR LINKED LIST*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<iostream.h>
class clink
{
struct node
{
int data;
struct node *link;
}*head;
public:
clink()
{
head=new(struct node);
head->link=head;
}
void insert();
void delet();
void display();
};
void clink::display()
{
struct node *ptr;
ptr=head->link;
clrscr();
cout<<"\n\nDATA ITEMS IN THE LIST\n\n";
while(ptr!=head)
{
cout<<" " <<ptr->data<<"-> ";
ptr=ptr->link;
}
cout<<" NULL\n\n";
}
void clink::insert()
{
struct node *ptr,*temp;
temp=new struct node();
cout<<"\n ENTER THE NUMBER ELEMENT TO INSERT\n";
cin>>temp->data;
ptr=head;
if(head->link==head)
{
head->link=temp;
temp->link=head;
}
else
{
while(ptr->link!=head)
ptr=ptr->link;
ptr->link=temp;
temp->link=head;
}

13

}
void clink::delet()
{
struct node *ptr,*pptr;
int ele,f=1;
cout<<"\n ENTER ELEMENT TO DELETE \n";
cin>>ele;
ptr=head->link;
if(head->link==head)
cout<<" \n LINKED LIST IS EMPTY \n";
else
{
if(ptr->data==ele)
{cout<<"\nITEM FOUND AND DELETED\n";
head->link=ptr->link;}
else
while(ptr->data==ele||ptr!=head)
{
if(ptr->data==ele)
{
f=0;
pptr->link=ptr->link;
free(ptr);
break;
}
pptr=ptr;
ptr=ptr->link;
}
if(f==0)
cout<<"\n ITEM DELETED \n";
}
}
void main()
{
clink k;
int ch;
clrscr();
do
{
cout<<"\n***** MENU *****\n";
cout<<"\n1.INSERT \n";
cout<<"\n 2.DELETE \n";
cout<<"\n 3.DISPLAY \n";
cout<<"\n4.EXIT\n";
cout<<"\nENTER YOUR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:k.insert();break;
case 2:k.delet();break;
case 3:k.display();break;
}
}while(ch!=4);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

14

Program # 6
/* MENU DRIVEN PROGRAM TO PERFORM OPERATION ON STACK */
#include<iostream.h>
#include<conio.h>
#define null 0
class sa
{
private:
struct node
{
int data;
struct node *link;
}*top;
public:
sa()
{
top=null;
}
void push();
void pop();
void display();
};
void sa::push()
{
struct node *temp;
int e;
clrscr();
cout<<"\n ENTER AN ELEMENT TO PUSH:";
cin>>e;
temp=new(struct node);
temp->data=e;
if(top==null)
{
temp->link=null;
top=temp;
cout<<"\n ELEMENT IS INSERTED";
}
else
{
temp->link=top;
top=temp;
cout<<"\n ELEMENT IS INSERTED ";
}
cout<<"\n\n";
}
void sa::pop()
{
struct node *temp;
clrscr();
temp=top;
if(temp==null)
cout<<"\n STACK IS EMPTY \n";
else
{
cout<<"\n POPPED ELEMENT IS "<<temp->data;
top=top->link;

15

}
cout<<"\n\n";
}
void sa::display()
{
struct node *ptr;
clrscr();
ptr=top;
if(ptr==null)
cout<<"\n STACK IS EMPTY \n";
else
cout<<"\n STACK ELEMENTS "<<endl;
cout<<"\n-----------------\n";
while(ptr!=null)
{
cout<<ptr->data<<endl;
ptr=ptr->link;
}
cout<<"\n\n";
}
void main()
{
int ch;
sa k;
clrscr();
do
{
clrscr();
cout<<"\n **** MENU ****\n ";
cout<<"\n ~~~~~~~~~~~~~~~~~\n";
cout<<"\n 1.PUSH \n";
cout<<"\n 2.SHOW \n ";
cout<<"\n 3.POP \n";
cout<<"\n 4.EXIT \n";
cout<<"\n ENTER YOUR CHOICE:";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
k.push();
cout<<"\n PRESS ANY KEY";
getch();
break;
case 2:
clrscr();
k.display();
cout<<"\n PRESS ANY KEY";
getch();
break;
case 3:
clrscr();
k.pop();
cout<<"\n PRESS ANY KEY ";
getch();
break;
case 4:
break;
}}

16

while(ch!=4);
getch();}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Program # 7
/* MENU DRIVEN RECURSIVE PROGRAM
A)FIND FACTORIAL NUMBER
B) GENERATE FIBONACCI SEQUENCE
C)GCD OF THREE NUMBERS*/
#include<iostream.h>
#include<conio.h>
class sa
{
public:
long int fact(int n)
{
clrscr();
if((n==0)||(n==1))
return 1;
else
return (n*fact(n-1));
}
int fibo(int n)
{
if(n==1)
return 0;
else
if(n==2)
return 1;
else
return (fibo(n-1)+fibo(n-2));
}
int gcd(int a,int b)
{
clrscr();
if(a==b)
return a;
if(a<b)
return (gcd(b,a));
else
return (gcd(a-b,b));
}
};
void main()
{
clrscr();
long int l;
int ch,n,m,p,g;
sa k;
do
{
clrscr();
cout<<"\n ***** MENU ****";
cout<<"\n ~~~~~~~~~~~~~~~~\n";
cout<<"\n 1.FACTORIAL \n";
cout<<"\n 2.FIBONACCI \n";
cout<<"\n 3.GCD \n";
cout<<"\n 4.EXIT \n";

17

cout<<"\n ENTER YOUR CHOICE :";


cin>>ch;
switch(ch)
{
case 1:
clrscr();
cout<<"\n ENTER A NUMBER:";
cin>>n;
l=k.fact(n);
cout<<"\n THE FACTORIAL OF "<< n <<" is:"<<l;
cout<<"\n PRESS ANY KEY \n";
getch();
break;
case 2:
clrscr();
cout<<"\n ENTER THE NUMBER OF TERMS:";
cin>>n;
cout<<"\n THE FIBONACCI SERIES \n";
cout<<"\n----------------------\n";
for(int i=1;i<=n;i++)
cout<<k.fibo(i)<<endl;
cout<<"\n PRESS ANY KEY";
getch();
break;
case 3:
clrscr();
cout<<"\n ENTER 3 NUMBERS \n ";
cin>>m>>n>>p;
g=k.gcd(m,n);
g=k.gcd(g,p);
cout<<"\n GCD OF 3 NUMBERS ="<<g;
cout<<"\n PRESS ANY KEY ";
getch();
break;
case 4:
break;
}
}
while(ch!=4);
getch();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

18

Program # 8
/* PROGRAM TO SOLVE TOWERS OF HANOI PR0BLEM */
#include<iostream.h>
#include<conio.h>
void main()
{
unsigned int n;
char sour='L',inter='C',des='R';
clrscr();
void hanoi(unsigned int ,char,char,char);
cout<<"\n ENTER NO OF DISCS:";
cin>>n;
cout<<"\n TOWERS OF HANOI PROBLEM WITH "<<n<<" DISCS";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<endl;
hanoi(n,sour,inter,des);
getch();
}
void hanoi(unsigned int n,char left,char mid,char right)
{
if(n!=0)
{
hanoi(n-1,left,right,mid);
cout<<"MOVE DISC "<<n<<" FROM "<<left<<" TO "<<right;
cout<<endl;
hanoi(n-1,mid,left,right);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

19

Program # 9
/* MENU DRIVEN PROGRAM TO PERFORM OPERATIONS ON A
CIRCULAR QUEUE */
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
class cirque
{
struct cql
{
int data;
struct cql *link;
}*front,*rear;
public:
cirque()
{
front=new struct cql();
front->link=front;
rear=NULL;
}
public:
void enque();
void deque();
void display();
};
void cirque::enque()
{
struct cql *temp,*ptr;
temp=new struct cql();
cout<<"\n ENTER ELEMENT TO INSERT \n";
cin>>temp->data;
temp->link=front;
if(front->link==front)
{
front->link=temp;
rear->link=temp;
}
else
{
ptr=rear->link;
ptr->link=temp;
rear->link=temp;
}
}
void cirque::deque()
{
struct cql *ptr;
if(front->link==front)
cout<<"\n CIRCULAR QUE IS EMPTY \n";
else
{
ptr=front->link;
front->link=ptr->link;
cout<<"\n ITEM DELETED : "<<ptr->data;
free(ptr);
}

20

}
void cirque::display()
{
struct cql *ptr;
ptr=front->link;
cout<<"DATA ITEMS IN THE QUEUE\n";
while(ptr!=front)
{
cout<<" "<<ptr->data;
ptr=ptr->link;
}
}
void main()
{
clrscr();
cirque c;
int ch=0;
do
{
clrscr();
cout<<"\n OPERATION ON CIRCULAR QUEUE\n";
cout<<"\n -------------------------------------------------\n";
cout<<"\n1. ENQUE \n";
cout<<"\n2. DEQUE \n";
cout<<"\n3. DISPLAY \n";
cout<<"\n4. EXIT\n";
cout<<"\n ENTER YOUR CHOICE :";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
cout<<"\n";
c.enque();
cout<<"\n Press any key";
getch();
break;
case 3:
clrscr();
cout<<"\n";
c.display();
getch();
break;
case 2:
clrscr();
cout<<"\n ";
cout<<"\nITEM PRESENT IN THE CIRCULAR QUEUE\n";
c.deque();
getch();
break;
}
}while(ch<=3);
getch();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

21

Program # 10
/* MENU DRIVEN PROGRAM TO
a)FIND THE LENGTH OF A STRING
b)CONCATENATE TWO STRINGS
c)TO EXTRACT A SUBSTRING FROM A GIVEN STRING
d)FINDING AND REPLACING A STRING BY ANOTHER STRING*/
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
class str{
public:
void concat();
void read(char *x);
void display(char *x);
void extract();
void findreplace();
void length();
};
void str::read(char *x)
{
char ch;
fflush(stdin);
while((ch=getchar())!='\n')
{
*x=ch;
x++;
}
*x='\0';
}
void str::display(char *x)
{
while(*x!='\0')
{
cout<<*x;
x++;
}
}
void str::concat()
{
char *str1,*dp1,*str2,*dp2;clrscr();
str1=(char*)malloc(20 *sizeof(char));
str2=(char *)malloc(20 *sizeof(char));
dp1=str1;
dp2=str2;
cout<<"\nENTER FIRST STRING:";
read(str1);
cout<<"\nENTER SECOND STRING :";
read(str2);
//MOVING FIRST STRING POINTER TO THE END OF THE STRING
while(*dp1!='\0')
dp1++;
//CONCATENATION
while(*dp2!='\0')
{

22

*dp1=*dp2;
dp1++;
dp2++;
}*dp1='\0';
cout<<"\nCONCATENATED STRING :";
dp1=str1;
display(dp1);
}
void str::length()
{
char *str1;
int l=0;
str1=new char(20);
cout<<"\n ENTER THE STRING : \n";
read(str1);
while(*str1!='\0')
{str1++;l++;
}
cout<<"\nLENGTH OF THE STRING :"<<l;
}
void str::extract()
{
char *str1;
int pos=0,num=0,i;
str1=new char(20);
cout<<"\n ENTER STRING \n";
read(str1);
cout<<"\nENTER POSITION FROM WHERE THE SUBSTRING IS TO BE EXTRACTED:";
cin>>pos;
cout<<"\nHOW MANY CHARACTER IS TO BE EXTRACTED :";
cin>>num;
i=pos-1;
pos=1;
cout<<"\n EXTRACTED SUBSTRING IS : \n";
while(pos<=num)
{
cout<<str1[i];
i++;
pos++;
}}
void str::findreplace()
{
char *str1,*str2,*res;
int i,c,j,k=0,flag=0,l1=0,l2=0,count=0;
str1=(char *)malloc(20 *sizeof(char));
cout<<"\nENTER MAIN STRING:";
read(str1);
l1=strlen(str1);
str2=(char *)malloc(20 *sizeof(char));
cout<<"\n ENTER SUBSTRING TO FIND:";
read(str2);
l2=strlen(str2);
for(i=0;i<(l1-l2+1);i++)
{
count=0;
for(j=0;j<l2;j++)
{
if(*(str1+i+j)==*(str2+j))

23

{
count++;
if(count==l2)
{
flag=1;
cout<<"\n STRING FOUND \n";
res=new char(10);
cout<<"\n ENTER STRING TO REPLACE THE SUBSTRING :";
read(res);
for(k=0;k<l2;k++)
{
*(str1+i+k)=*(res+k);
}
}
}
}
}
if(flag==1)
{
cout<<"\n STRING AFTER REPLACING "<<str2<<" BY "<<res;
cout<<"\n";
display(str1);
}
else
cout<<"\n STRING NOT FOUND \n";
}
void main()
{
str ob;
int c=0;
clrscr();
do
{
cout<<"\n\n ****** MENU****** \n";
cout<<"----------------------------------";
cout<<"\n1.LENGTH OF THE STRING \n";
cout<<"\n2.CONCATENATE TWO STRINGS \n";
cout<<"\n3.EXTRACT SUBSTRING FROM MAIN STRING\n ";
cout<<"\n4.FIND THE SUBSTRING AND REPLACE \n";
cout<<"\n5.EXIT \n";
cout<<"\n ENTER YOUR CHOICE:";
cin>>c;
clrscr();
switch(c)
{
case 1:
ob.length();
break;
case 2: ob.concat();
break;
case 3: ob.extract();
break;
case 4: ob.findreplace();
break;
case 5:
break;
}
} while(c>=1&&c<=4);
cout<<"\n Press any key to quit......";

24

getch();}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Program # 11
/* PROGRAM TO CONVERT INFIX EXPRESSION TO POSTFIX */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
class sa
{
struct stack
{
char op[50];
int top;
}s;
char in[50],post[50],inchar;
int len,i,j,r;
int rank(char x);
int precedence(char x);
void input();
public:
void convert();
sa()
{
s.top=-1;
}
};
int sa::rank(char x)
{
return(x=='+'||x=='-'||x=='*'||x=='/'||x=='^')?-1:1;
}
int sa::precedence(char x)
{
if(x=='^')
return 3;
else
if(x=='*'||x=='/')
return 2;
else
if(x=='+'||x=='-')
return 1;
return 0;
}
void sa::input()
{
cout<<"\n ENTER THE INFIX EXPRESSION :\n";
cin>>in;
len=strlen(in);
}
void sa::convert()
{
input();
for(i=0,j=0;i<len;i++)
{
inchar=in[i];
r+=rank(inchar);
if(isalpha(inchar))
{

25

post[j++]=inchar;
}
else
if(inchar=='(')
{
s.op[++s.top]=inchar;
}
else
if(inchar==')')
{
while(s.op[s.top]!='(')
{
post[j++]=s.op[s.top--];
}
s.top--;
}
else
if(s.top==-1)
{
s.op[++s.top]=inchar;
}
else
{
while(precedence(s.op[s.top])>=precedence(inchar))
{
post[j++]=s.op[s.top--];
}
s.op[++s.top]=inchar;
}
}
for(;s.top>=0;s.top--)
{
post[j++]=s.op[s.top];
}
post[j]='\0';
cout<<"\n THE POST EXPRESSION IS:"<<post;
getch();
}
void main()
{
clrscr();
sa k;
k.convert();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

26

Program # 12
/* PROGRAM TO EVALUATE POSTFIX EXPRESSION */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#define max 20
int top=-1;
int s[max];
void main()
{
int rank=0,i,l,val;
char str[max];
int t1,t2,t3;
int pop();
void push(int val);
clrscr();
cout<<"\n EVALUATION OF POSTFIX EXPRESSION \n";
cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n ENTER A POSFIX EXPRESION : ";
cin>>str;
l=strlen(str);
for(i=0;i<l;i++)
{
if((str[i]=='+')||(str[i]=='-')||(str[i]=='*')||(str[i]=='/')||
(str[i]=='^'))
{
rank--;
}
else
rank++;
if(rank!=1)
{
cout<<"\n INVALID EXPRESION \n";
}
for(i=0;i<l;i++)
{
if((str[i]!='+')&&(str[i]!='-')&&(str[i]!='*')&&(str[i]!='/')&&(str[i]!
='^'))
{
cout<<"\n THE VALUE OF "<<str[i]<<" :";
cin>>val;
push(val);
}
else
{
t2=pop();
if(top==-1)
{
cout<<"\n INVALID EXPRESION \n";
}
t1=pop();
switch(str[i])
{
case '+':
t3=t1+t2;

27

break;
case '-':
t3=t1-t2;
break;
case '*':
t3=t1*t2;
break;
case '/':
t3=t1/t2;
break;
case '^':
t3=pow(t2,t1);
break;
}
push(t3);
}
}
}
t3=pop();
cout<<"\n THE VALUE OF POSTFIX EXPRESION: "<<t3;
getch();
}
void push(int val)
{
if(top>=max)
{
cout<<"\n STACK OVERFLOW \n";
}
else
{
top++;
s[top]=val;
}
}
pop()
{
int val;
if(top==-1)
{
cout<<"\n STACK UNDERFLOW \n";
}
else
{
val=s[top];
top--;
}
return val;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

28

Program # 14
/* MENU DRIVEN PROGRAM TO CREATE BINARY SEARCH TREE AND
PERFORM TREE TRAVERSALS */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define null 0
class tree
{
private:
struct node
{
node *left;
int data;
node *right;
}*p;
public:
tree()
{
p=null;
}
void call();
void insert(node **,int);
void inorder(node **);
void preorder(node **);
void postorder(node **);
};
void tree::call()
{
clrscr();
int n,d;
cout<<"\n ENTER THE NUMBER OF NODES: \n";
cin>>n;
cout<<"\n ENTER DATA (numeric data) :";
for(int i=1;i<=n;i++)
{
cin>>d;
insert(&p,d);
}
while(1){
int ch;
clrscr();
cout<<"\n *** TREE TRAVERSAL **** \n";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n 1.INORDER \n";
cout<<"\n 2.PREORDER \n";
cout<<"\n 3.POSTORDER \n";
cout<<"\n 4.EXIT \n";
cout<<"\n ENTER YOUR CHOICE:";
cin>>ch;
switch(ch){
case 1:
clrscr();
cout<<"\n ***INORDER***\n";
cout<<"\n ---------------\n";
inorder(&p);

29

cout<<"\n PRESS ANY KEY ";getch();


break;
case 2:
clrscr();
cout<<"\n ***PREORDER***\n";
cout<<"\n ---------------\n";
preorder(&p);
cout<<"\n PRESS ANY KEY ";getch();
break;
case 3:
clrscr();
cout<<"\n ****POSTORDER***\n";
cout<<"\n -----------------\n";
postorder(&p);
cout<<"\n PRESS ANY KEY ";getch();
break;
case 4:
exit(1);}}}
void tree::inorder(node **t)
{
if((*t)!=null)
{
inorder(&(*t)->left);
cout<<(*t)->data<<endl;
inorder(&(*t)->right);
}}
void tree::preorder(node **t)
{
if((*t)!=null)
{
cout<<(*t)->data<<endl;
preorder(&(*t)->left);
preorder(&(*t)->right);
}}
void tree::postorder(node **t)
{
if((*t)!=null){
postorder(&(*t)->left);
postorder(&(*t)->right);
cout<<(*t)->data<<endl;
}}
void tree::insert(node **t,int data)
{
if((*t)==null)
{
(*t)=new node;
(*t)->left=null;
(*t)->right=null;
(*t)->data=data;
}
else
if(data<(*t)->data)
insert(&(*t)->left,data);
else
insert(&(*t)->right,data);
}
void main(){
tree k;
k.call();}

30

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

31

Program # 15
/* PROGRAM TO SORT N ELEMENTS IN ASCENDING ORDER USING
HEAP SORT */
#include<iostream.h>
#include<conio.h>
int a[20],b[20];
void main()
{
int i,x;
clrscr();
void hcreate(int);
void hsort(int);
cout<<"\n HOW MANY ELEMENTS TO STORE:";
cin>>x;
hcreate(x);
cout<<"\n ELEMENTS AS INSERTED IN HEAP \n";
cout<<"\ -----------------------------\n";
for(i=1;i<=x;i++)
cout<<a[i]<<"\n";
cout<<"\n";
hsort(x);
cout<<"\n ELEMENTS AS IN SORTED ORDER \n";
cout<<"\n------------------------------\n";
for(i=1;i<=x;i++)
cout<<b[i]<<"\n";
getch();
}
void hcreate(int n)
{
int i,item,par,ptr;
cout<<"\n ENTER ELEMENTS ONE BY ONE \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
item=a[i];
ptr=i;
while(ptr>1)
{
par=ptr/2;
if(item<=a[par])
{
a[ptr]=item;
break;
}
else{
a[ptr]=a[par];
ptr=par;
}}
if(ptr==1)
a[1]=item;
}}
void hsort(int n)
{
int i,ptr,left,right,last;
for(i=n;i>0;i--)
{
b[i]=a[1];

32

last=a[i];
a[i]=0;
ptr=1;
left=2;right=3;
while(right<=i)
{
if(last>=a[left]&&last>=a[right])
{
a[ptr]=last;
ptr=i;
}
else
if(a[right]<=a[left])
{
a[ptr]=a[left];
ptr=left;
}
else{
a[ptr]=a[right];
ptr=right;
}
left=2*ptr;
right=left;
}
a[ptr]=last;}}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

33

Program # 16
/* PATH MATRIX
*/
#include <iostream.h>
#include <conio.h>
# define MAX 10
class graph
{
int a[MAX][MAX], i,j,n;
public :void read();
void findpath();
void print();
};
void graph::read()
{
cout<<"\n ENTER THE NUMBER OF VERTICES:\n";
cin>>n;
cout<<"\n Enter the Matrix:\n";
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
cin>>a[i][j];
}
}
void graph::print()
{
cout<<"\n The Matrix is entered";
for(i=0;i<n;i++)
cout<<"\tv"<<i+1;
{
cout<<"\nv"<<i+1;
for (j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}
}
void graph::findpath()
{
int i,j,k;
cout<<"\n\n";
for (k=0;k<n;k++)
for (j=0;j<n;j++)
a[i][j]=a[i][j] ||(a[i][k] && a[k][j]);
cout<<"\t";
for (i=0;i<n;i++)
cout<<"\tv"<<i+1;
for (i=0;i<n;i++)
{cout<<"\nv"<<i+1;
for (j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}}
void main()
{
clrscr();
graph ob;
ob.read();
ob.print();
ob.findpath();
cout<<"\n\n\nPress any key to continue...";

34

getch();
}

35

You might also like