CS6311 - Programming and Data Structure Laboratory II
CS6311 - Programming and Data Structure Laboratory II
COM
SATHIYAMANGALAM
Year/Semester : II / 3
PREPARED BY
P.PARVATHI ASSOC.PROF/CSE
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM:
To write a C++ program to find the sum for the given variables using function with
default arguments.
ALGORITHM:
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
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
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);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
getch();
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM:
To write a C++ program to find the value of a number raised to its power that demonstrates a
ALGORITHM:
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.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
int x,y;
clrscr();
cout<<"Enter x,y:"<<endl;
cin>>x>>y;
getch();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
Output:
ENTER X ,Y:
23
2 TO THE POWER 3 IS 8
RESULT:Thus, the given program for implementation of call by value has been written and
executed successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
ALGORITHM:
6. Call the swap function by passing address of the two variable as arguments
CODING:
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int i,j;
i=10;
j=20;
swap (&i,&j);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
getch();
return(0);
int temp=*x;
*x=*y;
*y=temp;
OUTPUT:
RESULT:
Thus to write a c++ program and to implement the concept of Call by Address was
successfully completed.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
To write the c++ program and to implement the concept of copy constructor
ALGORITHM:
CODING:
#include<iostream.h>
#include<conio.h>
class code
int id;
public:
code(){}
code(int a)
id=a;
code(code&x)
{ id=x.id;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
void display(void)
cout<<id;
};
int main()
clrscr();
code a(100);
code b(a);
code c=a;
code d;
d=a;
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.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
To write a c++ program to concatenate two string using the concept of operator
Overloading.
ALGORITHM:
CODING:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
private:
char str[bufsize];
public:
string()
strcpy(str," ");
string(char*mystr)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
strcpy(str,mystr);
void echo()
cout<<str;
string operator+(string s)
string temp=str;
strcat(temp.str,s.str);
return temp;
};
void main()
clrscr();
string str3;
cout<<"\n str1=";
str1.echo();
cout<<"\nstr2=";
str2.echo();
cout<<"\n str3=";
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
str3.echo();
str3=str1+str2;
cout<<"\n str1=";
str1.echo();
cout<<"\n str2=";
str2.echo();
cout<<"\n str3=";
str3.echo();
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM:
ALGORITHM:
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
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class unary
private:
int x,y,z;
public:
unary(void)
cin>>x>>y>>z;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
void display(void)
cout<< endl<< " The Three Nos. Are : "<< x<< " , "<< y<< " , "<< z;
x = --x;
y = --y;
z = --z;
{ x = ++x;
y = ++y;
z = ++z;
};
void main()
clrscr();
unary s;
s.display();
--s;
cout<< endl<< endl<< endl<< "******* The Decremented Values ********"<< endl;
s.display();
++s;
cout<< endl<< endl<< endl<< "******* The Incremented Values ********"<< endl;
s.display();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
getch();
Output:
479
RESULT:
Thus, the given program for unary operator overloading has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
ALGORITHM:
CODING:
#include <iostream.h>
#include<conio.h>
class complex
float x;
float y;
public:
complex(){}
x=real;
y=image;
void display(void);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
};
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 =";
c1.display();
cout<<"c2 =";
c2.display();
cout<<"c3 =";
c3.display();
getch();
return 0;}
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
To write a c++ program to find the volume using function overloading concept
ALGORITHM:
CODING:
#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;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
getch();
int volume(int s)
{ return(s*s*s);
return(3.14*r*r*h);
return(l*b*h);}
OUTPUT:
RESULT: Thus to write a c++ program to find the volume using function overloading concept was
successfully completed.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM
ALGORITHM
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
CODING:
#include<iostream.h>
class M
protected:
int m;
public:
void getm(int);
};
class N
protected:
int n;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
public:
void getn(int);
};
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;
p.getm(10);
p.getn(20); p.display();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
return 0;
OUTPUT:
m=10
n=20
m*n=200
RESULT:
Thus to write a c++ program for multiple inheritance was successfully completed.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
AIM:
ALGORITHM:
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
CODING:
#include<iostream.h>
class student
protected:
int roll_number;
public
void get_number(int a)
roll_number=a;
void put_number(void)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"ROLL
NO:"<<roll_number<<"\n";
};
protected:
float
part1,part2;
public:
part1=x;
part2=y;
void put_marks(void)
cout<<"marks obtined:"<<"\n"
<<"part1="<<part1<<"\n"
<<"part2="<<part2<<"\n";
};
class sports
protected:
float score;
public:
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
void get_score(float s)
score=s;
void put_score(void)
cout<<"sports wt:"<<score<<"\n\n";
};
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);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
return 0;
OUTPUT:
Marks obtained:
Part1=27.5
Part2=33
RESULT:
Thus to write a c++ program for hybrid inheritance was successfully completed.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
ALGORITHM:
Step1: Create nodes first,last,next,prev and cur then set the value as
NULL.
1. Allocate
4. Assign first=last=cur.
1. Allocate
first=cur.
5. If availability of position
1. Assign
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
Step5:
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
4. If position is last.
5. If position
is enter Mediate.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cin>>c;
switch(c)
default:
cout<<"\nDo u
cin>>g;
clrscr();
while(g=='y'|| g=='Y');
getch();
void create()
cin>>n;
for(i=0;i<n;i++)
cin>>b[i];
}}
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
void deletion()
cin>>d;
for(i=0;i<n;i++)
if(b[i]==d)
b[i]=0;
}}}
void search()
\n";
cin>>e;
for(i=0;i<n;i++)
if(b[i]==e)
}}}
void insert()
\n";
cin>>f;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Do u want to continue
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
Result:
Thus, the
array implementation of list ADT program has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM:
Algorithm:
Step1: Create nodes first,last,next,prev and cur then set the value as NULL.
1. Allocate
4. Assign first=last=cur.
4. Availability of the position is true then assign cur's link as first and first=cur.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
4. If position is last.
Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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;
disp();
node *q;
q=p;
p=new
node;
p->data=x;
p->link=q;
disp();
void list::delelement(int x)
{ node *q,*r;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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;
disp();
node *q;
q=p;
if(q==NULL)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"\n\nNo data is
present..";
return;
p=q->link;
delete q;
return;
disp();
node *q,*t;
q=p;
if(q==NULL)
return;
if(q->link==NULL)
{ p=q->link;
delete q;
return;
while(q->link->link!=NULL)
q=q->link;
q->link=NULL;
return;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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)
return;
while(q!=NULL)
{ cout<<q->data<<"\n";
q=q->link;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
node *temp,*temp1;
temp=p;
if(temp1==NULL)
temp1->data=value;
temp1->link=NULL;
p=temp1;
return;
if(i==(position-1))
temp1->data= value;
temp1->link=temp->link;
temp->link=temp1;
temp=temp->link;
disp();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
node *temp;
temp=p;
int position=0;
while(temp!=NULL)
if(temp->data==value)
return position+1;
else
temp=temp->link;
position=position+1;
return 0;
void main()
list l;
int ch,v,p,ps;
do
clrscr();
cout<<"\n\nOperations
on List..";
cout<<"\n\n1.Insertion\n2.Deletion\n3.Display\n4.Seek\n5.Exit";
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cin>>ch;
switch(ch)
case 1:
clrscr();
cout<<"INSERTION";
cout<<"\n\nEnter ur choice:";
cin>>ps;
cin>>v;
switch(ps)
case 1:
l.insbeg(v);
break;
case 2:
l.inslast(v);
break;
case 3:
cin>>p;
l.insnext(v,p);
break;
default:
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"\nThe choice is
invalid";
return;
break;
case 2:
clrscr();
the list";
cout<<"\n\nEnter ur choice:";
cin>>ps;
switch(ps)
case 1:
l.delbeg();
l.disp();
break;
case 2:
l.dellast();
l.disp();
break;
case 3:
l.disp();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cin>>v;
l.delelement(v);
l.disp();
break;
default:
cout<<"\nThe option is
invalid...";
break;
break;
case 3:
clrscr();
l.disp();
break;
case 4:
clrscr();
l.disp();
cin>>v;
cout<<"\nThe position
getch();
break;
case 5:
exit(1);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
default:
cout<<"\nThe option is
invalid...";
return;
getch();
}while(ch!=5);
getch();
return;
Output:
Singly Linked
List
1.Create
2.Insert
3.Delete
4.Exit
10
1.Create
2.Insert
3.Delete
4.Exit
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
30
10
1.Create
2.Insert
3.Delete
4.Exit
List Is Empty
Result: Thus, the linked list implementation of list ADT for singly linked list program has been
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM:
Algorithm:
Step1: Create nodes first,last,next,prev and cur then set the value as
NULL.
4. Assign first=last=cur.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
2. Check list is Empty .If it is true display the message List empty.
3. If position is first.
4. If position is last.
Program
#include<iostream.h>
#include<conio.h>
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
#include<process.h>
#define max 5
struct node
int data;
int next;
};
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;
cin>>n;
//cout<<n;
if(n>=max)
else
{ initial();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
if(avail==-1)
exit(0);
list = avail;
else avail=n+1;
for(i=0;i<n;i++)
cin>>item;
cout<<"\t";
//cout<<item;
curs[i+1].data=item;
curs[n].next=-1;
void display()
int i;
cout<<"\n\nAvail = "<<avail<<"
\t ";
cout<<"List = "<<list<<"\n";
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"_______________";
cout<<"_______________";
i=0;
while(i<max)
cout<<"\n"<<curs[i].data<<"
\t "<<curs[i].next;
cout<<"\n_______________";
i++;
void insbeg()
{ int item,i;
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()
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
int item,i;
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;
cin>>item;
//cout<<item;
cout<<endl;
cin>>pos;
//cout<<pos;
cout<<endl;
i=list;
count=1;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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()
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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;
cin>>pos;
//cout<<pos;
cout<<endl;
i=list;
count=1;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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";
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<"\n8. Display";
cout<<"\n9. Exit";
cin>>ch;
//cout<<"\n"<<ch;
switch(ch)
{ case 1:
create();
break;
case 2:
else insbeg();
break;
case 3:
else insend();
break;
case 4:
else insint();
break;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
case 5:
to delete";
else delbeg();
break;
case 6:
to delete";
else delend();
break;
case 7:
to delete";
else delint();
break;
case 8:
display();
break;
case 9:
cout<<"\nEnd of
the operation";
break;
default:
}while(ch!=9);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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
LIST
1. Create
2. Insert at begin
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Cursor space:
DATA NEXT
01
22
33
44
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
9. Exit
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
Cursor space:
Avail = 4 List = 0
DATA NEXT
01
22
33
4 -1
0 -1
LIST
1. Create
2. Insert at begin
3. Insert at end
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
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
Cursor space:
DATA NEXT
04
22
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
33
4 -1
61
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
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
Cursor space:
Avail = 1 List = 0
DATA NEXT
04
0 -1
33
4 -1
62
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
LIST
1. Create
2. Insert at begin
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
3. Insert at end
4. Insert at intermediate
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
Cursor space:
DATA NEXT
04
9 -1
33
41
62
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
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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
Cursor space:
Avail = 4 List = 0
DATA NEXT
02
9 -1
33
41
0 -1
LIST
1. Create
2. Insert at begin
3. Insert at end
4. Insert at intermediate
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
5. Delete at begin
6. Delete at end
7. Delete at intermediate
8. Display
9. Exit
21
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
Cursor space:
DATA NEXT
02
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
9 -1
34
41
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
Result :Thus, the cursor implementation of list ADT for singly linked list program has been
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
Algorithm:
Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{ int stk[5];
int top;
public:
stack()
top=-1;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
void push(int x)
if(top > 4)
return;
stk[++top]=x;
void pop()
if(top <0)
return;
void display()
if(top<0)
return;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
for(int i=top;i>=0;i--)
};
main()
Clrscr();
int ch;
stack st;
while(1)
switch(ch)
st.push(ch);
break;
case 3: st.display();break;
case 4: exit(0);
} return (0);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
OUTPUT
Enter ur choice2
Enter ur choice 1
inserted 2
Inserted 3
Enter ur choice2
Deleted 3
Enter ur choice1
inserted 5
Enter ur choice3
52
Enter ur choice4
Result: Thus, the stack ADT- array implementation program has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE :
AIM: To Write a C++ Program to Stack ADT implementation using linked list
Alogarithm :
ii) Read the stack element and store it in top's data area.
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).
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
public:
int data;
};
node *head;
int tos;
public:
stack()
tos=-1;
void push(int x)
if (tos < 0 )
head =new
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
node;
head->next=NULL;
head->data=x;
tos ++;
else
{ node *temp,*temp1;
temp=head;
if(tos >= 4)
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)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
return; }
while(temp != NULL)
temp=temp->next;
void pop()
node *temp;
temp=head;
return;
tos--;
while(temp->next->next!=NULL)
temp=temp->next;
temp->next=NULL;
};
main()
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
Clrscr();
stack s1;
int ch;
while(1)
switch(ch)
s1.push(ch);
break;
case 2: s1.pop();break;
case 3: s1.display();
break;
case 4: exit(0);
return (0);
Output
enter ru choice:1
enter a element23
enter ru choice:1
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
enter a element67
enter ru choice:3
23 67
enter ru choice:2
enter ru choice:3
23
enter ru choice:2
enter ru choice:2
enter ru choice:4
Result: Thus, the stack ADT- linked list implementation program has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
Algorithm
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. Move the elements to one step forward (i.e. move to previous index ).
4. Display queue
5. Go to step2.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue {
int queue1[5];
int rear,front;
public:
queue()
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
rear=-1;
front=-1;
void insert(int x)
if(rear > 4)
front=rear=-1;
return; }
queue1[++rear]=x;
void delet()
if(front==rear)
return;
void display()
if(rear==front) {
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
return;
for(int i=front+1;i<=rear;i++)
};
main()
{ Clrscr();
int ch;
queue qu;
while(1)
switch(ch)
qu.insert(ch);
break;
case 3: qu.display();break;
case 4: exit(0);
return (0);
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
OUTPUT
Enter ur choice1
inserted21
Enter ur choice1
inserted22
Enter ur choice1
inserted16
Enter ur choice3
21 22 16
Enter ur choice2
deleted21
Enter ur choice3
22 16
Enter ur choice
Result: Thus, the queue ADT- array implementation program has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
Algorithm
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.
7. Move the elements to one step forward (i.e. move to previous index ).
9. Display queue
10.
Go to step2.
Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
public:
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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)
return;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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)
return;
while(temp !=
NULL)
temp=temp->next;
void pop()
node *temp;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
temp=head;
return;
if(front == rare)
head=NULL;
return;
front++;
head=head->next;
};
main()
Clrscr();
queue s1;
int ch;
while(1)
switch(ch)
case 1:
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
s1.push(ch); break;
case 2: s1.pop();break;
case 3: s1.display();break;
case 4: exit(0);
}}
return (0);
OUTPUT
enter ru choice:1
enter a element23
enter ru choice:1
enter a element54
enter ru choice:3
23 54
enter ru choice:2
enter ru choice:2
enter ru choice:2
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
enter ru choice:4
Result Thus, the queue ADT- linked list implementation program has been written and executed
successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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 TreeTest and select BinaryTree instance in the object bench as the
parameter in the constructor.
• Inspect the BinaryTree. Its attributes are a left subtree, a right subtree
Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
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();
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
switch(ch)
case 1:
element to insert";
insert(1,ch);
break;
case 2:
cin >>x;
y=search(1);
if(y!=-1) delte(y);
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout <<"\n";
break;
case 4:
element to search:";
cin >> x;
y=search(1);
break;
case 5:
exit(0);
int x;
if(t==1)
tree[t++]=ch;
return;
x=search1(s,ch);
if(tree[x]>ch)
tree[2*x]=ch;
else
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
tree[2*x+1]=ch;
t++;
void delte(int x)
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)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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)
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
return ;
if(t==1)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
return -1;
if(tree[s]==-1)
return s/2;
if(tree[s] >
ch)
search1(2*s,ch);
else search1(2*s+1,ch);
OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
no element in tree:
0123456789011121314151617181920212223242526272829303132
1. INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
10 is in 1 position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Result: Thus, the t program to perform Insert, Delete, Search an element into a binary tree has
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
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 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.
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)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
{ if(size>=9)
{ cout<<”array overflow “;
exit(0);
minheap[++size]=n;
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];
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
int k=1;
int newk;
if(2*k==size) //if
{ newk=2*k;
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;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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];
size=0;
elements\n”;
double number;
for(int i=1;i<=n;i++)
cin>>number;
insert_minheap(number);
void sorting::output()
::\n”;
for(int i=1;i<=n;i++)
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
cout<<delete_one_minheap()<<’\t’;
cout<<endl;
int main()
clrscr();
sorting obj;
obj.input();
obj.output();
getch();
Output
Enter how many numbers you are going to enter for sorting :5
12689
Result: Thus, the heap sort program has been written and executed successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
DATE:
AIM:
Algorithm
• 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
• 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];
j);
};
if(first<last)
{ pivot=a[first];
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
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);
j)
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
void main()
QuiSort obj;
WWW.VIDYARTHIPLUS.COM PPARVATHI
WWW.VIDYARTHIPLUS.COM
clrscr();
cout<<"\n\nQUICK SORT";
cin>>obj.n;
clrscr();
for(int i=0;i<obj.n;i++)
cin>>obj.a[i];
obj.quick(obj.a,0,obj.n-1);
\n\n";
for(i=0;i<obj.n;i++)
cout<<obj.a[i]<<" ";
getch();
OUTPUT:
12345
Result :Thus, the quick sort program has been written and executed successfully.
WWW.VIDYARTHIPLUS.COM PPARVATHI