Вы находитесь на странице: 1из 70

EC6312 - OOPS AND DATA STRUCTURES LABORATORY

LIST OF EXPERIMENTS
S.NO.
1.

2.
3.
4.
5.
6.

7.

NAME OF THE CONCEPT / PROGRAM


Basic Programs for C++ Concepts.
1.1 C++ Programs without using classes and
objects
a) Displaying Student Details
b) Sum of N Natural numbers
c) Finding sum and average of 5 marks
1.2 C++ Programs without using classes and
objects
a) Displaying time in different formats
b) Factorial of the given numbers
c) Reference variables
d) Constructors and Destructors
e) Function Overloading
f) Operator Overloading
i. Unary minus operator
ii. Binary addition operator
g) Inheritance
Array implementation of LIST ADT
Linked List Implementation of LIST ADT
Cursor Implementation of LIST ADT
Stack ADT
a) Array Implementation
b) Linked List Implementation
Program source files
i. Stack application 1 Checking parenthesis
ii. Array implementation of STACK ADT
Application 1 - Checking parenthesis
Application 2 - Checking parenthesis
iii. Linked list implementation of STACK ADT
Application 1 - Checking parenthesis
Application 2 - Checking parenthesis
iv. Stack application 2 Post-fix expression
evaluation
v. Program with necessary header files
Stack Application (expression conversion ) - Infix to
postfix

8.
9.

10.
11.
12.

i. Array Implementation
ii. Linked List Implementation
Stack Application String Reverse
i. Array Implementation
ii. Linked List Implementation
Stack Application (expression conversion ) - Infix to
prefix
i. Array Implementation
ii. Linked List Implementation
QUEUE ADT
i. Array Implementation
ii. Linked List Implementation
Search Tree ADT Binary Search Tree
Quick Sort

1. BASIC C++ PROGRAMS


1.1 C++ Program Without Classes And Objects
a. DISPLAYING STUDENT DETAILS
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[23],college[23],department[23];
intregno;
cout<<"*** STUDENT DETAILS ***\n";
cout<<Enter name, Depatment, regno and college : \n;
cin>>name>>department>>regno>>college;
cout<<"\nNAME:\t"<<name;
cout<<"\nDEPARTMENT:\t"<<department;
cout<<"\nREG. NO:\t"<<regno;
cout<<"\nCOLLEGE:\t"<<college;
getch();
}

OUTPUT
*** STUDENT DETAILS
***
Enter name, Depatment,
regno and college :
Raja
CSE
210
SSMIET
NAME : Raja
DEPARTMENT : CSE
REG. NO : 210
COLLEGE : SSMIET

b. SUM OF N NATURAL NUMBERS


#include<iostream.h>
#include<conio.h>
void main()
{
inti,n,sum=0;
clrscr();
cout<<*** SUM OF N NATURAL NUMBERS ***;
cout<<"\nEnter the no. of terms : ";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<"\nThe sum of"<<n<<" natural
getch();
}

OUTPUT
*** SUM OF N
NATURAL
NUMBERS ***
Enter the no. of terms : 3
The sum of 3 natural
numbers : 6

number :"<<sum;

c. FINDING THE TOTAL AND AVERAGE OF FIVE MARKS


#include<iostream.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5;
float total,avg;
clrscr();
cout<<*** Finding Total and Average of 5 marks ***\n;
cout<<"\nEnter the value of five marks : \n";
cin>>m1>>m2>>m3>>m4>>m5;
total=(m1+m2+m3+m4+m5);
avg=(total)/5;
cout<<"\nThe total of 5 marks is "<<total<<"and their average is"<<avg;
getch();
}
OUTPUT
*** Finding Total and
Average of 5 marks
***
Enter the value of five
marks :
99
98
96
97
95
The total of 5 marks is
486 and their average
is 97

1.2 C++PROGRAMS WITH CLASSES AND OBJECTS


a. DISPLAYING TIME USING 12 HOURS AND 24 HOURS FORMAT
#include<iostream.h>
#include<conio.h>
class time
{
public:
int hr,min,sec;
void getdata();
void display();
};
void time::getdata()
{
cout<<"\nEnter the time : ";
cin>>hr>>min>>sec;
}
void time::display()
{
if(hr>24)
cout<<"\nWRONG INPUT DATA";
else
{
if(hr>12)
{
cout<<"\n24 hours format";
cout<<hr<<":"<<min<<":"<<sec;
cout<<"\n12 houts format";
hr=hr-12;
cout<<hr<<":"<<min<<":"<<sec;
}
else
{
cout<<"\n24 hour format";
cout<<hr<<":"<<min<<":"<<sec;
cout<<"\n12 hour format";
cout<<hr<<":"<<min<<":"<<sec;
}
}
}
void main()
{
clrscr();
time obj;
cout<<*** Displaying time in two formats ***\n;
obj.getdata();

OUTPUT
Compilatiopn 1
*** Displaying time in
two formats ***
Enter the time
18 18 34
24 hours format 18:18:34
12 hours format 6:18:34

OUTPUT
Compilatiopn 2
*** Displaying time in
two formats ***
Enter the time
10 23 45
24 hours format 10:23:45
12 hours format 10:23:45
OUTPUT
Compilatiopn 3
*** Displaying time in
two formats ***
Enter the time
54 23 45
WRONG INPUT DATA

obj.display();
getch();
}
b. FACTORIAL OF THE GIVEN NUMBER
#include<iostream.h>
#include<conio.h>
class factorial
{
public:
intn,i,fact;
void getdata();
void calc();
void display();
};
OUTPUT Compilation 1
void factorial::getdata()
{
*** FACTORIAL OF THE GIVEN NUMBER ***
cout<<"\nEnter the number : ";
Enter the number : 5
cin>>n;
The factorial of the given number is 120
}
void factorial::calc()
{
fact=1;
OUTPUT Compilation 2
if(n==0)
display();
*** FACTORIAL OF THE GIVEN NUMBER ***
else
Enter the number : 0
{
The factorial of the given number is 1
for(i=1;i<=n;i++)
fact=fact*i;
}
}
void factorial::display()
{
cout<<"\nThe factorial of the given number is"<<f;
}
void main()
{
clrscr();
cout<<"*** FACTORIAL OF THE GIVEN NUMBER ***\n";
factorial obj;
obj.getdata();
obj.calc();
obj.display();
getch();
}

c. REFERENCE VARIABLE
#include<iostream.h>
#include<conio.h>
class reference
{
public:
void display();
};
void reference::display()
{
float total=100;
float &sum=total;
cout<<Initial Values of total & Sum : \n\n;
cout<<'\n'<<"Total"<<'\t'<<total;
cout<<'\n'<<"Sum"<<'\t'<<sum<<endl;
cout<<\nAfter adding 10 to total :\n;
total=total+10;
cout<<'\n'<<"Total"<<'\t'<<total;
cout<<'\n'<<"Sum"<<'\t'<<sum;
cout<<\nAfter assigning 0 to sum :\n;
sum=0;
cout<<'\n'<<"total"<<'\t'<<total;
cout<<'\n'<<"sum"<<'\t'<<sum;
}
void main()
{
clrscr();
cout<<"*** REFERENCE VARIABLE ***\n";
reference obj;
obj.display();
getch();
}
OUTPUT
*** REFERENCE
VARIABLE ***
Initial Values of total &
sum :
Total 100
Sum 100
After adding 1o to total
Total 110
Sum 110
After assigning 0 to sum
Total 0
Sum 0

d. CONSTRUCTOR AND DESTRUCTOR


#include<iostream.h>
#include<conio.h>
class code
OUTPUT
{
public:
** CONSTRUCTOR
int id;
AND DESTRUCTOR
code(int a)
***
{
cout<<"\nConstructor called\n";
Constructor called
id=a;
}
Copy constructor called
code(code &x)
{
Copy constructor called
cout<<"\nCopy constructor called\n";
id=x.id;
id of A is : 100
}
~code()
id of B is : 100
{
cout<<"\nDestructor called\n";
id of C is : 100
}
void display()
Destructor called
{
cout<<id<<endl;;
Destructor called
}
};
void main()
{
clrscr();
cout<<"*** CONSTRUCTOR AND DESTRUCTOR ***\n\n";
code A(100);
code B(A);
code C=A;
cout<<"\nid of A is :";
A.display();
cout<<"\nid of B is :";
B.display();
cout<<"\nid of C is :";
C.display();
getch();
}

e. FUNCTION OVERLOADING
#include<iostream.h>
#include<conio.h>
class calc
{
inta,b,c;
float avg;
public:
void average(inta,int b);
void average(inta,intb,int c);
};
void calc::average(inta,int b)
{
avg=(a+b)/2;
cout<<"\nThe average of <<a<< and <<b<< is"<<"\t"<<avg;
}
void calc::average(inta,intb,int c)
{
avg=(a+b+c)/3;
cout<<"\nThe average of <<a<< , <<b<< and <<c<< is"<<"\t"<<avg;
}
void main()
{
clrscr();
calc obj
cout<<"*** AVERAGE USING FUNCTION OVERLOADING ***\n\n";
obj.average(10,20);
obj.average(10,20,30);
getch();
}
OUTPUT
*** AVERAGE USING
FUNCTION
OVERLOADING ***
The average of 10 and 20
is 15
The average of 10 , 20 and
30 is
20

f. OPERATOR Overloading
(i)

UNARY OPERATOR minus

#include<iostream.h>
#include<conio.h>
class unary
{
intx,y,z;
inta,b,c;
public:
void getdata(inta,intb,int c);
void display();
void operator -();
};
void unary::getdata(inta,intb,int c)
{
cout<<endl;
x=a;
y=b;
z=c;
}
void unary::display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
void unary::operator -()
{
x=-x;
y=-y;
z=-z;
}
void main()
{
clrscr();
cout<<"*** Unary operator overloading ***\n\n";
unary obj;
intm,n,o;
cout<<"\nEnter the values of 3 variables: \n";
cin>>m>>n>>o;
s.getdata(m,n,o);
cout<<"\nValues before overloading\n";
s.display();
//Unary operator is applied on the object created

OUTPUT
*** Unary operator
overloading ***
Enter the values of 3
variables:
5
-6
7
Values before overloading
x=5
y = -6
z=7
Values after overloading
x = -5
y=6
z = -7
s.display();

-s;
cout<<"\nValues after overloading\n";
s.display();
getch();
}
(ii)

BINARY OPERATOR OVERLOADING addition

#include<iostream.h>
#include<conio.h>
class complex
{
double a,b;
public:
complex()
{
}
complex(double real,double img)
{
a=real;
b=img;
}
complex operator +(complex c)
{
complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return(temp);
}
complex operator -(complex c)
{
complex temp;
temp.a=a-c.a;
temp.b=b-c.b;
return(temp);
}
complex operator *(complex c)
{
complex temp;
temp.a=a*c.a;
temp.b=b*c.b;
return(temp);
}
complex operator /(complex c)
{
complex temp;
temp.a=a/c.a;
temp.b=b/c.b;
return(temp);
}
void display()

{
cout<<a<<"+"<<b<<"j";
}
};
void main()
{
clrscr();
complex c1,c2,c3;
cout<<"*** Binary Operator overloading ***";
c1=complex(8,4);
c2=complex(2,2);
cout<<"\nTwo complex numbers \n";
cout<<"c1=";
c1.display();
cout<<"\nc2=";
c2.display();
cout<<"\n\nAddition : c3 = c1 + c2";
c3=c1+c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nSubtraction : c3 = c1 - c2";
c3=c1-c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nMultiplication : c3 = c1 * c2";
c3=c1*c2;
cout<<"\nRESULT = ";
c3.display();
cout<<"\n\nDivision : c3 = c1 / c2";
c3=c1/c2;
cout<<"\nRESULT = ";
c3.display();
getch();
}
OUTPUT

g. C++ Programs for inheritance concept


Refer programs on inheritance in chapter 2.

2. ARRAY IMPLEMENTATIO OF LIST ADT


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class arr
{
public:
intx,i,sea,del,m[25],p;
void create();
void display();
void search();
void erase();
void insert();
};
void arr::create()
{
cout<<"\nEnter the no. of elements";
cin>>x;
cout<<"\Enter the array elements";
for(i=0;i<x;i++)
{
cin>>m[i];
}
}
void arr::display()
{
cout<<"\nthe entered array is";
for(i=0;i<x;i++)
{
cout<<"\n"<<m[i];
}
}

void arr::search()
{
cout<<"\nEnter the element to be searched";
cin>>sea;
for(i=0;i<x;i++)
{
if(sea==m[i])
{
cout<<"\nThe position of the element is"<<i+1;
}
else
{
cout<<"\nElement is not in the given list of arrays";
}
}
}
void arr::erase()
{
cout<<"\nEnter the element to be deleted";
cin>>del;
for(i=0;i<x;i++)
{
if(del==m[i])
{
m[i]=0;
}
else
{
cout<<"\nm[i]="<<m[i];
}
}
}
void arr::insert()
{
cout<<"\nenter the no.of variables to be inserted";
cin>>p;
cout<<"\ninsert the variables";
for(i=0;i<p;i++)
{
cin>>m[x++];
}
}
void main()
{
clrscr();
arr a;
int n;
do
{
cout<<"\n1.create\n2.display\n3.search\n4.delete\n5.insert\n6.exit";
cout<<"\nEnter the choice";

cin>>n;
switch(n)
{
case 1: a.create();
break;
case 2: a.display();
break;
case 3: a.search();
break;
case 4: a.erase();
break;
case 5: a.insert();
break;
case 6: exit(1);
break;
default:cout<<"\nINVALID";
break;
}
}while(n<6);
getch();
}
OUTPUT
1. create
2. display
3. search
4. erase
5. insert
6. exit
Enter the choice 1
Enter the no. of elements 5
Enter the array elements 1
2
3
4
5
1. create
2. display
3. search
4. erase
5. insert
6. exit
Enter the choice 2
The entered array is
1
2
3
4

5
1. create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice3
Entertheelementtobesearched2
Notfound
Locationoftheelementis2
Notfound
Notfound
Notfound
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice4
Entertheelementtobedeleted5
m[i]=1
m[i]=2
m[i]=3
m[i]=4
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice2
Theenteredarrayis
1
2
3
4
0
1.create
2.display
3.search
4.erase
5.insert
6.exit

Enterthechoice5
Entertheno.ofelementstobeinserted3
Insertthevariables99
88
77
1.create
2.display
3.search
4.erase
5.insert
6.exit
Enterthechoice6

3. LINKED LIST IMPLEMENTATION OF LIST ADT


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
}
*head,*temp,*nnode,*prev;
class sll
{
public:
sll()
{
head=NULL;
}
intn,pos,sea,f,del;
void create();
void display();
void delbeg();
void delend();
void delpos();
void search();
void insbeg();
void inspos();
void insend();
};
void sll::create()
{

cout<<"\nEnter the no. of elements to be created";


cin>>n;
for(inti=0;i<n;i++)
{
nnode=new node;
cout<<"\nEnter data";
cin>>nnode->data;
nnode->next=NULL;
if(head==NULL)
{
head=nnode;
temp=nnode;
}
else
{
temp->next=nnode;
temp=temp->next;
}
}
}
void sll::display()
{
temp=head;
while(temp->next!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
cout<<"\n"<<temp->data;
}
void sll::delend()
{
temp=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
cout<<"\n"<<temp->data<<"is deleted";
delete temp;
}
void sll::delpos()
{
temp=head;
cout<<"\nEnter the element to be deleted";
cin>>del;
while(temp->next!=NULL)
{
if(temp->data==del)
{
prev->next=temp->next;

delete temp;
break;
}
prev=temp;
temp=temp->next;
}
}
void sll::delbeg()
{
temp=head;
head=head->next;
delete temp;
}
void sll::inspos()
{
temp=head;
nnode=new node;
cout<<" enter the element after which new element is to be inserted";
cin>>pos;
while(temp->next!=NULL)
{
if(pos==temp->data)
{
cout<<"\nEnter data to be inserted";
cin>>nnode->data;
nnode->next=temp->next;
temp->next=nnode;
break;
}
else
{
temp=temp->next;
}
}
}
void sll::insend()
{
temp=head;
nnode=new node;
cout<<"\n enter the data:";
cin>>nnode->data;
nnode->next=NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nnode;
}
void sll::insbeg()
{
temp=head;
nnode=new node;

cout<<"enter the data:";


cin>>nnode->data;
nnode->next=head;
head=nnode;
}
void sll::search()
{
temp=head;
cout<<"enter the element to be searched:";
cin>>sea;
while(temp->next!=NULL)
{
if(temp->data==sea)
{
f=1;
break;
}
else
{
temp=temp->next;
}
}
if(f==1)
{
cout<<"\nElement found";
}
else
{
cout<<"\nElement not found";
}
}
void main()
{
clrscr();
int m;
sll s;
do
{
cout<<"\n1.create"<<"\n"<<"2.display"<<"\n"<<"3.search"<<"\n"<<"4.deletion in
begining"<<"\n"<<"5.deletion in end"<<"\n"
<<"6.deletion of selected element"<<"\n"<<"7.insertion in beginning"<<"\n"<<"8.insertion in
end"<<"\n"<<"9.insertion in given position\n";
cout<<"\nEnter the choice";
cin>>m;
switch(m)
{
case 1: s.create();
break;
case 2: s.display();
break;
case 3: s.search();
break;

case 4: s.delbeg();
s.display();
break;
case 5: s.delend();
s.display();
break;
case 6: s.delpos();
s.display();
break;
case 7: s.insbeg();
s.display();
break;
case 8: s.insend();
s.display();
break;
case 9: s.inspos();
s.display();
break;
}
}while(m<10);
getch();
}
OUTPUT
SINGLE LINKED LIST
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter you choice:1
enter the number of element to be created:5
enter the new data:10
enter the new data:20
enter the new data:30
enter the new data:40
enter the new data:50
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit

enter your choice:2


10
20
30
40
50
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter your choice:3
enter the element to be searched:30
element found
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter your choice:4
20
30
40
50
1.create
2.display
3.search
4.delete at the beginning
5.delete at the end
6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter your choice:5
20
30
40
1.create
2.display
3.search
4.delete at the beginning

5.delete at the end


6.delete at the given position
7.insert at the beginning
8.insert at the end
9.insert at the given position
10.exit
enter your choice:10

4.

CURSOR IMPLEMENTATION OF LIST ADT

==============
//cursorHeader.h
==============
#include<iostream.h>
#include<conio.h>
#define SPACE_SIZE 10
struct Node
{
int data;
int Next;
};
typedef int PtrToNode;
typedef PtrToNode POSITION;
typedef PtrToNode LIST;
struct Node cursor[SPACE_SIZE];
void InitializeCursor()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
{
cursor[i].Next=i+1;
cursor[i].data=0;
}
cursor[SPACE_SIZE-1].Next=-1;
}
POSITION CursorAlloc()
{
POSITION P;
P=cursor[0].Next;
cursor[0].Next=cursor[P].Next;
cursor[P].data=-1;

cursor[P].Next=-1;
return P;
}
void CursorFree(POSITION P)
{
cursor[P].Next=cursor[0].Next;
cursor[0].Next=P;
cursor[P].data=0;
}
void Insert(int X,POSITION P)
{
POSITION Temp;
Temp=CursorAlloc();
if(Temp==-1)
cout<<"\nOut of space";
else if(cursor[P].data==0)
cout<<"\nPosition is not in the list";
else
{
cursor[Temp].data=X;
cursor[Temp].Next=cursor[P].Next;
cursor[P].Next=Temp;
}
}
int IsLast(POSITION P)
{
return cursor[P].Next==-1;
}
int IsEmpty(LIST L)
{
return cursor[L].Next==-1;
}
POSITION Find(int X,LIST L)
{
POSITION Temp;
Temp=cursor[L].Next;
while(Temp!=-1&&cursor[Temp].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
POSITION FindPrevious(int X,LIST L)
{
POSITION Temp;
Temp=L;
while(Temp!=-1&&cursor[cursor[Temp].Next].data!=X)
Temp=cursor[Temp].Next;
return Temp;
}
void Delete(int X,LIST L)
{
POSITION P,Temp;
P=FindPrevious(X,L);

if(!IsLast(P))
{
Temp=cursor[P].Next;
cursor[P].Next=cursor[Temp].Next;
CursorFree(Temp);
}
}
void MakeEmpty(LIST L)
{
while(!IsEmpty(L))
Delete(cursor[cursor[L].Next].data,L);
}
void Display()
{
int i;
for(i=0;i<=SPACE_SIZE-1;i++)
cout<<"\n"<<i<<"\t"<<cursor[i].data<<"\t"<<cursor[i].Next;
}

==============
// cursorimpl.cpp
==============
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include"cursorHeader.h"
class cursorDemo
{
public:
LIST L;
cursorDemo()
{
L=-1;
}
POSITION P;
int choice,place,x;
void menu();
};
void cursorDemo::menu()
{
while(1)
{
cout<<"\n1.Create\n2.Insert\n3.Delete\n4.MakeEmpty\n5.Display\n6.Find\n7.Exit";
cout<<"\nEnter ur choice:\t";
cin>>choice;
switch(choice)
{
case 1:
if(L==-1)

{
InitializeCursor();
L=CursorAlloc();
cout<<"List created.. ";
}
else
cout<<"\nList is already created";
break;
case 2:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhere u want to insert?";
cin>>place;
cout<<"\nEnter the element to insert";
cin>>x;
Insert(x,place);
}
break;
case 3:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhich element you want to delete?";
cin>>x;
Delete(x,L);
}
break;
case 4:
if(L==-1)
cout<<"\nList is not yet initialized";
else
MakeEmpty(L);
break;
case 5:
if(L==-1)
cout<<"\nList is not yet initialized";
else
Display();
break;
case 6:
if(L==-1)
cout<<"\nList is not yet initialized";
else
{
cout<<"\nWhich element you want to search?";
cin>>x;
P=Find(x,L);
cout<<"\nThe element is at "<<P;
}
break;
case 7:
exit(0);
default:
cout<<"Enter a valid choice..";

}
cout<<"\nEnter any key to continue..";
getch();
clrscr();
}
}
void main()
{
clrscr();
cout<<"*** Cursor Implementation of List ADT ***\n";
cursorDemo obj;
obj.menu();
}
Note
1. Save both the files cursorHeader.h and cursorimpl.cpp and execute the cpp file

5.

STACK ADT

a. ARRAY IMPLEMENTATION
#include<iostream.h>
#include<conio.h>
#define size 3
class stack
{
public:
int s[size];
inttop,i,n;
stack()
{
top=-1;
}
intisempty();
intisfull();
void display();
void push(int n);
void pop();
};
int stack::isempty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}

int stack::isfull()
{
if(top==size-1)
{
return 1;
}
else
{
return 0;
}
}
void stack::push(int n)
{
{
top++;
s[top]=n;
cout<<"\nelement"<<n<<"is inserted";
}
}
void stack::pop()
{
int a;
a=top;
top--;
cout<<"\nThe element"<<s[a]<<"is deleted";
}
void stack::display()
{
if(top==-1)
{
cout<<"\nNO ELEMENTS IN STACK";
}
else
{
for(i=top;i>=0;i--)
{
cout<<"\n"<<s[i];
}
}
}
void main()
{
clrscr();
stack obj;
intch,item,n;
do
{
cout<<"\n1.push\n2.pop\n3.display\n";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1: if(obj.isfull())
\cout<<"\nSTACK OVERFLOW:INSERTION/CREATION IMPOSSIBLE";
else
{

cout<<"\nEnter the element to be pushed";


cin>>item;
obj.push(item);
obj.display();
}
break;
case 2: if(obj.isempty())
cout<<"\nSTACK UNDERFLOW:DELETION IMPOSSIBLE";
else
{
obj.pop();
obj.display();
}
break;
case 3: obj.display();
break;
default:cout<<"\nEnter the correct option";
}
} while(ch<4);
getch();
}

OUTPUT
Array implementation using stack adt
1.push
2.pop
3.display
4.exit
Enter your choice:1
Enter the element:6
Element 6 is insersted on to the stack
1.push
2.pop
3.display
4.exit
Enter your choice:1
Enter the element:5
Element 5 is inserted on to the stack
1.push
2.pop
3.display
4.exit
Enter your choice:3
6
5
1.push
2.pop
3.display
4.exit
Enter your choice:1
Stack is over flow you cannot insert an element
1.push
2.pop
3.display
4.exit
Enter your choice:2
Element 6 is deleted from the stack
1.push
2.pop
3.display
4.exit
Enter your choice:3
5
1.push
2.pop
3.display
4.exit
enter your choice:4

b. Linked List Implementation


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
}*snode,*top,*temp;
class llstack
{
public:
int n;
llstack()
{
top=NULL;
}
void push();
void pop();
void display();
void topstack();
};
void llstack::push()
{
cout<<"\nEnter the no. of elements to be inserted";
cin>>n;
for(inti=0;i<n;i++)
{
snode=new node;
cout<<"\nEnter the element to be inserted";
cin>>snode->data;

if(top==NULL)
{
snode->next=NULL;
top=snode;
}
else
{
snode->next=top;
top=snode;
}
}
}
void llstack::pop()
{
cout<<"\nDELETION";
if(top==NULL)
{
cout<<"\nstack underflow";
}
else
{
temp=top;
top=top->next;
delete temp;
}
}
void llstack::display()
{
temp=top;
while(temp->next!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
cout<<"\n"<<temp->data;
}
void llstack::topstack()
{
temp=top;
cout<<"\nThe element is:"<<temp->data;
}
void main()
{
clrscr();
llstack s;
intch;
do
{
cout<<"\n1.push\n2.pop\n3.display\n4.top element of the stack";
cout<<"\nenter the choice";
cin>>ch;
switch(ch)
{
case 1: s.push();
s.display();
break;
case 2: s.pop();

s.display();
break;
case 3: s.display();
break;
case 4: s.topstack();
break;
default: cout<<"\nEnter the correct choice";
break;
}
}while(ch<5);
getch();
}

OUTPUT
Linked list implememntation of stack
Menus
1.push
2.pop
3.display
Enter your choice:1
Enter the no.of elements to be inserted:2
Enter the element:2
Enter the element:12
Menus
1.push
2.pop
3.display
Enter your choice:3
12
2
Menus
1.push
2.pop
3.display
Enter your choice:4
Top element is 12
Menus
1.push
2.pop
3.display
Enter your choice :2
12 is deleted from stack
Menus
1.push
2.pop
3.display
Enter your choice:2
stack underflow cannot delete
Menus
1.push
2.pop
3.display
Enter your choice:4
Top element is 0

4.top element

5.exit

4.top element

5.exit

4.top element

5.exit

4.top element

5.exit

4.top element

5.exit

4.top element

5.exit

Menus
1.push
2.pop
Enter your choice:5

3.display

4.top element

5.exit

6. SOURCE FILES
(i) Program source file for stack application 1 Checking parenthesis
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//Inclusion of necessary header files is must for the execution of this program
#define SIZE 10
void main()
{
char item;
char ans,bracket[SIZE];
stk_class obj;
int i;
clrscr();
cout<<"*** STACK APPLICATION 1 - WELL FORMED PARANTHESIS ***\n\n";
cout<<"\n Enter an expression(add $ at end) : ";
cin>>bracket;
i=0;
if(bracket[i]==')')
cout<<"\n Expression never starts with opening paranthesis..";
else
{
do
{
while(bracket[i]=='(')
{
obj.push(bracket[i]);
i++;
}

while(bracket[i]==')')
{
item=obj.pop();
i++;
}
}
while(bracket[i]!='$');
if(!obj.stempty())
cout<<"\n No balanced paranthesis in the given expression";
else
cout<<"\n Entered expression has well formed parenthesis";
}
getch();
}

(ii) Array implementation of stack ADT


Stackar.h // file to be included for application 1 - parenthesis
#define SIZE 10
class stk_class
{
public:
char s[SIZE];
int top;
stk_class()
{
top=-1;
}
void push(char);
int stempty();
int stfull();
char pop();
};
void stk_class::push(char item)
{
top++;
s[top]=item;
}
int stk_class::stempty()
{
if(top==-1)
return 1;
else
return 0;

}
int stk_class::stfull()
{
if(top==SIZE)
return 1;
else
return 0;
}
char stk_class::pop()
{
char item;
item=s[top];
top--;
return(item);
}

Stk.h // file to be included for application 2 pot fix expression evaluation


#define MAX 10
class stk_class
{
double s[MAX];
int top;
public:
stk_class()
{
top=0;
}
void push(double val);
double pop();
int isempty();
int isfull();
};
int stk_class::isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int stk_class::isfull()
{
if(top==MAX)
return 1;
else
return 0;

}
void stk_class::push(double val)
{
top++;
s[top]=val;
}
double stk_class::pop()
{
double val;
val=s[top];
top--;
return(val);
}

(iii)

Linked list implementation of stack ADT

Stack1.h // file to be included for application 1 - parenthesis


class stk_class
{
private:
typedef struct stack
{
char data;
struct stack * next;
}node;
node *top;
public:
stk_class()
{
top=NULL;
}
void push(char item);
int stempty();
void pop();
};
void stk_class::push(char item)
{
node * New;
New=new node;
if(New==NULL)
cout<<"\n memory cannot be allocated \n";
else

{
New->data=item;
New->next=top;
top=New;
}
}
int stk_class::stempty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stk_class::pop()
{
node *temp;
temp=top;
top=top->next;
delete temp;
}

Stk2.h // file to be included for application 2 pot fix expression evaluation


class stk_class
{
/* data structure for the linked stack*/
typedef struct stack
{
char data;
struct stack *next;
}node;
public:
node *top;
stk_class()
{
top=NULL;
}
void push(char item);
char pop();
};
void stk_class::push(char item)
{
node *New;
New=new node;
New->data =item;
New->next=top;
top=New;
}
char stk_class::pop()
{
char item;

node *temp;
item=top->data;
temp=top;
top=top->next;
delete temp;
return item;
}

(iv)Program source file for stack application 2 Evaluating postfix expression


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//Inclusion of necessary header files is must for execution of this program
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<"*** STACK APPLICATION 2 - EVALUATION OF POSTFIX EXPRESSION ***\n\n";
cout<<"Enter the postfix expression\n";
cin>>exp;
len=strlen(exp);
exp[len]='$';/*append $ at the end a end marker*/
result=post(exp);
cout<<"The value of the expression is "<<result;
getch();
exit(0);
}
double post(char exp[])
{
stk_class obj;
char ch,*type;

double result ,val,op1,op2;


int i;
i=0;
ch=exp[i];
while(ch!='$')
{
if(ch>='0'&&ch<='9')
type="operand";
else if(ch=='+'||ch=='-'||ch=='^'||ch=='*'||ch=='/')
type="operator";
if(strcmp(type,"operand")==0)/* if the character is operator*/
{
val=ch-48;
obj.push(val);
}
else if(strcmp(type,"operator")==0)/* if it is operator*/
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case '+':result=op1+op2;
break;
case '-':result=op1-op2;
break;
case '*':result=op1*op2;
break;
case '/': result=op1/op2;
break;
case '^':result=pow(op1,op2);
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}
result-obj.pop();/*pop the result*/
return(result);
}
(v)

Program using stack header files

For the program code (i) and (iv), include the header files of (ii) and (iii) for its execution.

7. Stack Application Infix to Postfix Conversion


a. Stack ADT implemented as ARRAYS
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX 20
class intopo
{
public:
char stack[MAX];
int top;
intopo()
{
top=-1;
}
char pop();
void push(char item);
int prcd(char);
int isoperator();
int isoperator(char);
void convertip(char[],char[]);
};
int intopo::prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;

case '*':
case '/':return 4;
case '^':return 6;
case '(':
case ')':
case '#':return 1;
}
}
int intopo::isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':return 1;
default:return 0;
}
}
void intopo::push(char item)
{
top++;
stack[top]=item;
}
char intopo::pop()
{
char a;
a=stack[top];
top--;
return a;
}
void intopo::convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{

while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()
OUTPUT compilation
{
1
char infix[20],postfix[20];
clrscr();
*** Infix to postfix
intopo in;
conversion using stack
cout<<*** Infix to postfix conversion using stack array ***\n;
array ***
cout<<"Enter the valid infix string: ";
Enter the valid infix
cin>>infix;
string: 4+5*6-7
in.convertip(infix,postfix);
The corresponding postfix
cout<<"The corresponding postfix string is: ;
string is: 456*+7cout<<postfix;
getch();
OUTPUT compilation
}
2
*** Infix to postfix
conversion using stack
array ***
Enter the valid infix
string:a+b*c-d
The corresponding postfix
string is: abc*+d-

b. Stack ADT implemented as LINKED LIST


#include<iostream.h>
#include<conio.h>
#include<string.h>
class intopo
{
public:
struct stack
{
int data;
struct stack *next;
}*top,*nnode;
intopo()
{
top=NULL;
}
char pop();
void push(char item);
int prcd(char);
int isoperator();
int isoperator(char);
void convertip(char[],char[]);
};
int intopo::prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;

case '*':
case '/':return 4;
case '^':return 6;
case '(':
case ')':
case '#':return 1;
}
}
int intopo::isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':return 1;
default:return 0;
}
}
void intopo::push(char item)
{
nnode=new stack;
nnode->data=item;
nnode->next=NULL;
if(top==NULL)
top=nnode;
else
{
nnode->next=top;
top=nnode;
}
}
char intopo::pop()
{
char a;
a=top->data;
nnode=top;
top=top->next;
delete nnode;
return a;
}
void intopo::convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
nnode=new stack;
nnode->data='#';
nnode->next=top;
top=nnode;
for(i=0;i<strlen(infix);i++)

{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(top->data!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(top->data))
push(symbol);
else
{
while(prcd(symbol)<=prcd(top->data))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(top->data!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()
{
char infix[20],postfix[20];
clrscr();
intopo in;
cout<<"*** Infix to postfix conversion using linked stack ***\n";
cout<<"Enter the valid infix string: ";
cin>>infix;
in.convertip(infix,postfix);

cout<<"The corresponding postfix string is: ";


cout<<postfix;
getch();
}

OUTPUT compilation
1
*** Infix to postfix
conversion using linked
stack ***
Enter the valid infix
string: 4+5*6-7
The corresponding postfix
string is: 456*+7OUTPUT compilation
2
8.
a.

*** Infix to postfix


conversion using linked
stack ***
Enter the valid infix
string: a+b*c-d
The corresponding postfix
string is: abc*+d-

class
{

STACK APPLICATION STRING REVERSE


Array Implementation
#include<string.h>
#include<iostream.h>
#include<conio.h>
#define MAX 100
stackDemo

public:
int top,i,len;
char Stack[MAX];
void getData();
void push(char);
void display();
char pop();
};
void stackDemo :: push(char x)
{
if(top==MAX-1)
{
cout<<"stack overflow\n";
return;
}
Stack[++top]=x;
return;
}
char stackDemo :: pop(void)

{
char y;
y=Stack[top];
Stack[top--]=0;
return y;
}
void stackDemo :: getData()
{
char stk[20];
cout<<"\nEnter the string : ";
cin>>stk;
len=strlen(stk);
for(i=0;i<len;i++)
push(stk[i]);
}
void stackDemo :: display()
{
cout<<"\nThe reversed string is :";
for(i=0;i<len;i++)
cout<<pop();
}
int main()
{
char Stack[MAX];
clrscr();
stackDemo obj;
cout<<"*** STACK APPLICATION - STRING REVERSE *** \n";
obj.getData();
obj.display();
getch();
return 0;
}

OUTPUT
*** STACK
APPLICATION STRING
REVERSE ***
Enter the string :
stack application
The reversed
string is :
noitacilppa kcats

b. Linked List Implementation


#include<iostream.h>
#include<conio.h>
#include<string.h>
struct node
{
char data;
node *next;
}*snode,*top,*temp;
class llstack
{
public:
int n;
char stk[20];
llstack()
{
top=NULL;
}
void push(char);
char pop();
void display();
void getData();
};
void llstack::push(char c)
{
snode=new node;
snode->data=c;
if(top==NULL)

{
snode->next=NULL;
top=snode;
}
else
{
snode->next=top;
top=snode;
}
}
char llstack::pop()
{
char ch;
if(top!=NULL)
{
temp=top;
top=top->next;
ch=temp->data;
return (ch);
}
}
void llstack::display()
{
temp=top;
cout<<"\nThe reversed string is : ";
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
void llstack :: getData()
{
cout<<"\nEnter the string : ";
cin>>stk;
int len=strlen(stk);
for(int i=0;i<len;i++)
push(stk[i]);
}
void main()
{
clrscr();
llstack s;
cout<<"*** Linked stack implementation - String Reverse ***\n";
s.getData();
s.display();
getch();
}

OUTPUT
*** Linked stack
implementation
String Reverse ***
Enter the string :
stack application
The reversed
string is :
noitacilppa kcats

9. STACK APPLICATION (EXPRESSION CONVERSION) INFIX TO PREFIX


a. Array Implementation
#include<string.h>
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include "stackar.h"
class infix
{
public:
char infx[50],prfx[50],ch,elem;
int i,k;
infix()
{
i=0;
k=0;
}
int pr(char);
void getData();
void display();
};
int infix::pr(char elem)
{
switch(elem)
{
case '#': return 0;
case ')': return 1;
case '+':

case '-': return 2;


case '*':
case '/': return 3;
}
}
void infix::getData()
{
cout<<"\n\nEnter the Infix Expression : ";
cin>>infx;
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{
h(ch);[-+++
+++else if(isalnum(ch)) prfx[k++]=ch;
else if( ch == '(')
+{
+while( s[top] != ')')
+e( pr(s[top]) >= pr(ch) )
+prfx[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
prfx[k++]=pop();
prfx[k]='\0';
strrev(prfx);
strrev(infx);
cout<<"\n\nGiven Infix Expression: "<<infx<<" Prefix Expression: "<<prfx;
}
void main()
{
clrscr();
cout<<"*** Stack Application - Infix to prefix conversion using stack array ***\n";
infix obj;
stack st;
obj.getData();
OUTPUT
getch();
}
*** Stack
Application - Infix
to prefix
conversion using
stack array ***
Enter the Infix
Expression : a+b
Given Infix
Expression: a+b Prefix
Expression: +ab

b. Linked List Implementation


#define SIZE 20
#include<string.h>
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
class infix
{
public:
char infx[50],prfx[50],ch,elem;
int i,k;
struct node
{
char data;
struct node *next;
}*top,*snode;
infix()
{
top=NULL;
i=0;
k=0;
}
void push(char);
char pop();
int pr(char);
void getData();
void display();

};
void infix :: push(char elem)
{
snode=new node;
snode->data=elem;
snode->next=NULL;
if(top==NULL)
top=snode;
else
{
snode->next=top;
top=snode;
}
}
char infix :: pop()
{
char c;
c=top->data;
top=top->next;
return c;
}
int infix::pr(char elem)
{
switch(elem)
{
case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void infix::getData()
{
cout<<"\n\nRead the Infix Expression : ";
cin>>infx;
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{
if( ch == ')') push(ch);
else if(isalnum(ch)) prfx[k++]=ch;
else if( ch == '(')
{
while( top->data != ')')
prfx[k++]=pop();
elem=pop(); /* Remove ) */
}
else
{

while( pr(top->data) >= pr(ch) )


prfx[k++]=pop();
push(ch);
}
}
while( top->data != '#')
prfx[k++]=pop();
prfx[k]='\0';
strrev(prfx);
strrev(infx);
cout<<"\n\nGiven Infix Expression: "<<infx<<" Prefix Expression: "<<prfx;
}

void main()
{
clrscr();
cout<<"*** Stack Application - Infix to prefix conversion using linked stack***\n";
infix obj;
obj.getData();
getch();
}

OUTPUT
*** Stack
Application - Infix
to prefix
conversion using
linked stack***
Enter the Infix
Expression : a+b
Given Infix
Expression: a+b Prefix
Expression: +ab

10. QUEUE ADT


a. Array Implementation
#include<iostream.h>
#include<conio.h>
#define SIZE 5
class arrqueue
{
public:
int q[SIZE],front,rear,i,n,x,a;
arrqueue()
{
front=0;
rear=-1;
}
void creation();
void dequeue();
void display();
void fiqueue();
};
void arrqueue::creation()
{
if(rear==SIZE)
{
cout<<"OVERFLOW";
}
else

{
cout<<"Enter the variable";
cin>>x;
rear++;
q[rear]=x;
}
}
void arrqueue::dequeue()
{
if(front==SIZE)
{
cout<<"\nUNDERFLOW";
}
else
{
q[a]=q[front];
front++;
cout<<"\nThe element"<<q[a]<<"is deleted";
}
}
void arrqueue::display()
{
for(i=front;i<=rear;i++)
{
cout<<"\n"<<q[i];
}
}
void arrqueue::fiqueue()
{
cout<<"\n"<<q[front]<<"is the first element in the queue";
}
void main()
{
clrscr();
intch;
arrqueue a;
do
{
cout<<"\n1.enqueue\n2.dequeue\n3.display\n4.first element in queue";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:a.creation();
break;
case 2:a.dequeue();
break;
case 3:a.display();
break;
case 4:a.fiqueue();
break;
default:cout<<"\nEnter the correct choice";

break;
}
}while(ch<5);
getch();
}

b. Linked List Implementation


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node* next;
}*front,*rear,*qnode,*temp;
class queuel
{
public:
intn,i;
queuel()
{
front=NULL;
}
void enqueue();
void dequeue();
void display();
void fiqueue();
};
void queuel::enqueue()
{
cout<<"\nEnter the no. of elements";
cin>>n;
for(i=0;i<n;i++)
{
qnode=new node;
cout<<"\nEnter the data";

cin>>qnode->data;
qnode->next=NULL;
{
if(front==NULL)
{
front=qnode;
rear=qnode;
}
else
{
rear->next=qnode;
rear=qnode;
}
}
}
}
void queuel::dequeue()
{
temp=front;
front=front->next;
cout<<"\nThe element"<<"\t"<<temp->data<<"\t"<<"is deleted";
delete temp;
}
void queuel::display()
{
temp=front;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
}
void queuel::fiqueue()
{
temp=front;
cout<<"\nThe first element of the queue is"<<"\t"<<temp->data;
}
void main()
{
clrscr();
intch;
queuel s;
do
{
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.First element";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
s.enqueue();
break;

case 2:
case 3:
case 4:
default:

s.dequeue();
break;
s.display();
break;
s.fiqueue();
break;
cout<<"\nwrong number";
break;

}
}while(ch<6);
getch();
}

11. SEARCH TREE ADT BINARY SEARCH ADT


#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
class BST
{
public:
void find(int, node **, node **);
void insert(node *, node *);
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *, int);
BST()
{

root = NULL;
}
};
int main()
{
int choice, num;
clrscr();
BST bst;
node *temp;
while (1)
{
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:

cout<<"Preorder Traversal of BST:"<<endl;


bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
getch();
clrscr();
}
}
void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{

if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;

}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
}
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
void BST::case_a(node *par, node *loc )
{
if (par == NULL)

{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else

case_b(parsuc, suc);
if (par == NULL)
root = suc;
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if (root == NULL)
{

cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<"
";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}

12. QUICK SORT


//C++ program to implement Quick sort
#include<iostream.h>
#include<conio.h>
classqsort
{
public:
int Partition(int[],int,int);
void Sort(int[],int,int);
};
intqsort::Partition(int a[], int beg, int end)
{
int p=beg, pivot=a[beg], loc;
for(loc=beg+1;loc<=end;loc++)
{
if(pivot>a[loc])
{
a[p]=a[loc];
a[loc]=a[p+1];
a[p+1]=pivot;

p=p+1;
}
}
return p;
}
voidqsort::Sort(int a[], int beg, int end)
{
if(beg<end)
{
int p=Partition(a,beg,end);
Sort(a,beg,p-1);
Sort(a,p+1,end);
}
}
void main()
{
clrscr();
qsort q;
int a[100],i,n,beg,end;
cout<<"\n*** QUICK SORT ***\n";
cout<<"Enter the number of Elements : ";
cin>>n;
cout<<"\nEnter the elements : \n";
for(i=1;i<=n;i++)
cin>>a[i];
beg=1;
end=n;
q.Sort(a,beg,end);
cout<<"\nThe sorted elements are : \n";
for(i=1;i<=n;i++)
cout<<a[i]<<"\t";
getch();
}