Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structures Programs Using C++::: 1) Stacks Using Arrays (MSTACK - CPP)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

:: Data Structures Programs Using C++ ::

1) Stacks using arrays (MSTACK.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>
class stack
{
T *s;
int top,size;
public: stack(int);
~stack();
void push();
void pop();
void display();
};
template<class T>
stack<T>::stack(int k)
{
size=k;
s=new T[size];
top=0;
}
template<class T>
stack<T>::~stack()
{
delete s;
}
template<class T>
void stack<T>::push()
{
if(top==size)
cout<<"Stack Overflow!"<<endl;
else
{
T x;
cout<<"Enter the data value: ";
cin>>x;
s[top]=x;
top++;
}
}
template<class T>
void stack<T>::pop()
{
if(top==0)
cout<<endl<<"Stack Underflow";
else
{
top--;
cout<<"\nThe pop element is: "<<s[top];

K S V KRISHNA SRIKANTH Page 1


}
}
template<class T>
void stack<T>::display()
{
if(top==0)
cout<<endl<<"Stack is Empty!";
else
{
cout<<endl<<"The stack elements are: ";
for(int i=0;i<top;i++)
{
cout<<s[i]<<" ";
}
}
}

void main()
{
int m;
clrscr();
cout<<endl<<"Enter the size of array: ";
cin>>m;
stack <int> t1(m);
while(1)
{
int ch;
cout<<endl<<"*****STACK MENU*****"<<endl;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;
cout<<"Enter Choice:";
cin>>ch;
switch(ch)
{
case 1:t1.push();
break;
case 2:t1.pop();
break;
case 3:t1.display();
break;
case 4:exit(0);
break;
}
}
}

2) Stacks using linked lists (MLSTACK.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>

K S V KRISHNA SRIKANTH Page 2


class lstack
{
typedef struct node
{
int data;
struct node *link;
}s;
s *head,*tail,*p,*k;
public: lstack();
void push();
void pop();
void display();
};
template<class T>
lstack<T>::lstack()
{
head=tail=NULL;
}
template<class T>
void lstack<T>::push()
{
T x;
p=new s;
cout<<endl<<"Enter data value:";
cin>>x;
p->data=x;
p->link=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->link=p;
tail=p;
}
}
template<class T>
void lstack<T>::pop()
{
if(head==NULL)
cout<<endl<<"Stack Underflow";
else
{
if(head->link!=NULL)
{
p=head;
while(p->link!=NULL)
{
k=p;
p=p->link;
}
tail=k;
tail->link=NULL;
cout<<endl<<"The pop element is "<<p->data;
delete p;

K S V KRISHNA SRIKANTH Page 3


}
else
{
p=head;
head=tail=NULL;
cout<<endl<<"The pop element is "<<p->data;
delete p;
}
}
}
template<class T>
void lstack<T>::display()
{
if(head==NULL)
cout<<endl<<"Stack Empty";
else
{
cout<<endl<<"The Linked Stack elements are: ";
for(p=head;p!=NULL;p=p->link)
cout<<p->data<<" ";
}
}
void main()
{
int ch,n;
clrscr();
lstack <int> t;
while(1)
{
cout<<endl<<"*****LINKED STACK MENU*****"<<endl;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;
cout<<"Enter Choice:";
cin>>ch;
switch(ch)
{
case 1:t.push();
break;
case 2:t.pop();
break;
case 3:t.display();
break;
case 4:exit(0); break;
}
}
}

3) Queues using linked lists (QUEUE.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>
K S V KRISHNA SRIKANTH Page 4
template<class T>
class lqueue
{
typedef struct node
{
int data;
struct node *link;
}s;
s *head,*tail,*p,*k;
public: lqueue();
void insertion();
void deletion();
void display();
};
template<class T>
lqueue<T>::lqueue()
{
head=tail=NULL;
}
template<class T>
void lqueue<T>::insertion()
{
T x;
p=new s;
cout<<"Enter data value:";
cin>>x;
cout<<endl;
p->data=x;
p->link=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->link=p;
tail=p;
}
}
template<class T>
void lqueue<T>::deletion()
{
if(head==NULL)
cout<<"Queue Underflow"<<endl;
else
{
p=head;
head=head->link;
cout<<"The deleted element is "<<p->data;
delete p;
}
cout<<endl;
}
template<class T>
void lqueue<T>::display()
{

K S V KRISHNA SRIKANTH Page 5


if(head==NULL)
cout<<"Queue Empty"<<endl;
else
{
cout<<"The Linked Queue elements are ";
for(p=head;p!=NULL;p=p->link)
cout<<p->data<<" ";
cout<<endl;
}
}
void main()
{
lqueue <int> t1;
clrscr();
while(1)
{
int ch;
cout<<"****LINKED QUEUE MENU****"<<endl;
cout<<"1. Insertion"<<endl;
cout<<"2. Deletion"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Enter choice:";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1: t1.insertion(); break;
case 2: t1.deletion(); break;
case 3: t1.display(); break;
case 4: exit(0); break;
default: cout<<"Enter correct choice..."; break;
}
}
}

4) Circular Queues using arrays (CQUEUEA.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>
class cqueue
{
T *v;
int size,front,rear;
public: cqueue(int);
~cqueue();
void insertion();
void deletion();
void display();
};

template<class T>

K S V KRISHNA SRIKANTH Page 6


cqueue<T>::cqueue(int k)
{
size=k;
v=new T[size];
front=rear=-1;
}
template<class T>
cqueue<T>::~cqueue()
{
delete v;
}
template<class T>
void cqueue<T>::insertion()
{
T x;
if(front==(rear+1)%size)
cout<<endl<<"Queue Overflow"<<endl;
else
{
cout<<endl<<"Enter a value: ";
cin>>x;
if(front==-1)
front=rear=0;
else
rear=(rear+1)%size;
v[rear]=x;
}
}
template<class T>
void cqueue<T>::deletion()
{
if(front==-1)
cout<<endl<<"Queue Empty"<<endl;
else
{
cout<<endl<<"Deleted element is "<<v[front]<<endl;
if(front==rear)
front=rear=-1;
else
front=(front+1)%size;
}
}
template<class T>
void cqueue<T>::display()
{
if(front==-1)
cout<<endl<<"Queue Empty"<<endl;
else
{
cout<<endl<<"Elements of circular queue: ";
for(int i=front;i!=rear;i=(i+1)%size)
{
cout<<v[i]<<" ";
}

K S V KRISHNA SRIKANTH Page 7


cout<<v[rear]<<" ";
cout<<endl;
}
}
void main()
{
cqueue <int> t(5);
int ch;
clrscr();
do
{
cout<<endl<<"##Circular Queue Menu##"<<endl;
cout<<"1. Insertion"<<endl;
cout<<"2. Deletion"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch(ch)
{
case 1: t.insertion(); break;
case 2: t.deletion(); break;
case 3: t.display(); break;
case 4: exit(0); break;
default: cout<<"Enter valid choice...";
break;
}
}
while(ch<=4);
}

5) Circular Queues using linked lists (CQUEUELL.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>
template <class T>
class cqueue
{
typedef struct node
{
int data;
struct node *link;
}s;
s *front,*rear,*p,*k;
public: cqueue();
void insertion();
void deletion();
void display();
};
template<class T>
cqueue<T>::cqueue()
{
front=rear=NULL;

K S V KRISHNA SRIKANTH Page 8


}
template<class T>
void cqueue<T>::insertion()
{
T x;
p=new s;
cout<<endl<<"Enter data value:";
cin>>x;
p->data=x;
p->link=NULL;
if(front==NULL)
front=rear=p;
else
{
rear->link=p;
p->link=front;
rear=p;
}
}
template<class T>
void cqueue<T>::deletion()
{ if((front==NULL)&&(rear==NULL))
{
cout<<endl<<"Queue Empty";
}
else
{
if(front==rear)
{
cout<<endl<<"The deleted element is "<<front->data;
delete front;
front=NULL;
rear=NULL;
}
else
{
p=front;
front=front->link;
rear->link=front;
cout<<endl<<"The deleted element is "<<p->data;
delete p;
}
}

}
template<class T>
void cqueue<T>::display()
{
if(front==NULL)
cout<<endl<<"Queue Empty";
else
{
cout<<endl<<"The elements are: ";
for(p=front;p->link!=front;p=p->link)

K S V KRISHNA SRIKANTH Page 9


{ cout<<p->data<<" ";}
cout<<p->data<<" ";
cout<<endl;
}
}
void main()
{
int ch;
clrscr();
cqueue <int> t1;
while(1)
{
cout<<endl<<"****CIRCULAR QUEUE MENU****"<<endl;
cout<<"1. Insertion"<<endl;
cout<<"2. Deletion"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Enter choice:";
cin>>ch;
switch(ch)
{
case 1: t1.insertion(); break;
case 2: t1.deletion(); break;
case 3: t1.display(); break;
case 4: exit(0); break;
default: cout<<"Enter valid choice...";
break;
}
}
}

6) Dequeue using double linked lists (DEQUEUE.cpp)

#include<iostream.h>
#include<conio.h>
#inlcude<process.h>
template<class T>
class dequeue
{
typedef struct nnode
{
int data;
node *left,*right;
}s;
s *front,*rear,*p,*q,*k;
public:
dequeue()
{
front=rear=NULL;
}
void insbegin();
void insend();
void delbegin();
void delend();

K S V KRISHNA SRIKANTH Page 10


void display();
};

template<class T>
void dequeue<T>::display()
{
cout<<"The Dequeue elements are: ";
for(p=front;p!=NULL;p=p->right)
cout<<p->data<<" ";
cout<<endl;
}
template<class T>
void dequeue<T>::insbegin()
{
T x;
q=new s;
cout<<"Enter data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
if(front==NULL)
front=rear=q;
else
{
q->right=front;
front->left=q;
front=q;
}
}
template<class T>
void dequeue<T>::insend()
{
T x;
q=new s;
cout<<"Enter data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
if(front==NULL)
front=rear=q;
else
{
rear->right=q;
q->left=rear;
rear=q;
}
}

template<class T>
void dequeue<T>::delbegin()
{
p=front;
front=front->right;
front->left=NULL;

K S V KRISHNA SRIKANTH Page 11


cout<<"Deleted Element is "<<p->data;
delete p;
}
template<class T>
void dequeue<T>::delend()
{
p=rear;
rear=rear->left;
rear->right=NULL;
cout<<"Deleted Element is "<<p->data;
delete p;
}

void main()
{
int ch;
clrscr();
dequeue <int> t1;
while(1)
{
cout<<"**Dequeue Menu**"<<endl;
cout<<"1.Insertion at front"<<endl;
cout<<"2.Insertion at rear"<<endl;
cout<<"3.Deletion at front"<<endl;
cout<<"4.Deletion at rear"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch(ch)
{
case 1: t1.insbegin();
break;
case 2: t1.insend();
break;
case 3: t1.delbegin();
break;
case 4: t1.delend();
break;
case 5: t1.display();
break;
case 6: exit(0);
break;
default: cout<<"Enter valid Option";
break;
}
}
}

7) Evaluation of Postfix Expression (POSTFIXE.cpp)

#include<iostream.h>
#include<process.h>
#include<conio.h>

K S V KRISHNA SRIKANTH Page 12


#inlcude<math.h>
#inlcude<string.h>
class postfix
{
private:
int stack[50],len,top;
char post[50];
public:
postfix()
{
top=-1;
}
void push(int);
int pop();
int pfix();
};
int postfix::pfix()
{
int a,b,c,temp;
cout<<"Enter postfix expression:";
cin>>post;
len=strlen(post);
post[len]='#';
for(i=0;post[i]!='#';i++)
{
if(post[i]<='9'&&post[i]>='0')
push(post[i]-48);
else
{
a=pop();
b=pop();
switch(post[i])
{
case '+':temp=b+a;
break;
case '-':temp=b-a;
break;
case '*':temp=b*a;
break;
case '/':temp=b/a;
break;
case '%':temp=b%a;
break;
case '^':temp=pow(b,a);
break;
}
push(temp);
}
}
return(pop());
}
void postfix::push(int x)
{
stack[++top]=x;

K S V KRISHNA SRIKANTH Page 13


}
int postfix::pop()
{
int x=stacl[top];
top--;
return x;
}
void main()
{
int x;
postfix b;
clrscr();
x=b.pfix();
cout<<endl<<"Result of postfix expression is "<<x;
getch();
}

8) Conversion of Infix to Postfix (INTOPOST.cpp)

#include<iostream.h>
#include<conio.h>
#include<process.h>
char stack[30],postfix[30],infix[30];
class intopost
{
public:
int top;
public:
intopost()
{
top=-1;
}
int pri(char);
void push(char);
char stacktop();
int isalnum(char);
char pop();
void conintopost(char[],char[]);
};
int intopost::pri(char x)
{
int value;
switch(x)
{
case ')':value=0;
break;
case '+':value=1;
break;
case '-':value=1;
break;
case '*':value=2;
break;
case '/':value=2;
break;

K S V KRISHNA SRIKANTH Page 14


case '^':value=3;
break;
case '(':value=4;
break;
}
return value;
}
void intopost::push(char x)
{
top=top+1;
stack[top]=x;
}
char intopost::stacktop()
{
return stack[top];
}
int intopost::isalnum(char x)
{
return((x>='0'&&x<='9')||(x>='a'&&x<='z')||(x>='A'&&x<='Z'));
}
char intopost::pop()
{
return stack[top--];
}
void intopost::conintopost(char infix[],char postfix[])
{
int i,j=0;
char c,pc;
for(i=0;(c=infix[i])!='\o';i++)
{
if(isalnum(c))
postfix[j++]=c;
else
{
while(top!=-1 && (pri(stacktop())>=pri(c)))
{
if(stacktop()=='('&&c!=')')
break;
if(stacktop()=='('&&c==')')
{
pop();
break;
}
pc=pop();
if(pc!='(')
postfix[j++]=pc;
else
break;
}
if(c!=')')
push(c);
}
}
while(top!=-1)

K S V KRISHNA SRIKANTH Page 15


postfix[j++]=pop();
postfix[j]='\o';
}

void main()
{
clrscr();
intopost t1;
cout<<"Enter infix expression:";
cin>>infix;
t1.conintopost(infix,postfix);
cout<<endl<<"Postfix Expression is ";
cout<<postfix;
getch();
}

9) Addition & Subtraction of 2 sparse matrices (GSPARSE.cpp)

#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
int sp1[10][3],sp2[10][3],sp3[10][3];
int i,j,m,n,p,q,t1,t2,d,s,element;
int sum[10][3],diff[10][3];
cout<<"Enter the number of rows and columns\n";
cin>>m;
cin>>n;
t1=t2=0;
cout<<" Enter the first matrix("<<m<<"*"<<n<<")\n";
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>element;
if(element!=0)
{
t1=t1+1;
sp1[t1][1]=i;
sp1[t1][2]=j;
sp1[t1][3]=element;
}
}
}
sp1[0][1]=m;
sp1[0][2]=n;
sp1[0][3]=t1;
cout<<"Enter the Second Matrix("<<m<<"*"<<n<<"):\n";
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{

K S V KRISHNA SRIKANTH Page 16


cin>>element;
if(element!=0)
{
t2=t2+1;
sp2[t2][1]=i;
sp2[t2][2]=j;
sp2[t2][3]=element;
}
}
}
sp2[0][1]=m;
sp2[0][2]=n;
sp2[0][3]=t2;
cout<<"\n The first sparse matrix is\n Row \t column\t element";
for(i=0;i<=t1;i++)
{
cout<<"\n"<<sp1[i][1]<<"\t"<<sp1[i][2]<<"\t"<<sp1[i][3]<<"\n";
}
cout<<"The second sparse matrix is:";
for(i=0;i<=t2;i++)
{
cout<<"\n"<<sp2[i][1]<<"\t"<<sp2[i][2]<<"\t"<<sp2[i][3]<<"\n";
}
i=j=s=d=1;
while((i<=t1)&&(j<=t2))
{
if(sp1[i][1]=sp2[j][1])

{
if(sp1[i][2]=sp1[j][2])
{
sum[s][1]=diff[d][1]=sp1[i][1];
sum[s][2]=diff[d][2]=sp1[j][2];
sum[s][3]=sp1[i][3]+sp2[j][3];
diff[d][3]=sp1[i][3]-sp2[j][3];
i++;
j++;
if(sum[s][3]!=0)
s++;
if(diff[d][3]!=0)
d++;
}
else
{
if(sp1[i][2]<sp2[3][2])
{
sum[s][1]=diff[d][1]=sp1[i][1];
sum[s][2]=diff[d][2]=sp1[i][2];
sum[s][3]=diff[d][3]=sp1[i][3];
i++;
s++;
d++;
}
else

K S V KRISHNA SRIKANTH Page 17


{
sum[s][1]=diff[d][1]=sp2[j][1];
sum[s][2]=diff[d][2]=sp2[j][2];
sum[s][3]=sp2[j][3];
diff[d][3]=0-sp2[j][3];
j++;
d++;
s+1;
}
}
}
else
{
if(sp1[i][1]<sp2[3][1])
{
sum[s][i]=diff[d][1]=sp1[i][1];
sum[s][2]=diff[d][2]=sp1[i][2];
sum[s][3]=diff[d][3]=sp1[i][3];
i++;
d++;
s++;
}
else
{
sum[s][1]=diff[d][1]=sp2[j][1];
sum[s][2]=diff[d][2]=sp2[j][2];
sum[s][3]=sp2[j][3];
diff[d][3]=0-sp2[j][3];
j++;
s++;
d++;
}
}
}
if(i<=t1)
{
for(p=1;p<=t1;p++)
{
sum[s][1]=diff[d][1]=sp1[p][1];
sum[s][2]=diff[d][2]=sp1[p][2];
sum[s][3]=diff[d][2]=sp1[p][3];
s++;
d++;
}
}
else if(j<=t2)
{
for(p=j;p<=t2;p++)
{
sum[s][1]=diff[d][1]=sp2[p][2];
sum[s][2]=diff[d][2]=sp2[p][2];
sum[s][3]=sp2[p][3];
diff[d][3]=0-sp2[j][3];
s++;

K S V KRISHNA SRIKANTH Page 18


d++;
}

}
sum[0][1]=diff[0][1]=m;
sum[0][2]=diff[0][2]=n;
sum[0][3]=s-1;
diff[0][3]=d-1;
cout<<"the sum of two sparse matrices is:";
for(i=0;i<s;i++)
{
cout<<"\n"<<sum[i][1]<<"\t"<<sum[i][2]<<"\t"<<sum[i][3]<<"\n";
}
cout<<"The difference of the two matrices is :";
for(i=0;i<d;i++)
{
cout<<"\n"<<diff[i][1]<<"\t"<<diff[i][2]<<"\t"<<diff[i][3]<<"\n";
}
getch();
return 0;
}

10) Binary tree traversals using recursion (BSHTREE.cpp)

#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class T>
class bstree
{
typedef struct node
{
int data;
struct node *left,*right;
}s;
s *head,*prev,*p,*c,*k,*q;
int t;
public:bstree();
void creation();
void display();
void insertion();
void deletion();
void preorder(s *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
preorder(p->left);
preorder(p->right);
}
else
return;
}
void inorder(s *p)

K S V KRISHNA SRIKANTH Page 19


{
if(p!=NULL)
{
inorder(p->left);
cout<<p->data<<" ";
inorder(p->right);
}
else
return;
}
void postorder(s *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->data<<" ";
}
else
return;
}
};

template<class T>
bstree<T>::bstree()
{
head=prev=c=NULL;
}

template<class T>
void bstree<T>::creation()
{
int n;
T x;
cout<<"Enter the n value: ";
cin>>n;
cout<<endl;
for(int i=1;i<=n;i++)
{
p=new s;
cout<<"Enter the data value:";
cin>>x;
p->data=x;
p->left=p->right=NULL;
if(head==NULL)
head=p;
else
c=head;
while(c!=NULL)
{
prev=c;
if(c->data<p->data)
c=c->right;
else

K S V KRISHNA SRIKANTH Page 20


c=c->left;
}
if(prev!=NULL)
{
if(prev->data<p->data)
prev->right=p;
else
prev->left=p;
}
}
}

template<class T>
void bstree<T>::insertion()
{
T x;
q=new s;
cout<<endl<<"Enter the data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
c=head;
while(c!=NULL)
{
prev=c;
if(c->data<q->data)
c=c->right;
else
c=c->left;
}
if(prev!=NULL)
{
if(prev->data<q->data)
prev->right=q;
else
prev->left=q;
}
}

template<class T>
void bstree<T>::display()
{
int ch;
cout<<endl<<"The tree elements:\n ";
cout<<"\n---------------------------\n";
cout<<" DISPLAY MENU \n";
cout<<"----------------------------\n";
cout<<" 1. Pre-Order \n";
cout<<" 2. In-Order \n";
cout<<" 3. Post-Order \n";
cout<<"----------------------------\n";
do
{
cout<<endl<<"Enter the display choice: ";

K S V KRISHNA SRIKANTH Page 21


cin>>ch;

switch(ch)
{
case 1: cout<<endl<<"Preorder Display: ";
preorder(head);
cout<<endl;
break;
case 2: cout<<endl<<"Inorder Display: ";
inorder(head);
cout<<endl;
break;
case 3: cout<<endl<<"Postorder Display: ";
postorder(head);
cout<<endl;
break;
}
}while(ch<=3);
}

template<class T>
void bstree<T>::deletion()
{
if(head==NULL)
cout<<endl<<"Deletion not possible";
else
{
int x;
cout<<endl<<"Enter the delete element: ";
cin>>x;
if(head->data==x)
cout<<endl<<"The root element deletion not possible ";
else
{
c=head;
while(c->data!=x)
{
prev=c;
if(x<c->data)
c=c->left;
else
c=c->right;
}
if(c->left==NULL && c->right==NULL)
{
if(prev->left==c)
prev->left=NULL;
else
prev->right=NULL;
}
else
if(c->left!=NULL && c->right==NULL)
prev->left=c->left;
else

K S V KRISHNA SRIKANTH Page 22


if(c->left==NULL && c->right!=NULL)
{
if(prev->left==c)
prev->left=c->right;
else
prev->right=c->right;
}
else
if(c->left!=NULL && c->right!=NULL)
{
while(c->right!=NULL)
{
c->data=c->right->data;
prev=c;
c=c->right;
}
prev->right=NULL;
}
}

}
}

void main()
{
int ch;
clrscr();
bstree <int> t1;
t1.creation();
while(1)
{
cout<<"\n--------------------- \n";
cout<<" MAIN MENU \n";
cout<<"----------------------\n";
cout<<" 1.Insertion \n";
cout<<" 2.Deletion \n";
cout<<" 3.Display \n";
cout<<"-----------------------\n";
cout<<endl<<"Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1: t1.insertion();break;
case 2: t1.deletion();break;
case 3: t1.display();break;
default:exit(0);
}
}
}

11) Binary Search Tree Operations (BSTREE.cpp)

#include<iostream.h>
#include<conio.h>

K S V KRISHNA SRIKANTH Page 23


#include<process.h>
#define TRUE 1
#define FALSE 0
class bstree
{
private: struct node
{
node *lc;
int data;
node *rc;
}*root;
public:
bstree {
root=NULL;
}
void buildtree(int num)
{
insert(&root,num);
}
static void insert(node **sr,int);
static void search(node **sr,int num,node **par,node **x,int *found);
void remove(int num) {
rem(&root,num);
}
static void rem(node **sr,int num);
void display() {
inorder(root);
}
static void inorder(node *sr);
~bstree() {
del(root);
}
static void del(node *sr)
{
if(sr!=NULL) {
del(sr->lc);
del(sr->rc);
}
delete sr;
}
};
void bstree::insert(node **sr,int num)
{
if(*sr==NULL) {
*sr=new node;
(*sr)->lc=NULL;
(*sr)->data=num;
(*sr)->rc=NULL;
}
else {
if(num<(*sr)->data)
insert(&((*sr)->lc),num);
else
insert(&((*sr)->rc),num);

K S V KRISHNA SRIKANTH Page 24


}
}
void bstree::rem(node **sr,int num)
{
int found;
node *parent,*x,*xsucc;
if(*sr==NULL) {
cout<<"Tree is Empty";
return;
}
parent=x=NULL;
search(sr,num,&parent,&x,&found);
if(found==FALSE) {
cout<<endl<<"Data to be deleted, not found";
return;
}
if(x->lc!=NULL && x->rc!=NULL)
{
parent=x;
xsucc=x->rc;
while(xsucc->lc!=NULL) {
parent=xsucc;
xsucc=xsucc->lc;
}
x->data=xsucc->data;
x=xsucc;
}
if(x->lc==NULL && x->rc!=NULL)
{
if(parent->lc==x)
parent->rc=x->rc;
else
parent->lc=x->rc;
delete x;
return;
}
if(x->lc!=NULL && x->rc==NULL)
{
if(parent->lc==x)
parent->lc=x->lc;
else
parent->rc=x->lc;
delete x;
return;
}

}
void bstree::search(node **sr,int num,node **par,node **x,int *found)
{
node *q;
q=*sr;
*found=FALSE;
*par=NULL;
while(q!=NULL)

K S V KRISHNA SRIKANTH Page 25


{
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data>num)
q=q->lc;
else
q=q->rc;
}
}
void bstree::inorder(node *sr)
{
if(sr!=NULL)
{
inorder(sr->lc);
cout<<sr->data<<" ";
inorder(sr->rc);
}
}
void main()
{
bstree bst;
int req,i=0,num,a[]={11,9,13,8,10,12,14,15,7};
while(i<=8)
{
bst.buildtree(a[i]);
i++;
}
cout<<"BST before deletion: \n";
bst.display();
bst.remove(10);
cout<<"BST after deletion: \n";
bst.display();
cout<<"BST before deletion: \n";
bst.remove(13);
cout<<"BST after deletion: \n";
bst.display();
}

12) Polynomial Addition & Multiplication (POLYA_M.cpp)

#include<iostream.h>
#include<conio.h>
#define n 100
class poly
{
private:
int a[n],b[n],add[n],mul[n],p,q,at;
public:
void init();

K S V KRISHNA SRIKANTH Page 26


void input();
void process();
void display();
};

void poly :: init()


{
int i;
for(i=0;i<n;i++)
a[i] = b[i] = add[i] = mul[i] = 0;
}

void poly :: input()


{
int i;
cout<<"\nEnter Degree of First Polynomial: ";
cin>>p;
cout<<"\nEnter Degree of Second Polynomial: ";
cin>>q;
cout<<"\nEnter Values of First Polynomial\n";
for(i=0;i<=p;i++)
{
cout<<"\nEnter x^"<<i<<" th Coefficient: ";
cin>>a[i];
}
cout<<"\nEnter Values of Second Polynomial\n";
for(i=0;i<=q;i++)
{
cout<<"\nEnter x^"<<i<<" th Coefficient: ";
cin>>b[i];
}
}

void poly :: process()


{
int i, j;
if(p>q)
at = p;
else
at = q;
for(i=0;i<=at;i++)
add[i]=a[i]+b[i];
for(i=0;i<=p;i++)
for(j=0;j<=q;j++)
mul[i+j]+=a[i]*b[j];
}

void poly :: display()


{
int i;
cout<<"\n\nAddition of 2 Polynomial Expressions is\n\n";
for(i=at;i>=0;i--)
cout<<add[i]<<"x^"<<i<<"+";
cout<<"\n\nMultiplication of 2 Polynomial Expressions is\n\n";

K S V KRISHNA SRIKANTH Page 27


for(i=p+q;i>=0;i--)
cout<<mul[i]<<"x^"<< i <<"+";
}

void main()
{
poly ob;
clrscr();
ob.init();
ob.input();
ob.process();
ob.display();
getch();
}

13) Heap Sort (HEAPSORT.cpp)

#include<iostream.h>
#include<conio.h>
class hsort
{
private:
int *a,n;
public:
hsort(int x)
{
n=x;
a=new int[n];
}
void getdata()
{
cout<<"\n";
for(int i=0;i<n;i++) {
cout<<"\tEnter "<<i<<" th value: ";
cin>>a[i];
}
}
void heapsort(int l)
{
for(int i=(l-1)/2;i>=0;i--)
formheap(i,l);
for(i=l;i>0;i--)
{
int t=a[0];
a[0]=a[i];
a[i]=t;
formheap(0,i-1);
}
}
void formheap(int i,int n)
{
int j=2*i+1;
while(j<=n)
{

K S V KRISHNA SRIKANTH Page 28


if(j+1<=n && a[j+1] > a[(j-1)/2])
if(a[j+1] >a[j])
j++;
if(a[j] > a[(j-1) / 2])
{
int t=a[j];
a[j]=a[(j-1)/2];
a[(j-1)/2]=t;
}
j=2*j+1;
}
}
void display()
{
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
};

void main()
{
int n;
clrscr();
cout<<"Enter n value:";
cin>>n;
hsort t(n);
t.getdata();
cout<<"\n\nElements before sorting:\n\n\t";
t.display();
t.heapsort(n-1);
cout<<"\n\nElements after sorting are:\n\n\t";
t.display();
}

14) Merge Sort (MERGESORT.cpp)

#include<iostream.h>
#include<conio.h>
class msort
{
int *a,*b,n,i,mid,k;

public:
msort(int size)
{
n=size;
a=new int[n];
b=new int[n];
}
void getdata()
{
cout<<"\n";
for(i=0;i<n;i++) {
cout<<"\tEnter "<<i<<" th value:";

K S V KRISHNA SRIKANTH Page 29


cin>>a[i];
}
void display()
{
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
void sort(int left,int right)
{
if(left<right)
{
mid=(left+right)/2;
sort(left,mid);
sort(mid+1,right);
merge(left,right);
}
else
return;
}
void merge(int first,int last)
{
int mid=(first+last)/2;
int i1=first;
int i2=mid+1;
int i3=0;
while(i1<=mid && i2<=last)
{
if(a[i1]<a[i2])
b[i3++]=a[i1++];
else
b[i3++]=a[i2++];
}
for(;i1<=mid;)
b[i3++]=a[i1++];
for(;i2<=last;)
b[i3++]=a[i2++];
for(i=first,k=0;i<=last;i++,k++)
a[i]=b[k];
}
};

void main()
{
int m;
clrscr();
cout<<"Enter the array size:";
cin>>m;
msort t(m);
t.getdata();
cout<<"\n\nElements before sorting: \n\n\t";
t.display();
t.sort(0,m-1);
cout<<"\n\nElements after sorting: \n\n\t";
t.display();

K S V KRISHNA SRIKANTH Page 30


}

15) Insertion Sort

#include<iostream.h>
#include<conio.h>
class isort
{
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();

};
void isort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void isort::sort()
{
for(i=0;i<n;i++)
{
j=i;
while((j>0)&&(array[j-1]>array[j]))
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
j--;
}

}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}

void main()
{
clrscr();
isort b;
b.init();
b.sort();
getch();
}

16) Bubble Sort (BSORT.cpp | BUBBLESORT.cpp)

#include<iostream.h>

K S V KRISHNA SRIKANTH Page 31


#include<conio.h>
void main()
{
int array[100],n,i,j,temp;
clrscr();
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"Enter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
cout<<"\nArray is sorted in ascending order.\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
getch();
}

#include<iostream.h>
#include<conio.h>
class bsort
{
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();

};
void bsort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"Enter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void bsort::sort()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];

K S V KRISHNA SRIKANTH Page 32


array[j]=array[j+1];
array[j+1]=temp;
}
}
cout<<"\nArray is sorted in ascending order:\n\n\t";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}

void main()
{
clrscr();
bsort b;
b.init();
b.sort();
getch();
}

17) Selection Sort (SSORT.cpp)

#include<conio.h>
#include<iostream.h>
#include<process.h>

void main()
{
int array[100],n,i,j,temp;
clrscr();
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n\n";
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
getch();
}

#include<iostream.h>
#include<conio.h>
class ssort
{
K S V KRISHNA SRIKANTH Page 33
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();

};
void ssort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void ssort::sort()
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}

void main()
{
clrscr();
ssort b;
b.init();
b.sort();
getch();
}

18) Binary Search (BSEARCH.cpp | CBSEARCH.cpp)

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int ar[100],beg,mid,end,i,n,search;
clrscr();
cout<<"Enter the size of array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers in ascending order: \n";
for(i=0;i<n;i++)

K S V KRISHNA SRIKANTH Page 34


cin>>ar[i];
beg=0;
end=n-1;
cout<<"\nEnter a number to search: ";
cin>>search;
while(beg<=end)
{
mid=(beg+end)/2;
if(ar[mid]==search)
{
cout<<"\nItem "<<search<<" found at position "<<(mid+1);
getch();
exit(0);
}
if(search>ar[mid])
beg=mid+1;
else
end=mid-1;
}
cout<<"\nSorry! "<<search<<" is not found.";
getch();
}

#include<iostream.h>
#include<conio.h>
#include<process.h>
class bsearch
{
private:
int ar[100],beg,mid,end,i,n,s;
public:
void search();
};

void bsearch::search()
{
cout<<"Enter the size of array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers in ascending order: \n";
for(i=0;i<n;i++)
cin>>ar[i];
beg=0;
end=n-1;
cout<<"\nEnter a number to search: ";
cin>>s;
while(beg<=end)
{
mid=(beg+end)/2;
if(ar[mid]==s)
{
cout<<"\nItem "<<s<<" found at position "<<(mid+1);
getch();
exit(0);

K S V KRISHNA SRIKANTH Page 35


}
if(s>ar[mid])
beg=mid+1;
else
end=mid-1;
}
cout<<"\nSorry! "<<s<<" is not found.";
getch();
}

void main()
{

clrscr();
bsearch b;
b.search();
}

19) Linear Search (LSEARCH.cpp | CLSEARCH.cpp)

#include<iostream.h>
#include<conio.h>
int main()
{
int a[10],i,n,m,c=0;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;

cout<<"\nEnter the elements of the array: \n";


for(i=0;i<=n-1;i++){
cin>>a[i];
}

cout<<"\nThe elements of an array are: ";


for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}

cout<<"\n\nEnter the number to be searched: ";


cin>>m;
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}

if(c==0)
cout<<"\nThe number "<<m<< " is not in the list";
else
cout<<"\nThe number "<<m<< " is found";
return 0;
}

K S V KRISHNA SRIKANTH Page 36


#include<iostream.h>
#include<conio.h>
class lsearch
{
private:
int c,a[10],i,n,m;
public:
lsearch();
void init();
void search();
};

lsearch::lsearch()
{
c=0;
}
void lsearch::init()
{
cout<<"Enter the size of an array: ";
cin>>n;

cout<<"\nEnter the elements of the array: \n";


for(i=0;i<=n-1;i++){
cin>>a[i];
}

cout<<"\nThe elements of an array are: ";


for(i=0;i<=n-1;i++){
cout<<a[i]<<" ";
}
}
void lsearch::search()
{
cout<<"\n\nEnter the number to be searched: ";
cin>>m;
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}

if(c==0)
cout<<"\nThe number "<<m<< " is not in the list";
else
cout<<"\nThe number "<<m<< " is found";
return 0;
}

int main()
{
clrscr();

K S V KRISHNA SRIKANTH Page 37


lsearch l;
l.init();
l.search();
}

20) Quick Sort

#include<iostream.h>
#include<process.h>
#include<conio.h>

void qsort(int numbers[], int left, int right);


int numbers[150];

int main()
{
int i,n;
clrscr();
cout<<"How many numbers you want to sort: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers:\n";
for (i = 0; i<n; i++)
cin>>numbers[i];
//perform quick sort on array
qsort(numbers,0,n-1);

cout<<"\nSorted Numbers are: \n";


for (i = 0; i<n; i++)
cout<<numbers[i]<<" ";
getch();
return(0);
}

// Function to sort
void qsort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];

K S V KRISHNA SRIKANTH Page 38


right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
qsort(numbers, left, pivot-1);
if (right > pivot)
qsort(numbers, pivot+1, right);
}

K S V KRISHNA SRIKANTH Page 39

You might also like