OOPS and Data Structures Lab Guide
OOPS and Data Structures Lab Guide
(Autonomous)
Approved by AICTE, New Delhi & Affiliated to Anna University,
Chennai Accredited by NBA, NAAC with “A+” and Recognised
by UGC (2f &12B)
KOMARAPALAYAM – 637303
PREPARED BY
[Link] AP/BME
[Link] AP/BME
PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)
attitude in the students for enabling them to relate engineering issues with social
issues in a broader context.
4. To provide students managerial and leadership skills so as to make them successfully
3. Build a C++ program with member function and friend function in C++.
4. Write a C++ program to use pointer for both base and derived classes and call
the member function
5. Write C++ program to demonstrate operator overloading.
9. Develop a C++ application to perform insertion sort, quick sort and merge soft.
10. Write a C++ programs to implement recursive and non recursive i) Linear
search ii) Binary search
[Link]
Develop simple program in C++ Using Control Structure
Aim:
Algorithm:
}
cout<<”The biggest number is”<<a;
else
cout<<”The biggest number is”<<b;
7
return 0;
}
Output:
number is 30
Result:
Thus the program in C++ using Control statements was verified and output
was verified successfully.
8
[Link]
Implement Constructors and Destructors in C++
Aim:
To write a program to generate Constructors and Destructors in C++
Algorithm:
Start by defining your class with private data members and public member functions.
Default Constructor
Implement the destructor to clean up resources used by the object. Here, it doesn’t need to
do much since std::vector handles its own memory management.
Write a main() function to create instances of Algorithm and test the constructors and
destructor.
Program
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
9
// Default Constructor
Person() {
name = "Unknown";
age = 0;
std::cout << "Default Constructor called" << std::endl;
}
// Parameterized Constructor
Person(std::string n, int a) {
name = n;
age = a;
std::cout << "Parameterized Constructor called with Name: " << name << ", Age: " << age
<< std::endl;
}
// Copy Constructor
Person(const Person& p) {
name = [Link];
age = [Link];
std::cout << "Copy Constructor called to create a copy of " << [Link] << std::endl;
}
// Destructor
~Person() {
std::cout << "Destructor called for " << name << std::endl;
}
void display() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
int main() {
std::cout << "Creating objects:" << std::endl;
std::cout << "-----------------" << std::endl;
std::cout << "Exiting scope, destructors will be called automatically:" << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
return 0;
}
11
Output
Copy Constructor called to create a copy of Alice Name: Alice, Age: 30 Exiting scope,
Result
Thus the program to generate Constructors and Destructors in C++ was
executed and output verified successfully.
12
[Link]
Build a C++ program with member function and friend function in
C++.
Aim:
To write a C++ program for implementing the member function friend function.
Algorithm:
1. Start the program
2. Define a class box.
3. Define the friend function with different arguments with different data types.
4. Define the object for the class Box in main function.
5. Call the friend function.
6. Print the output.
7. Stop the program.
Program:
#include<iostream.h>
#include<conio.
h> class Box
{
private:
int
length;
public:
Box(): length(0) { }
friend int printLength(Box); //friend function
};
int printLength(Box b)
{
[Link] +=
10; return
[Link];
13
}
int main()
{
Box b;
cout<<"Length of box: "<<
printLength(b)<<endl; return 0;
}
14
Output:
Length of box: 10
Result:
Thus the C++ program for implementing the member function and friend function
was executed and output verified successfully.
15
[Link] Write C++ program to demonstrate operator overloading.
Aim:
To write a C++ program for implementing the function overloading.
Algorithm:
1. Define a class test.
2. Define the function sum with different arguments with different data types.
3. Define the object for the class test in main function.
4. Call the function using the argument type to perform different operations.
5. Print the output.
Program:
#include<iostream.
h>
#include<conio.h
> class test
{
public:
int sum(int,int);
float sum(float,float);
double
sum(double,double); }; int
test::sum(int a, int b)
{
return(a+b);
}
float test::sum(float a ,float b)
{
return(a+b);
}
double test::sum(double a, double b)
{
return(a+b);
16
}
void main()
{
test obj;
int
choice,ans;
int a,b;
float x,y;
double m,n; double result=0;
clrscr();
cout<<"\n\t\t main menu";
cout<<"\n\t1. Addition of two integer
numbers"; cout<<"\n\t2. Addition of two
float numbers"; cout<<"\n\t3. Addition of
two double numbers"<<endl; do
{
cout<<"\n enter your
choice:"; cin>>choice;
switch(choice)
{
case 1: cout<<"\n enter 2
numbers"; cin>>a>>b;
result=[Link](a,b
); break;
case 2:
cout<<"\n enter 2
number"; cin>>x>>y;
result=[Link](x,y);
break;
case 3:
cout<<"\n enter 2
number";
cin>>m>>n;
result=[Link](m,n);
break;
default:
cout<<"wrong choice";
17
break;
}
cout<<"\n\n
result"<<result<<endl;
cout<<"\ndo you want to
continue:"; ans=getch();
}
while(ans=='y'||ans=='Y');
getch();
}
18
Output:
Main menu
Addition of two integer
numbers Addition of two
float numbers Addition of
two double numbers enter
your choice 1
enter 2 number 2 3
result 5
do you want to
continue: y enter your
choice 2
enter 2 number 1.13 5.6
result 6.73
do you want to
continue: y enter your
choice 3
enter 2 number 345 354
result 699
do you want to continue: n
Result:
Thus the C++ program for function Overloading was created, executed and output
was verified successfully.
19
[Link]
Write a C++ program to use pointer for both base and
derived classes and call the member function
Aim:
To write a program in C++ to use pointer for both base and derived classes and
call the member function
Algorithm:
In main(), create objects using pointers to both Shape and Rectangle, and call the calculateArea()
function.
Program:
include <iostream> //
Base class class Shape
{
public: // Virtual function to calculate area virtual
void calculateArea()
{
std::cout << "Calculating area of Shape" << std::endl; }
};
20
}:
int main()
{
// Pointer to base class Shape* shapePtr;
// Pointer to derived class Rectangle rect(5, 10);
shapePtr = ▭ // Call calculate Area() using base class pointer shapePtr->calculateArea();
return 0;
}
21
Output:
Result:
Thus the C++ program for implementation of classes and objects was created,
executed and output was verified successfully.
22
[Link]
Make use of Inheritance in C++ application
Aim:
To write a C++ program for implementing the inheritance.
Algorithm:
Program:
#include<iostream.
h>
#include<conio.h
> class base
{
public:
int x;
void set_x(int )
{
x=n;
}void show_x()
{
23
cout<<”\n\t Base
class….”;
cout<<”\n\tx=”<<x;
}};
Output:
enter the value of
x 10 enter the
value of y 20
base
class….
x=10
derived
class…..
x=10
y=20
25
Result:
Thus the C++ program for inheritance was created, executed and output was
verified successfully.
26
[Link]
Build a C++ program to perform list operations
using linked list.
Aim:
To write a C++ program to implement a linked list and do all operations on it.
Algorithm:
4. If choice is INSERT
then Enter the element to
be inserted.
i. Get a new node and set DATA[NEWNODE] = ITEM.
ii. Find the node after which the new node is to be inserted.
iii. Adjust the link fields.
iv. Print the linked list after
insertion.
29
char
ans='y';
flag=TRU
E; do
{
cout<<"\n\nEnter the data:";
cin>>val;
New=new node;
if(New==NULL)
cout<<"unable to allocate
memory\n"; New->data=val;
New->next=NULL;
if(flag==TRUE)//executed only for the first time
{
head=New;
temp=head;
flag=FALSE;
}else
{
temp->next=New;
temp=New;
}
cout<<"\n do you want to enter more
elements?(y/n)"; ans=getch();
}
while(ans=='y'||ans=='Y');
cout<<"\n the singly linked list is created\n"; getch();
}
void sll::display()
{
node *temp;
temp=head;
if(temp==NULL)
{
cout<<"\n the list is
empty\n"; getch();
clrscr();
30
return;
}while(temp!=NULL)
{
cout<<temp-
>data<<" ";
temp=temp->next;
}getch();
}
void sll::search(int key)
{
node *temp; int
found; temp=head;
if(temp==NULL)
{
cout<<"linked list is
empty\n"; getch();
clrscr();
}
found=FALSE;
while(temp!=NULL&&found==FA
LSE)
{
if(temp-
>data!=key)
temp=temp-
>next; else
found=TRUE;
}if(found==TRUE)
{
cout<<"\n the element is present in the
list\n"; getch();
}else
{
cout<<"\nThe element is not present in the
list\n"; getch();
}}void sll::dele()
{
31
node
*temp,*prev; int
key;
temp=head;
cout<<"\n Enter the data of the node you want to
delete: "; cin>>key;
while(temp!=NULL)
{
if (temp->data==key)//traverse till required node to delete
break; //is found prev=temp;
temp=temp->next;
}
if(temp==NULL)
cout<<"\n node not
found"; else
{
if(temp==head)//first node
head=temp-
>next; else
prev->next=temp->next; //intermediate or end
node delete temp;
cout<<"\n the element is deleted\n";
}
getch();
}void sll::insert_last()
{
node *New,*temp;
cout<<"\n Enter the element which you want
to insert: "; cin>>New->data;
if(head==NULL)
head=New;
else
{
temp=head;
while(temp-
>next!=NULL)
temp=temp->next;
32
temp-
>next=New;
New-
>next=NULL;
}}void sll::insert_after()
{
int key;
node *temp,*New;
New=new node;
cout<<"\n Enter the element which you want
to insert: "; cin>>New->data;
if(head==NULL)
{
head=New;
}else
{
cout<<"\n Enter the element after which you want to insert the
node: "; cin>>key;
temp=hea
d; do
{
if(temp->data==key)
{
New->next=temp-
>next; temp-
>next=New; break;
}
else temp=temp->next;
}
while(temp!=NULL);
}}void sll::insert_head()
{
node *New,*temp;
New=new node;
cout<<"\n Enter the element which you want
to insert: "; cin>>New->data;
33
if(head==NULL)
head=New;
else
{
temp=head;
New->next=temp;
head=New;
}}void main()
{
sll s;
int choice,val,
ch1; char
ans='y';
clrscr();
cout<<"\n\n\tProgram to perform various operations on linked
list"; cout<<"\[Link]";
cout<<"\[Link]
y";
cout<<"\[Link]
h";
cout<<"\[Link] an element in
a list"; cout<<"\[Link] an
element from list";
cout<<"\[Link]";
do
{
cout<<"\n\nEnter your
choice(1-6): "; cin>>choice;
switch(choice)
{
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
34
cout<<"enter the element you want to
search"; cin>>val;
[Link](val);
break;
case 4:
clrscr();
cout<<"\n the list
is:\n"; [Link]();
cout<<"\n menu";
cout<<"\[Link] at
beginning ";
cout<<"\[Link] after";
cout<<"\[Link] at end";
cout<<"\n enter your
choice"; cin>>ch1;
switch(ch1)
{
case 1:
s.insert_head(
); break;
case 2:
s.insert_after();
break;
case 3:
s.insert_last();
break;
default:
cout<<"\n invalid
choice"; break;
}
case 5:
[Link]();
break;
case 6:
exit(0);
default:
cout<<"\n invalid choice";
35
}
cout<<"\nDo you want to
continue? "; cin>>ans;
}
while(ans=='y'||ans=='
Y'); return;
}
36
Output:
Program to perform various operations on linked list
1. Create
2. Display
3. Search
4. Insert an element in a list
5. Delete an element from list
6. Quit
menu
1. insert at
beginning
[Link] after
[Link] at end
enter your choice1
37
Enter the element which you want to
insert: 4 Do you want to continue? y
Enter your choice(1-6): 5
Enter the data of the node you want to
delete: 2 the element is deleted
Do you want to
continue? Y Enter
your choice(1-6): 5
Enter the data of the node you want to
delete: 12 node not found
Do you want to continue? y Enter your choice(1-6): 6
Result:
Thus the C++ program for linked list implementation of list ADT was created,
executed and output W was verified successfully
38
[Link]
Construct a C++ program to perform stack operation
using array
Aim:
To write a C++ program for stack using array implementation.
Algorithm:
39
Program:
#include<iostream.
h>
#include<conio.h
>
#include<stdlib.h
> class stack
{
int
stk[5];
int top;
40
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
41
empty"; return;
}
for(int
i=top;i>=0;i--)
cout <<stk[i] <<"
";
}
};
main()
{
int ch;
stack
st;
clrscr()
;
cout <<"\[Link] \[Link] \[Link] \[Link]"; while(1)
{
cout<<"\nEnter ur
choice"; cin >> ch;
switch(ch)
{
case 1: cout <<"enter the
element"; cin >> ch;
[Link](ch);
break;
case 2:
[Link]();
break;
42
case 3:
[Link]();
break;
case 4: exit(0);
}
}
return (0);
}
43
Output:
1. push
[Link]
[Link]
ay
[Link]
Enter ur
choice: 2
Deleted: 7
Enter ur choice:
3 5 3 Enter ur
choice:4
Result:
Thus the C++ program for array implementation of stack ADT was created,
executed and output was verified successfully.
44
[Link] Develop a C++ application to perform insertion sort, quick sort and
merge sort.
Aim:
To write a c++ program to perform quick sort.
Algorithm:
1. Get the value of how many no. to
be sorted. [Link] the number of
elements from the user.
3. Merge sort algorithm works The list to be sorted is divided into two arrays
of equal length by dividing the list on the middle element. If the number of
elements in the list is either 0 or 1, then the list is considered sorted.
4. Each sublist is sorted individually by using merge sort recursively.
5. The sorted sublists are then combined or merged together to form a complete
sorted list.
6. Display the sorted value.
Program:
#include
<iostream.h>
#include<conio.h
>
void merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
{
int mid;
if (low < high)
{
//divide the array at mid and sort independently using
45
merge sort mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
//merge or conquer sorted
arrays
merge(arr,low,high,mid);
}
}
// Merge sort
void merge(int *arr, int low, int high, int mid)
{
int i, j, k,
c[50]; i =
low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (arr[i] < arr[j])
{
c[k] =
arr[i];
k++;
i++;
}
else {
c[k] =
arr[j];
k++;
46
j++;
}
}
while (i <=
mid) { c[k] =
arr[i]; k++;
i++;
}
while (j <=
high) { c[k] =
arr[j]; k++;
j++;
}
for (i = low; i < k;
i++) { arr[i] = c[i];
}
}
// read input array and call
mergesort int main()
{
int myarray[30], num;
cout<<"Enter number of elements to be
sorted:"; cin>>num;
cout<<"Enter "<<num<<" elements to be
sorted:"; for (int i = 0; i < num; i++) {
cin>>myarray[i];
}
merge_sort(myarray, 0, num-
47
1); cout<<"Sorted array\n";
for (int i = 0; i < num; i++)
{
cout<<myarray[i]<<"\t";
}
}
Output:
89 76 Sorted array
2 10 12 34 43 54 64 76 89 101
Result:
Thus the C++ program for merge sort was created, executed and output was
verified successfully
48
[Link]
Write a C++ programs to implement recursive and non
recursive
i) Linear search ii) Binary search
Aim:
To write a c++ program to perform quick sort.
Algorithm:
1. Read the search element from the user
2. Find the middle element in the sorted list
3. Compare, the search element with the middle element in the sorted list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then check whether the search element is smaller or
larger than middle element.
6. If the search element is smaller than middle element, then repeat steps 2, 3, 4
and 5 for the left sublist of the middle element.
7. If the search element is larger than middle element, then repeat steps 2, 3, 4
and 5 for the right sublist of the middle element.
8. Repeat the same process until we find the search element in the list or until sublist
contains only one element.
9. If that element also doesn't match with the search element, then display " No
Match Found " and terminate the function.
Program:
#include <
iostream.h>
#include <
conio.h>
int binarySearch(int arr[], int left, int right,
49
int x) { while (left <= right) {
left) / 2; if (arr[mid] == x)
return mid;
} else if (arr[mid] <
x) { left = mid + 1;
}
else {
right = mid - 1;
return -1;
int main()
{
int
myarr[10];
int num;
int output;
50
order" << endl; for (int i = 0; i < 10; i++) {
output = binarySearch(myarr, 0,
cout << "Match found at position: " << output << endl;
return 0;
51
Output:
Result:
Thus the C++ program for binary search was created, executed and output
was verified successfully
52