Data Structure Practical
Data Structure Practical
1). Write a program to accept the elements in 2D array and perform all the matrix operations i.e.
addition, multiplication, transpose etc.
A] #include<iostream>
using namespace std;
#define s 20
class matrix
{
int a[s][s],x,y,i,j;
//static int n;
public:
void get();
void put();
matrix operator+(matrix);
matrix operator-(matrix);
matrix operator*(matrix);
matrix transpose();
};
void matrix::get()
{
cout<<"Enter the order of Matrix "<<" :\n";
cin>>x>>y;
cout<<"Enter the Matrix "<<" :\n";
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
cin>>a[i][j];
}
void matrix::put()
{
cout<<"The Ans is:\n";
for(int i=0;i<x;i++)
{
cout<<"\n\t";
for(int j=0;j<y;j++)
cout<<a[i][j]<<" ";
}
}
matrix matrix::operator+(matrix b)
{
matrix r;
if((x!=b.x)||(y!=b.y))
{
cout<<"\n\tMatrix Addition is not possible the result is incorrect\n\n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=y;
}
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
r.a[i][j]=a[i][j]+b.a[i][j];
return r;
}
matrix matrix::operator-(matrix b)
{
matrix r;
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
if((x!=b.x)||(y!=b.y))
{
cout<<"\n\tMatrix subtraction is not possible the result is incorrect\n\n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=y;
}
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
r.a[i][j]=a[i][j]-b.a[i][j];
return r;
}
matrix matrix::operator*(matrix b)
{
matrix r;
if((x!=b.y)||(y!=b.x))
{
cout<<"\n\tMatrix Multiplication is not possible the result is incorrect\n\n";
r.x=0;
r.y=0;
}
else
{
r.x=x;
r.y=b.y;
}
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
r.a[i][j]=0;
for(i=0;i<x;i++)
for(j=0;j<b.y;j++)
for(int k=0;(k<y)||(k<b.x);k++)
r.a[i][j]+=a[i][k]*b.a[k][j];
return r;
}
matrix matrix::transpose()
{
matrix r;
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
r.a[i][j]=a[j][i];
r.x=x;
r.y=y;
return r;
}
//..........................Main function..................................
int main()
{
matrix a,b,c;
int t=1,
while(t)
{
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
cout<<"\tSelect Option\n\n1.Matrix Addition\n2.Matrix Subtration\n3.Matrix Multiplication\n4.Matrix
Transponse\n5.Exit\n";
switch()
{
case'1':
cout<<"\n\tMatrix Addiation\n";
a.get();
b.get();
c=a+b;
c.put();
break;
case'2':
cout<<"\n\tMatrix Subtration\n";
a.get();
b.get();
c=a-b;
c.put();
break;
case'3':
cout<<"\n\tMatrix Multpication\n";
a.get();
b.get();
c=a*b;
c.put();
break;
case'4':
cout<<"\n\tMatrix Transpose\n";
a.get();
c=a.transpose();
c.put();
break;
case'5':
cout<<"\n\tPress any key to exit\n";
t=0;
break;
default:
cout<<"\n\tEnter a valid option\n";
}
return 0;
}
return 0;
}
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
2). Explain following techniques Bubble sort Insertion sort Radix sort
A]
1). Bubble sort,
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps
through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named
for the way smaller or larger elements "bubble" to the top of the list.
Step-by-step example
Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using
bubble sort. In each step, elements written in bold are being compared. Three passes will be required;
First Pass
( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not
swap them.
Second Pass
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458)
(12458)→(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm
needs one additional whole pass without any swap to know it is sorted.
Third Pass
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
#include<bits/stdc++.h>
using namespace std;
// Returns number of times x occurs in arr[0..n-1]
int countOccurrences(int arr[], int n, int x)
{ int res = 0;
for (int i=0; i<n; i++)
if (x == arr[i])
res++;
return res;
}
// Driver code
int main()
{ int arr[] = {1, 2, 2, 2, 2, 3, 4, 7 ,8 ,8 };
int n = sizeof(arr)/sizeof(arr[0]);
int x = 2;
cout << countOccurrences(arr, n, x);
return 0;
}
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2
Output: 4 // x (or 2) occurs 4 times in arr[]
ii. The position of first occurrence of x in the array.
#include <bits/stdc++.h>
using namespace std;
// Function for finding first occurrence
// of an elements
void findFirst(int arr[], int n, int x)
{ int first = -1;
for (int i = 0; i < n; i++) {
if (x != arr[i])
continue;
if (first == -1)
first = i; }
if (first != -1)
cout << "First Occurrence = " << first;
else
cout << "Not Found";
}
// Driver code
int main()
{ int arr[] = { 1, 2, 2, 2, 2, 3, 4, 7, 8, 8 };
int n = sizeof(arr) / sizeof(int);
int x = 8;
findFirst(arr, n, x);
return 0; }
Input : arr[] = {1, 3, 5, 5, 5, 5, 67, 123, 125}
x=5
Output : First Occurrence = 2
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
4). Write a program in C++ to delete particular element from an array of 10 integers.
A] #include<iostream>
using namespace std;
int main()
{
int arr[10], tot=10, i, elem, j, found=0;
cout<<"Enter 10 Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nEnter Element to Delete: ";
cin>>elem;
for(i=0; i<tot; i++)
{
if(arr[i]==elem)
{
for(j=i; j<(tot-1); j++)
arr[j] = arr[j+1];
found++;
i--;
tot--;
}
}
if(found==0)
cout<<"\nElement doesn't found in the Array!";
else
cout<<"\nElement Deleted Successfully!";
cout<<endl;
return 0;
}
Output:
Enter Element to Delete: 1,2,3,4,5,6,7,8,9,10
Enter Element to Delete:8
Element Deleted Successfully!"
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
5). Consider two single dimensional array of size 20 and 3 respectively. Write a program in C++ to
display all the elements which are common in both arrays.
A] #include <bits/stdc++.h>
using namespace std;
// x < y
else if {
(ar1[i] < ar2[j])
i++;
}
}
// Driver code
int main()
{
int ar1[] = { 1, 5, 10, 20, 40, 80,50,88,87,55 };
int ar2[] = { 6, 7, 55 };
int n1 = sizeof(ar1) / sizeof(ar1[0]);
int n2 = sizeof(ar2) / sizeof(ar2[0]);
Input:
ar1[] = { 1, 5, 10, 20, 40, 80,50,88,87,55 }
ar2[] = { 6, 5, 55 }
Output: 5, 55
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
6). Write a program to build a sparse matrix as an array. Write functions to check if the sparse
matrix is a square, diagonal, lower triangular, upper triangular or tridiagonal matrix.
A]
// C++ program to print Lower
// triangular and Upper triangular
// matrix of an array
#include<iostream>
// Function to form
// lower triangular matrix
void lower(int matrix[3][3], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i < j)
{
cout << "0" << " ";
}
else
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
{7, 8, 9}};
int row = 3, col = 3;
return 0;
}
Input : matrix[3][3] = {1 2 3
456
7 8 9}
Output :
Lower : 1 0 0 Upper : 1 2 3
450 056
789 009
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
7). Write a menu driven program for stack contain following function PUSH POP DISPLAY
PEE
A]
/* C++ program to implement basic stack operations */
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
return 0;
}
Output:
10 pushed into stack
20 pushed into stack
30 pushed into stack
30 Popped from stack
Elements preset in stack : 20 10
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
8). Transform the following infix expressions into their equivalent prefix expressions:
(A-B) *(D/ E)
(A+B^D) / (E-F) + G
A* (B+D) / E -F* (G + H/ K
A]
Output for "(A-B) *(D/ E)" expression
Step 1. Reversed string: (E /D)* (B-A)
Step 2. Postfix of (E /D)* (B-A): E D/ BA-*
Step 3. Reversed string of E D/ BA-* is *-AB /D E
Output:
1) Insert element to queue
2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
10). Write menu driven program which create and display the circular linked list.
A]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Represents the node of list.
struct node{
int data;
struct node *next;
};
//This function will add the new node at the end of the list.
void add(int data){
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
if(head == NULL){
//If list is empty, both head and tail would point to new node.
head = newNode;
tail = newNode;
newNode->next = head;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
//Since, it is circular linked list tail will point to head.
tail->next = head;
}
}
int main()
{
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
//Adds data to the list
add(1);
add(2);
add(3);
add(4);
//Displays all the nodes present in the list
display();
return 0;
}
Output:
Nodes of the circular linked list:
1234
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
11). Create binary search tree 15, 2, 25, 45, 35, 23, 100, 5
15
2 25
5 35 45
23 100
35
12). Given two binary trees, write a program that finds whether –
The two binary trees are similar. –
the two binary trees are mirror images of each other
// C++ program to check if two trees are mirror
// of each other
#include<bits/stdc++.h>
using namespace std;
return 0;
}
Output :
Yes
ii. the two binary trees are mirror images of each other
#include <iostream>
// Define node structure
struct node{
int data;
struct node* left,*right;
// Constructor
node(int x){
data = x;
left = right = NULL; }
};
bool mirror_image_check(struct node* root1,struct node* root2){
if((root1==NULL) and (root2==NULL))return true; // If both trees are empty, then they are mirror image
if((!root1 and root2) or (root1 and !root2))return false; // If either of the tree is empty, when other is not, they
cannot be mirror to each other
bool left_check = mirror_image_check(root1->left,root2->right); // Check if left subtree in tree 1 is mirror to right
subtree in tree 2
bool right_check = mirror_image_check(root1->right,root2->left);// Check if right subtree in tree 1 is mirror to left
subtree in tree 2.
return (root1->data == root2->data) and left_check and right_check ; // Check if root nodes are same, and also the
subtrees condition for mirror. }
int main() {
// Construct tree 1
struct node* root1 = new node(1);
root1->left = new node(2);
root1->right = new node(3);
root1->left->left = new node(4);
root1->left->right = new node(5);
// Construct tree 2
struct node* root2 = new node(1);
root2->left = new node(3);
root2->right = new node(2);
root2->right->left = new node(5);
root2->right->right = new node(4);
if(mirror_image_check(root1,root2)){ // Function to check if two trees are mirror image to each other
std::cout<<"Yes,the trees are mirror image of each other\n";
}else{
std::cout<<"No, the trees are not mirror image of each other\n";
}
return 0; }
Output :
Yes,the trees are mirror image of each other
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
A]
Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
vector<bool> visited;
visited.resize(V,false);
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
return 0;
}
Output:
Following is Breadth First Traversal (starting from vertex 2)
2031
PRN: 2021017001608861 Yash Parmar FYBCA – SEM-II (Data Srtucture Using C++ )
14). Write a program to traverse the graph using DFS method.
A]
// C++ program to print DFS traversal from
// a given vertex in a given graph
#include <bits/stdc++.h>
using namespace std;
void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
return 0;
}
Output:
Following is Depth First Traversal
01239