0% found this document useful (0 votes)
40 views70 pages

C++ Programs: Functions & Operator Overloading

The document contains programs demonstrating various concepts in C++ like function overloading, operator overloading, copy constructors, call by value, call by address, and default arguments. Each program contains the aim, algorithm, program code, output and result. The programs successfully demonstrate the intended concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views70 pages

C++ Programs: Functions & Operator Overloading

The document contains programs demonstrating various concepts in C++ like function overloading, operator overloading, copy constructors, call by value, call by address, and default arguments. Each program contains the aim, algorithm, program code, output and result. The programs successfully demonstrate the intended concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link].

A FUNCTION WITH DEFAULT ARGUMENTS


DATE :
AIM:
To write a C++ program to find the sum for the given variables using function with
default arguments.

ALGORITHM:
1) Start the program.
2) Declare the variables and functions.
3) Give the values for two arguments in the function declaration itself.
4) Call function sum() with three values such that it takes one default arguments.
5) Call function sum() with two values such that it takes two default arguments.
6) Call function sum() with one values such that it takes three default arguments
5) Inside the function sum(), calculate the total.
6) Return the value to the main() function.
7) Display the result.
8) Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
float sum(float a,int b=10,int c=15,int d=20);
int a=2,b=3,c=4,d=5;
clrscr();
cout<<"\nsum="<<sum(0);
cout<<"\nsum="<<sum(a,b,c,d);
cout<<"\nsum="<<sum(a,b,c);
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
getch();
}
float sum(float i, int j, int k, int l)
{
return(i+j+k+l);
}





OUTPUT:
sum=45
sum=14
sum=29
sum=40
sum=47
sum=32




































RESULT:
Thus, the given program for function with default arguments has been written and
executed successfully.
[Link].B IMPLEMENTATION OF CALL BY VALUE
DATE :
AIM:
To write a C++ program to find the value of a number raised to its power that
demonstrates a function using call by value.

ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Get two numbers as input
4) Call the function power to which a copy of the two variables is passed .
5) Inside the function, calculate the value of x raised to power y and store it in p.
6) Return the value of p to the main function.
7) Display the result.
8) Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
double power(int, int);
clrscr();
cout<<"Enter x,y:"<<endl;
cin>>x>>y;
cout<<x<<" to the power "<<y <<" is "<< power(x,y);
getch();
}
double power(int x,int y)
{
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}


OUTPUT:
ENTER X , Y:
2 3
2 TO THE POWER 3 IS 8






































RESULT:
Thus, the given program for implementation of call by value has been written and
executed successfully.
EX. NO: 1.C IMPLEMENTATION OF CALL BY ADDRESS
DATE:
AIM:
To write a c++ program and to implement the concept of Call by Address

ALGORITHM:
1. Start the program.
2. Include suitable header file.
3. Declare a function swap with two pointes variables arguments.
4. Declare and initialize the value as two variable in main().
5. Print the value of two variable before swapping.
6. Call the swap function by passing address of the two variable as arguments.
7. Print the value of two variable after swapping.
8. Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y);
int main()
{
clrscr();
int i,j;
i=10;
j=20;
cout<<"\n the value of i before swapping is:"<<i;
cout<<"\n the value of i before swapping is:"<<j;
swap (&i,&j);
cout<<"\n the value of i after swapping is:"<<i;
cout<<"\n the value of i after swapping is:"<<j;
getch();
return(0);
}
void swap(int *x,int*y)
{
int temp=*x;
*x=*y;
*y=temp;
}




OUTPUT:
The value of i before swapping is: 20
The value of j before swapping is: 10
The value of i after swapping is: 10
The value of j after swapping is: 20





































RESULT:
Thus to write a c++ program and to implement the concept of Call by Address was
successfully completed.
EX. NO:2 COPY CONSTRUCTORS
DATE:
AIM:
To write the c++ program and to implement the concept of copy constructor

ALGORITHM:
1. Start the program
2. Include suitable header file
3. Declare constructor with empty arguments and another ,object as arguments
4. Initialize the value of a variable by calling a constructor in main function
5. Assign the value of one object to another object
6. Execute the program
7. Print the statement
8. Stop the program

PROGRAM:
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code(){}
code(int a)
{
}
code(code&x)
{
id=[Link];
}
void display(void)
{
cout<<id;
}
};
int main()
{
clrscr();
code a(100);
code b(a);
code c=a;
code d;
d=a;
cout<<"\nid of a:"; [Link]();
cout<<"\nid of b:"; [Link]();
cout<<"\nid of c:"; [Link]();
cout<<"\nid of d:"; [Link]();
getch();
return 0;
}









OUTPUT:
id of A:100
id of B:100
id of C:100
id of D:100





















RESULT:
Thus to write the c++ program and implement the concept of copy constructor was
successfully completed.
EX. NO: 3.A OPERATOR OVERLOADING
DATE:
AIM:
To write a c++ program to concatenate two string using the concept of operator
Overloading.

ALGORITHM:
1. Start the program
2. Include suitable header file
3. Create a class with necessary variables and function
4. Get the value as two variables as concatenating
5. Overload an operator to concatenate two string
6. Execute the program
7. Stop the program

PROGRAM:
#include<iostream.h>
#include<string.h>
#include<conio.h>
const int bufsize=50;
class string
{
private:
char str[bufsize];
public:
string()
{
strcpy(str," ");
}
string(char*mystr)
{
strcpy(str,mystr);
}
void echo()
{
cout<<str;
}
string operator+(string s)
{
string temp=str;
strcat([Link],[Link]);
return temp;
}
};
void main()
{
clrscr();
string str1=" stud";
string str2=" ent";
string str3;
cout<<"\n before str3=str1=str2;.";
cout<<"\n str1=";
[Link]();
cout<<"\nstr2=";
[Link]();
cout<<"\n str3=";
[Link]();
str3=str1+str2;
cout<<"\n\n after str3=str1+str2:..";
cout<<"\n str1=";
[Link]();
cout<<"\n str2=";
[Link]();
cout<<"\n str3=";
[Link]();
getch();
}


OUTPUT:
Before str3=str1=str2;
str1=stud
str2=ent
str3=
After
str3=str1+str2:
str1=stud
str2=ent
str3=student








RESULT:
Thus to write a c++ program to concatenate two string using the concept of operator
overloading was successfully completed.
[Link]: 3.B UNARY OPERATOR OVERLOADING
DATE:
AIM:
To perform increment and decrement operations using unary operator overloading.

ALGORITHM:
1) Start the program.
2) Create Class unary with one constructors.
3) The constructor takes no arguments and is used to create objects that are
not initialised.
4) Declare a function operator ++ and -- inside the class unary which are the
function where overloading is done.
5) Create an object for the class.
6) Call the functions with declared object.
7) Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class unary
{
private:
int x,y,z;
public:
unary(void)
{
cout<< "Enter Any Three Integer Nos. : ";
cin>>x>>y>>z;
}
void display(void)
{
cout<< endl<< " The Three Nos. Are : "<< x<< " , "<< y<< " , "<< z;
}
void operator --()
{
x = --x;
y = --y;
z = --z;
}





void operator ++()
{
x = ++x;
y = ++y;
z = ++z;
}
};
void main()
{
clrscr();
unary s;
[Link]();
--s;
cout<< endl<< endl<< endl<< "******* The Decremented Values ********"<< endl;
[Link]();
++s;
cout<< endl<< endl<< endl<< "******* The Incremented Values ********"<< endl;
[Link]();
cout<< endl<< endl<< "***************************************";
getch();
}



OUTPUT:
Enter Any Three Integer Nos. :
4 7 9
The Three Nos. Are : 4 , 7 , 9
******* The Decremented Values ********
The Three Nos. Are : 3 , 6 , 8
******* The Incremented Values ********
The Three Nos. Are : 4 , 7 , 9










RESULT:
Thus, the given program for unary operator overloading has been written and executed
successfully.
EX. NO:3.C OVERLOADING BINARY OPERATOR
DATE:
AIM:
To write a c++ program to implement the concept Binary operator overloading.

ALGORITHM:
1. Start the program.
2. Include suitable header file.
3. Create a class with necessary variables and function.
4. Get the value as two variables.
5. Overload an binary operator.
6. Execute the program.
7. Stop the program.

PROGRAM:
#include <iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float image)
{
x=real;
y=image;
}
complex operator + (complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y;
}



int main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 =";
[Link]();
cout<<"c2 =";
[Link]();
cout<<"c3 =";
[Link]();
getch();
return 0;}








OUTPUT:
c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2














RESULT:
Thus to write a c++ program to concatenate two string using the concept Binary operator
overloading was successfully completed.
EX. NO: 3.D FUNCTION OVERLOADING
DATE:
AIM:
To write a c++ program to find the volume using function overloading concept

ALGORITHM:
1. Start the program
2. Include suitable header file
3. Declare the function of same name with different parameter
4. Define the function for calculating the volume
5. Call the respective functions
6. Print the volume
7. stop the program

PROGRAM:
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
clrscr();
cout<<volume of cube=;
cout<<volume(10)<<"\n";
cout<<volume of cylinder=;
cout<<volume(2.5,8)<<"\n";
cout<<volume of cubiod=;
cout<<volume(100,75,15)<<"\n";
return 0;
getch();
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
[Link] ( Department of Information Technology)
}
long volume(long l,int b,int h)
{
return(l*b*h);
}



OUTPUT:
volume of cube = 1000
volume of cylinder = 15700
volume of cubiod = 112500

































RESULT:
Thus to write a c++ program to find the volume using function overloading concept was
successfully completed.



EX. NO: 4.A MULTIPLE INHERITANCE
DATE:
AIM:
To write a c++ program to implement the concept of multiple inheritance.

ALGORITHM:
1. Start the program
2. Include the suitable header file
3. create a base class M and N
4. Declare a variable m and void getm function in class M
5. Declare a variable n and void getn function in class N
6. Derive a class P from the base class M&N and declare a function void display
7. Define the function void getm ,void getn and void display outside their respective class
8. Get the value for the variable in getm and getn function
9. Call the display function for displaying the output
10. Stop the program

PROGRAM:
#include<iostream.h>
class M
{
protected:
int m;
public:
void getm(int);
};
class N
{
protected:
int n;
public:
void getn(int);
};
class P:public M,public N
{
public:
void display(void);
};
void M::getm(int x)
{
m=x;
}
void N::getn(int y)
{
n=y;
}
void P::display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
cout<<"m*n="<<m*n<<"\n";
}
int main()
{
P p;
[Link](10);
[Link](20);
[Link]();
return 0;
}



OUTPUT:
m=10
n=20
m*n=200
















RESULT:
Thus to write a c++ program for multiple inheritance was successfully completed.
EX. NO: 4.B HYBRID INHERITANCE
DATE:
AIM:
To write a c++ program to implement the concept of hybrid inheritance.

ALGORITHM:
1. Start the program
2. Include suitable header files
3. Create a base class student and sports
4. In the base class student define the function void get number and put number
5. In the base class sports define the function void get score and void put score
6. Derive a class test form base student and define the function get mark and put mark
7. Derive a class result from test and sports class and define function void display
8. Get the value for get number ,get marks and get score function through main function
9. Call the display function in class result
10. Stop the program

PROGRAM:
#include<iostream.h>
class student
{
protected:
int roll_number;
public
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"ROLL NO:"<<roll_number<<"\n";
}
};
class test :public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"marks obtined:"<<"\n"
<<"part1="<<part1<<"\n"
<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports wt:"<<score<<"\n\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"total score:"<<total<<"\n";
}
int main()
{
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
return 0;
}


OUTPUT:
Roll no: 1234
Marks obtained:
Part1=27.5
Part2=33
Total score: 66.5





































RESULT:
Thus to write a c++ program for hybrid inheritance was successfully completed.
[Link]: 5 ARRAY IMPLEMENTATION OF LIST ADT
DATE :

AIM:
To Write a C++ Program for array implementation of list ADT.

ALGORITHM:
Step1: Create nodes first,last,next,prev and cur then set the value as NULL.
Step 2: Read the list operation type.
step 3: If operation type is create then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur node as NULL.
4. Assign first=last=cur.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be insert.
4. Availability of the position is true then assing cur's node as first and first=cur.
5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than postion.
1 .Assign prev as next
2. Next as prev of node.
3. Add count by one.
4. If prev as NULL then display the message INVALID POSITION.
5. If prev not qual to NULL then do the following steps.
1. Assign cur's node as prev's node.
2. Assign prev's node as cur.
Step5: If operation type is delete then do the following steps.
1. Read the position .
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
1. Assign cur as first.
2. Assign First as first of node.
3. Reallocate the cur from memory.
4. If position is last.
1. Move the current node to prev.
2. cur's node as Null.
3. Reallocate the Last from memory.
4. Assign last as cur.
5. If position is enter Mediate.
1. Move the cur to required postion.
2. Move the Previous to cur's previous position
3. Move the Next to cur's Next position.
4. Now Assign previous of node as next.
5. Reallocate the cur from memory.
step 6: If operation is traverse.
1. Assign current as first.
2. Repeat the following steps untill cur becomes NULL.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20],n,d,e,f,i;
void main()
{
int c;
char g='y';
clrscr();
do
{
cout<<"\n Main Menu";
cout<<"\n [Link] \n [Link] \n [Link] \n [Link] \n [Link] \n [Link]";
cout<<"\n enter your choice\n";
cin>>c;
switch(c)
{
case 1: create(); break;
case 2: deletion(); break;
case 3: search(); break;
case 4: insert(); break;
case 5: display(); break;
case 6: exit(0); break;
default:
cout<<"The given number is not between 1-5\n";
}
cout<<"\nDo u want to continue \n";
cin>>g;
clrscr();
}
while(g=='y'|| g=='Y');
getch();
}
void create()
{
cout<<"\n Enter the number\n";
cin>>n;
for(i=0;i<n;i++)
{
cin>>b[i];
}}
void deletion()
{
cout<<"Enter the limit u want to delete \n";
cin>>d;
for(i=0;i<n;i++)
{
if(b[i]==d)
{
b[i]=0;
}}}
void search()
{
cout<<"Enter the limit \n";
cin>>e;
for(i=0;i<n;i++)
{
if(b[i]==e)
{
cout<<"Value found the position\n"<<b[i];
}}}
void insert()
{
cout<<"enter how many number u want to insert \n";
cin>>f;
for(i=0;i<f;i++)
{
cin>>b[n++];
}}
void display()
{cout<<"\n\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n\n"<<b[i];
} }





OUTPUT:
Main Menu
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Enter the number
2
3
4
Do u want to continue
n

























RESULT:
Thus, the array implementation of list ADT program has been written and executed
successfully.
[Link] LINKED LIST IMPLEMENTATION OF LIST ADT
DATE :
AIM:
To Write a C++ Program for linked list implementation of list ADT.

ALGORITHM:

Step1: Create nodes first,last,next,prev and cur then set the value as NULL.
Step 2: Read the list operation type.
step 3: If operation type is create then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur link as NULL.
4. Assign first=last=cur.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be inserting.
4. Availability of the position is true then assign cur's link as first and first=cur.
5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than position.
1 .Assign prev as next
2. Next as prev of link.
3. Add count by one.
4. If prev as NULL then display the message INVALID POSITION.
5. If prev not qual to NULL then do the following steps.
1. Assign cur's link as prev's link.
2. Assign prev's link as cur.
Step5: If operation type is delete then do the following steps.
1. Read the position .
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
1. Assign cur as first.
2. Assign First as first of link.
3. Reallocate the cur from memory.
4. If position is last.
1. Move the current node to prev.
2. cur's link as Null.
3. Reallocate the Last from memory.
4. Assign last as cur.
5. If position is enter Mediate.
1. Move the cur to required position.
2. Move the Previous to cur's previous position
3. Move the Next to cur's Next position.
4. Now assign previous of link as next.
5. Reallocate the cur from memory.
step 6: If operation is traverse.
1. Assign current as first.
2. Repeat the following steps until cur becomes NULL.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class list
{
struct node
{
int data;
node *link;
}*p;
public:
void inslast(int);
void insbeg(int);
void insnext(int,int);
void delelement(int);
void delbeg();
void dellast();
void disp();
int seek(int);
list(){p=NULL;}
~list();
};
void list::inslast(int x)
{
node *q,*t;
if(p==NULL)
{
p=new node;
p->data=x;
p->link=NULL;
}
else
{
q=p;
while(q->link!=NULL)
q=q->link;
t=new node;
t->data=x;
t->link=NULL;
q->link=t;
}
cout<<"\n\nInserted successfully at the end..";
disp();
}
void list:: insbeg(int x)
{
node *q;
q=p;
p=new node;
p->data=x;
p->link=q;
cout<<"\n\nInserted successfully at the begining..";
disp();
}
void list::delelement(int x)
{
node *q,*r;
q=p;
if(q->data==x)
{
p=q->link;
delete q;
return;
}
r=q;
while(q!=NULL)
{
if(q->data==x)
{
r->link=q->link;
delete q;
return;
}
r=q;
q=q->link;
}
cout<<"\n\nElement you entered "<<x<<" is not found..";
}
void list:: delbeg()
{
cout<<"\n\nThe list before deletion:";
disp();
node *q;
q=p;
if(q==NULL)
{
cout<<"\n\nNo data is present..";
return;
}
p=q->link;
delete q;
return;
}
void list:: dellast()
{
cout<<"\n\nThe list before deletion:";
disp();
node *q,*t;
q=p;
if(q==NULL)
{
cout<<"\n\nThere is no data in the list..";
return;
}
if(q->link==NULL)
{
p=q->link;
delete q;
return;
}
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
}
list::~list()
{
node *q;
if(p==NULL) return;
while(p!=NULL)
{
q=p->link;
delete p;
p=q;
}
}
void list::disp()
{
node *q;
q=p;
if(q==NULL)
{
cout<<"\n\nNo data is in the list..";
return;
}
cout<<"\n\nThe items present in the list are \n";
while(q!=NULL)
{
cout<<q->data<<"\n";
q=q->link;
}
}
void list :: insnext(int value,int position)
{
node *temp,*temp1;
temp=p;
if(temp1==NULL)
{
temp1= new node;
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
}
for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
{
if(i==(position-1))
{
temp1= new node;
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
}
temp=temp->link;
}
cout<<"\n\nInserted successfully at "<<position;
disp();
}
int list::seek(int value)
{
node *temp;
temp=p;
int position=0;
while(temp!=NULL)
{
if(temp->data==value)
return position+1;
else
{
temp=temp->link;
position=position+1;
}
}
cout<<"\n\nElement "<<value<<" not found";
return 0;
}
void main()
{
list l;
int ch,v,p,ps;
do
{
clrscr();
cout<<"\n\nOperations on List..";
cout<<"\n\[Link]\[Link]\[Link]\[Link]\[Link]";
cout<<"\n\nEnter ur Option :";
cin>>ch;
switch(ch)
{
case 1:
clrscr();
cout<<"INSERTION";
cout<<"\n\[Link] at begining\[Link] at the end";
cout<<"\[Link] between two Nodes";
cout<<"\n\nEnter ur choice:";
cin>>ps;
cout<<"Enter the value to insert:";
cin>>v;
switch(ps)
{
case 1:
[Link](v);
break;
case 2:
[Link](v);
break;
case 3:
cout<<"\nEnter the position to insert the value:";
cin>>p;
[Link](v,p);
break;
default:
cout<<"\nThe choice is invalid";
return;
}
break;
case 2:
clrscr();
cout<<"\[Link] the first element\[Link] the last element";
cout<<"\[Link] the element to delete from the list";
cout<<"\n\nEnter ur choice:";
cin>>ps;
switch(ps)
{
case 1:
[Link]();
cout<<"\nThe list after deletion:";
[Link]();
break;
case 2:
[Link]();
cout<<"\nThe list after deletion:";
[Link]();
break;
case 3:
[Link]();
cout<<"\nEnter the element to delete : ";
cin>>v;
[Link](v);
cout<<"\nThe list after deletion:";
[Link]();
break;
default:
cout<<"\nThe option is invalid...";
break;
}
break;
case 3:
clrscr();
[Link]();
break;
case 4:
clrscr();
[Link]();
cout<<"\nEnter the element to search:";
cin>>v;
cout<<"\nThe position of the element "<< v<<" is "<<[Link](v);
getch();
break;
case 5:
exit(1);
default:
cout<<"\nThe option is invalid...";
return;
}
getch();
}while(ch!=5);
getch();
return;
}


OUTPUT:
Singly Linked List
[Link]
[Link]
[Link]
[Link]
Enter Your Choice : 1
Enter The Data: 10
10
[Link]
[Link]
[Link]
[Link]
Enter Your Choice : 2
Enter The Data: 30
Enter The Position: 1
30
10
[Link]
[Link]
[Link]
[Link]
Enter Your Choice : 3
Enter The Position : 2
List Is Empty




RESULT:
Thus, the linked list implementation of list ADT for singly linked list program has been
written and executed successfully.
[Link] CURSOR IMPLEMENTATION OF LIST ADT
DATE :

AIM:
To Write a C++ Program for Cursor implementation of list ADT.

ALGORITHM:
Step1: Create nodes first,last,next,prev and cur then set the value as NULL.
Step 2: Read the list operation type.
step 3: If operation type is create then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur link as NULL.
4. Assign first=last=cur.
Step 4: If operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be inserting.
4. Availability of the position is true then assign cur's link as first and first=cur.
5. If availability of position is false then do following steps.
1. Assign next as cur and count as zero.
2. Repeat the following steps until count less than position.
1 .Assign prev as next
2. Next as prev of link.
3. Add count by one.
4. If prev as NULL then display the message INVALID POSITION.
5. If prev not qual to NULL then do the following steps.
1. Assign cur's link as prev's link.
2. Assign prev's link as cur.
Step5: If operation type is delete then do the following steps.
1. Read the position .
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
1. Assign cur as first.
2. Assign First as first of link.
3. Reallocate the cur from memory.
4. If position is last.
1. Move the current node to prev.
2. cur's link as Null.
3. Reallocate the Last from memory.
4. Assign last as cur.
5. If position is enter Mediate.
1. Move the cur to required position.
2. Move the Previous to cur's previous position
3. Move the Next to cur's Next position.
4. Now assign previous of link as next.
5. Reallocate the cur from memory.
step 6: If operation is traverse.
1. Assign current as first.
2. Repeat the following steps until cur becomes NULL.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 5
struct node
{
int data;
int next;
};
typedef struct node NODE;
int avail,list = -1;
NODE curs[max];
void initial()
{
int i;
avail = 0;
for(i=0;i<max-1;i++)
curs[i].next=i+1;
curs[i].next=-1;
}
void create()
{
int n,i,item,temp;
cout<<"\nEnter the no of elements: ";
cin>>n;
//cout<<n;
if(n>=max)
cout<<"\n Size exists";
else
{
initial();
if(avail==-1)
{
cout<<"\nThere is no space to insert";
exit(0);
}
list = avail;
if(n==max-1) avail = -1;
else avail=n+1;
cout<<"\nEnter the elements one by one: ";
for(i=0;i<n;i++)
{
cout<<"\n Enter the "<<i+1<<"th element ";
cin>>item;
cout<<"\t";
//cout<<item;
curs[i+1].data=item;
}
curs[n].next=-1;
}
}
void display()
{
int i;
cout<<"\nCursor space: ";
cout<<"\n\nAvail = "<<avail<<" \t ";
cout<<"List = "<<list<<"\n";
cout<<"_______________";
cout<<"\n DATA NEXT \n";
cout<<"_______________";
i=0;
while(i<max)
{
cout<<"\n"<<curs[i].data<<" \t "<<curs[i].next;
cout<<"\n_______________";
i++;
}
}
void insbeg()
{
int item,i;
cout<<"\nEnter the item to be inserted: ";
cin>>item;
//cout<<item;
cout<<"\n";
i=avail;
avail=curs[avail].next;
curs[i].data=item;
curs[i].next=curs[list].next;
curs[list].next=i;
}
void insend()
{
int item,i;
cout<<"\nEnter the item to be inserted: ";
cin>>item;
//cout<<item;
cout<<endl;
i=list;
while(curs[i].next!=-1)
i=curs[i].next;
curs[i].next=avail;
avail=curs[avail].next;
i=curs[i].next;
curs[i].data=item;
curs[i].next=-1;
}
void insint()
{
int item,i,pos,count,temp;
cout<<"\nEnter the item to be inserted: ";
cin>>item;
//cout<<item;
cout<<endl;
cout<<"\nEnter the position of the item: ";
cin>>pos;
//cout<<pos;
cout<<endl;
i=list;
count=1;
while(count<pos)
{
i=curs[i].next;
count=count+1;
}
temp=avail;
avail=curs[avail].next;
curs[temp].data=item;
curs[temp].next=curs[i].next;
curs[i].next=temp;
}
void delbeg()
{
int i;
i=curs[list].next;
curs[list].next=curs[i].next;
curs[i].next=avail;
avail=i;
curs[avail].data=0;
if(curs[list].next==-1)
{
curs[list].next=avail;
avail=list;
list=-1;
}
}
void delend()
{
int i,prev;
i=list;
while(curs[i].next!=-1)
{
prev=i;
i=curs[i].next;
}
curs[prev].next=-1;
curs[i].next=avail;
avail=i;
curs[avail].data=0;
if(curs[list].next==-1)
{
curs[list].next=avail;
avail=list;
list=-1;
}
}
void delint()
{
int pos,i,count,prev;
cout<<"\nEnter the position: ";
cin>>pos;
//cout<<pos;
cout<<endl;
i=list;
count=1;
while(count<=pos)
{
prev=i;
i=curs[i].next;
count=count+1;
}
curs[prev].next=curs[i].next;
curs[i].next=avail;
avail=i;
curs[avail].data=0;
if(curs[list].next==-1)
{
curs[list].next=avail;
avail=list;
list=-1;
}
}
void main()
{
int ch;
clrscr();
do
{
cout<<"\n ***********************************************************";
cout<<"\n\t\t\t\t LIST\n";
cout<<" ***********************************************************";
cout<<"\n1. Create";
cout<<"\n2. Insert at begin";
cout<<"\n3. Insert at end";
cout<<"\n4. Insert at intermediate";
cout<<"\n5. Delete at begin";
cout<<"\n6. Delete at end";
cout<<"\n7. Delete at intermediate";
cout<<"\n8. Display";
cout<<"\n9. Exit";
cout<<"\n\nEnter your choice:";
cin>>ch;
//cout<<"\n"<<ch;
switch(ch)
{
case 1:
create();
break;
case 2:
if(avail==-1) cout<<"\nThere is no space";
else insbeg();
break;
case 3:
if(avail==-1) cout<<"\nThere is no space";
else insend();
break;
case 4:
if(avail==-1) cout<<"\nThere is no space";
else insint();
break;
case 5:
if(list==-1) cout<<"\nNo element to delete";
else delbeg();
break;
case 6:
if(list==-1) cout<<"\nNo element to delete";
else delend();
break;
case 7:
if(list==-1) cout<<"\nNo element to delete";
else delint();
break;
case 8:
display();
break;
case 9:
cout<<"\nEnd of the operation";
break;
default:
cout<<"\nEnter only 1 to 9: ";
}
}while(ch!=9);
}

OUTPUT:
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:1
1
Enter the no of elments: 4
4
Enter the elements one by one:
enter the element 2
2
enter the element 3
3
enter the element 4
4
enter the element 5
5

LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = -1 List = 0
______________
DATA NEXT
______________
0 1
_______________
2 2
_______________
3 3
_______________
4 4
_______________
5 -1
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:6
6
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = 4 List = 0
______________
DATA NEXT
______________
0 1
_______________
2 2
_______________
3 3
_______________
4 -1
_______________
0 -1
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:2
2
Enter the item to be inserted: 6
6
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = -1 List = 0
______________
DATA NEXT
______________
0 4
_______________
2 2
_______________
3 3
_______________
4 -1
_______________
6 1
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:7
7
Enter the position: 2
2
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = 1 List = 0
______________
DATA NEXT
______________
0 4
_______________
0 -1
_______________
3 3
_______________
4 -1
_______________
6 2
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:3
3
Enter the item to be inserted: 9
9
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = -1 List = 0
______________
DATA NEXT
______________
0 4
_______________
9 -1
_______________
3 3
_______________
4 1
_______________
6 2
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:5
5
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = 4 List = 0
______________
DATA NEXT
______________
0 2
_______________
9 -1
_______________
3 3
_______________
4 1
_______________
0 -1
_______________
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:4
4
Enter the item to be inserted: 21
21
Enter the position of the item: 2
2
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:8
8
Cursor space:
Avail = -1 List = 0
______________
DATA NEXT
______________
0 2
_______________
9 -1
_______________
3 4
_______________
4 1
_______________
21 3
_______________






LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Enter your choice:9






























RESULT:
Thus, the cursor implementation of list ADT for singly linked list program has been
written and executed successfully.

[Link]: 8 STACK ADT- ARRAY IMPLEMENTATION
DATE:
AIM:
To write a C++ Program to Stack ADT implementation using array

ALGORITHM:
Step 1: Define a stack size.
Step 2: Read the stack operation.
Step 3: Read the stack element.
Step 4: Check the stack operation is Push or Pop.
Step 5: If operation is push then check the stack status.
i. If stack status is over flow we cant push the element in to stack.
ii. Otherwise we can add the data into stack .
iii. Move top to next position.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
Clrscr();
int ch;
stack st;
while(1)
{
cout <<"\[Link] [Link] [Link] [Link]\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
[Link](ch);
break;
case 2: [Link](); break;
case 3: [Link]();break;
case 4: exit(0);
}
}
return (0);
}



OUTPUT:
[Link] [Link] [Link] [Link]
Enter ur choice2
stack under flow
[Link] [Link] [Link] [Link]



Enter ur choice1
enter the element2
inserted2
[Link] [Link] [Link] [Link]
Enter ur choice1
enter the element3
inserted3
[Link] [Link] [Link] [Link]
Enter ur choice2
deleted3
[Link] [Link] [Link] [Link]
Enter ur choice1
enter the element5
inserted5
[Link] [Link] [Link] [Link]
Enter ur choice3
5 2
[Link] [Link] [Link] [Link]
Enter ur choice4




















RESULT:
Thus, the stack ADT- array implementation program has been written and executed
successfully.
[Link] Stack ADT- Linked list Implementation
DATE :

AIM:
To Write a C++ Program to Stack ADT implementation using linked list

Algorithm :
Step 1: create a list.
i) Create a new empty node top.
ii) Read the stack element and store it in top's data area.
iii) Assign top's link part as NULL (i.e. top->link=NULL).
iv) Assign temp as top (i.e. temp=top).
Step 2: Read next stack operation.
i) If it is Create then go to step1.
ii) If it is Push then it process following steps
a) Check Main memory for node creation.
b) Create a new node top.
c) Read the stack element and store it in top's data area.
d) Assign top's link part as temp (i.e. top->link=temp).
e) Assign temp as top (i.e. temp=top).
iii) If it is pop then it process following steps
a) If top is NULL then display stack is empty.
b) Otherwise assign top as temp (i.e. top=temp, bring the top to top position)
c) Assign temp as temp's link. (i.e. temp=temp->link, bring the temp to top's previous
position).
d) Delete top from memory.
iv) If it is traverse then process the following steps
a) Bring the top to stacks top position(i.e. top=temp)
b) Repeat until top becomes NULL
i) Display the top's data.
ii) Assign top as top's link (top=top->link).

Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
class node
{
public:
class node *next;
int data;
};
class stack : public node
{
node *head;
int tos;
public:
stack()
{
tos=-1;
}
void push(int x)
{
if (tos < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else
{
node *temp,*temp1;
temp=head;
if(tos >= 4)
{
cout <<"stack over flow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
main()
{
Clrscr();
stack s1;
int ch;
while(1)
{
cout <<"\[Link]\[Link]\[Link]\[Link]\n enter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n enter a element";
cin >> ch;
[Link](ch);
break;
case 2: [Link]();break;
case 3: [Link]();
break;
case 4: exit(0);
}
}
return (0);
}




Output
[Link] [Link] [Link] [Link]
enter ru choice:1
enter a element23
[Link] [Link] [Link] [Link]
enter ru choice:1
enter a element67
[Link] [Link] [Link] [Link]
enter ru choice:3
23 67
[Link] [Link] [Link] [Link]
enter ru choice:2
[Link] [Link] [Link] [Link]
enter ru choice:3
23
[Link] [Link] [Link] [Link]
enter ru choice:2
[Link] [Link] [Link] [Link]
enter ru choice:2
stack under flow
[Link] [Link] [Link] [Link]
enter ru choice:4



















Result:
Thus, the stack ADT- linked list implementation program has been written and executed
successfully.

[Link].1 Queue ADT- Array Implementation
DATE:

AIM:
To write a C++ program to Queue ADT implementation using array

Algorithm
Step 1: Initialize the queue variables front =0 and rear = -1
Step 2: Read the queue operation type.
Step 3: Check the queue operations status.
i). If it is Insertion then do the following steps
1. Check rear < queue_size is true increment the rear by one and read the queue
element and also display queue. otherwise display the queue is full.
2. Go to step2.
ii). If it is deletion then do the following steps
1. Check rear< front is true then display the queue is empty.
2. Move the elements to one step forward (i.e. move to previous index ).
3. Decreases the rear value by one (rear=rear-1).
4. Display queue
5. Go to step2.

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
main()
{
Clrscr();
int ch;
queue qu;
while(1)
{
cout <<"\[Link] [Link] [Link] [Link]\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
[Link](ch);
break;
case 2: [Link](); break;
case 3: [Link]();break;
case 4: exit(0);
}
}
return (0);
}




OUTPUT
[Link] [Link] [Link] [Link]
Enter ur choice1
enter the element21
inserted21
[Link] [Link] [Link] [Link]
Enter ur choice1
enter the element22
inserted22
[Link] [Link] [Link] [Link]
Enter ur choice1
enter the element16
inserted16
[Link] [Link] [Link] [Link]
Enter ur choice3
21 22 16
[Link] [Link] [Link] [Link]
Enter ur choice2
deleted21
[Link] [Link] [Link] [Link]
Enter ur choice3
22 16
[Link] [Link] [Link] [Link]
Enter ur choice

















Result:
Thus, the queue ADT- array implementation program has been written and executed
successfully.

[Link].2 Queue ADT- Linked list Implementation
DATE:

AIM:
To Write a C++ Program to Queue ADT implementation using linked list

Algorithm
Step 1: Initialize the queue variables front =0 and rear = -1
Step 2: Read the queue operation type.
Step 3: Check the queue operations status.
i). If it is Insertion then do the following steps
3. Check rear not equal to null is true increment the rear by one and read the queue
element and also display queue. otherwise display the queue is full.
4. Go to step2.
ii). If it is deletion then do the following steps
6. Check rear< front is true then display the queue is empty.
7. Move the elements to one step forward (i.e. move to previous index ).
8. Decreases the rear value by one (rear=rear-1).
9. Display queue
10. Go to step2.

Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
class node
{
public:
class node *next;
int data;
};
class queue : public node
{
node *head;
int front,rare;
public:
queue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if (rare < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
rare ++;
}
else
{
node *temp,*temp1;
temp=head;
if(rare >= 4)
{
cout <<"queue over flow";
return;
}
rare++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
} }
void display()
{
node *temp;
temp=head;
if (rare < 0)
{
cout <<" queue under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}}
void pop()
{
node *temp;
temp=head;
if( rare < 0)
{
cout <<"queue under flow";
return;
}
if(front == rare)
{
front = rare =-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
Clrscr();
queue s1;
int ch;
while(1)
{
cout <<"\[Link]\[Link]\[Link]\[Link]\n enter ru choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
[Link](ch); break;
case 2: [Link]();break;
case 3: [Link]();break;
case 4: exit(0);
}}
return (0);
}

















OUTPUT
[Link] [Link] [Link] [Link]
enter ru choice:1
enter a element23
[Link] [Link] [Link] [Link]
enter ru choice:1
enter a element54
[Link] [Link] [Link] [Link]
enter ru choice:3
23 54
[Link] [Link] [Link] [Link]
enter ru choice:2
[Link] [Link] [Link] [Link]
enter ru choice:2
[Link] [Link] [Link] [Link]
enter ru choice:2
queue under flow
[Link] [Link] [Link] [Link]
enter ru choice:4



















Result
Thus, the queue ADT- linked list implementation program has been written and executed
successfully.




[Link] Search Tree ADT Binary Search Tree
DATE:

AIM:
To Write a C++ Program to to perform Insert, Delete, Search an element into a binary
search tree.

Algorithm
Create a new instance of BinaryTree.
Create a new instance of TreeTest and select BinaryTree instance in the object
bench as the parameter in the constructor.
Call the populate method of TreeTest instance.
Inspect the BinaryTree. Its attributes are a left subtree, a right subtree and a data item.

Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
main()
{
clrscr();
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
{
cout <<"[Link]\[Link]\[Link]\[Link]\[Link]\nEnter your
choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in tree";
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"\n";
break;
case 4:
cout <<"enter the element to search:";
cin >> x;
y=search(1);
if(y == -1) cout <<"no such element in tree";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}
void insert(int s,int ch )
{
int x;
if(t==1)
{
tree[t++]=ch;
return;
}
x=search1(s,ch);
if(tree[x]>ch)
tree[2*x]=ch;
else
tree[2*x+1]=ch;
t++;
}
void delte(int x)
{
if( tree[2*x]==-1 && tree[2*x+1]==-1)
tree[x]=-1;
else if(tree[2*x]==-1)
{ tree[x]=tree[2*x+1];
tree[2*x+1]=-1;
}
else if(tree[2*x+1]==-1)
{ tree[x]=tree[2*x];
tree[2*x]=-1;
}
else
{
tree[x]=tree[2*x];
delte(2*x);
}
t--;
}
int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}
void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}
int search1(int s,int ch)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
search1(2*s,ch);
else search1(2*s+1,ch);
}

OUTPUT
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice:3
no element in tree:
0123456789011121314151617181920212223242526272829303132
1. INSERT
[Link]
[Link]
[Link]
[Link]
Enter your choice:1
Enter the element to insert 10
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice: 4
Enter the element to search: 10
10 is in 1 position
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice:5

Result:
Thus, the t program to perform Insert, Delete, Search an element into a binary tree has
been written and executed successfully.






[Link] Heap Sort
DATE:

AIM:
To Write a C++ Program to perform heap sort.

Algorithm:
Step I: The user inputs the size of the heap(within a specified limit).The program generates
a corresponding binary tree with nodes having randomly generated key Values.
Step II: Build Heap Operation
Step III: Remove maximum element:The program removes the largest element of the
heap(the root) by swapping it with the last element.
Step IV: The program executes Heapify(new root) so that the resulting tree satisfies the
heap property.
Step V: Goto step III till heap is empty

Program:
#include<iostream.h>
#include<stdlib.h>
class sorting
{
private:
int n,size;
double *minheap;
public:
void insert_minheap(double);
double delete_one_minheap();
void input();
void output();
};
void sorting::insert_minheap(double n)
{
if(size>=9)
{
cout<<array overflow ;
exit(0);
}
minheap[++size]=n;
//Reorder the heap
int k=size;
while(k>1) //k has a parent
{
if(minheap[k]<minheap[k/2])
{
double t=minheap[k];
minheap[k]=minheap[k/2];
minheap[k/2]=t;
k/=2;
}
else
break;
}
}
double sorting::delete_one_minheap()
{
if(size<1)
return -1;
double val;
val=minheap[1];
minheap[1]=minheap[size];
size;
//Reorder the heap by moving down
int k=1;
int newk;
while(2*k<=size) //k has atleast one chaild
{
//Set newk to the index of the smallest chaild of k
if(2*k==size) //if k has only left chaid
{
newk=2*k;
}
else //k has two chailds
{
if(minheap[2*k]<minheap[2*k+1])
newk=2*k;
else
newk=2*k+1;
}
if(minheap[k]<minheap[newk])
break;
else
{
double t;
t=minheap[k];
minheap[k]=minheap[newk];
minheap[newk]=t;
k=newk;
}
}
return val;
}
void sorting::input()
{
cout<<Enter how many numbers you are going to enter for sorting :;
cin>>n;
minheap=new double[n+1];
/********** Construct a heap with the input elements *******/
size=0;
cout<<Now enter the elements\n;
double number;
for(int i=1;i<=n;i++)
{
cin>>number;
insert_minheap(number);
}}
void sorting::output()
{
cout<<The sorted numbers are ::\n;
for(int i=1;i<=n;i++)
cout<<delete_one_minheap()<<\t;
cout<<endl;
}
int main()
{
clrscr();
sorting obj;
[Link]();
[Link]();
getch();
}

Output
Enter how many numbers you are going to enter for sorting :5
Now enter the elements
9
1
8
2
6
The sorted numbers are:
1 2 6 8 9

Result:
Thus, the heap sort program has been written and executed successfully.




[Link] Quick Sort
DATE:

AIM:
To Write a C++ Program to perform quick sort.

Algorithm
Pick an element, called a pivot, from the list.
Reorder the list so that all elements which are less than the pivot come before the
pivot and so that all elements greater than the pivot come after it (equal values can
go either way). After this partitioning, the pivot is in its final position. This is called
the partition operation.
Recursively sort the sub-list of lesser elements and the sub-list of greater elements

Program
#include<iostream.h>
#include<conio.h>
class QuiSort
{
int i,j,pivot;
public:
int n,a[20];
void quick(int a[],int left,int right);
void swap(int a[],int i,int j);
};
void QuiSort :: quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i<last)
i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void QuiSort :: swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void main()
{
QuiSort obj;
clrscr();
cout<<"\n\nQUICK SORT";
cout<<"\n\nEnter the limit : ";
cin>>obj.n;
clrscr();
cout<<"\n\nEnter the element\n\n";
for(int i=0;i<obj.n;i++)
cin>>obj.a[i];
[Link](obj.a,0,obj.n-1);
cout<<"\n\nThe sorted list is \n\n";
for(i=0;i<obj.n;i++)
cout<<obj.a[i]<<" ";
getch();
}

OUTPUT:
Enter the limit: 5
Enter the elements
5
4
3
2
1
The sorted list is
1 2 3 4 5




Result

Thus, the quick sort program has been written and executed successfully.

You might also like