DataStructure Practicals
DataStructure Practicals
DataStructure Practicals
Aim: Write a program to accept the elements in 2D array and perform all the
matrix operations i.e. addition ,multiplication ,transpose etc.
Code:
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int a[3][3],b[3][3],c[3][3],d[3][3],e[3][3],f[3][3],i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
cin>>a[i][j];
cout<<"\nno.";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
cout<<a[i][j]<<endl;
}
}
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
cin>>b[i][j];
cout<<"\n Add";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"\t"<<c[i][j];
cout<<"\n";
cout<<"\n sub";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
d[i][j]=a[i][j]-b[i][j];
cout<<"\t"<<d[i][j];
}
cout<<"\n";
cout<<"\n mul";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
e[i][j]=a[i][j]*b[i][j];
cout<<"\t"<<e[i][j];
cout<<"\n";
cout<<"\n div";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
f[i][j]=a[i][j]/b[i][j];
cout<<"\t"<<f[i][j];
cout<<"\n";
getch();
return 0;
}
Output:
Practical No.2
Bubble sort
Insertion sort
Radix sort
Bubble Sort: - Bubble Sort is an algorithm which is used to sort N elements that are given in a
memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one
and sort them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the
first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent
data items and swapping each pair that is out of order.
Ans.: k=6-1=5
Pass 1: 5 1 6 2 4 3
1 5 6 2 4 3
1 5 6 2 4 3
1 5 2 6 4 3
1 5 2 4 6 3
1 5 2 4 3 6
Pass 2: 1,5,2,4,3,6.
1 5 2 4 3 6
1 5 2 4 3 6
1 2 5 4 3 6
1 2 4 5 3 6
1 2 4 3 5 6
1 2 4 3 5 6
Pass 3 : 1,2,4,3,5,6.
1 2 4 3 5 6
1 2 4 3 5 6
1 2 4 3 5 6
1 2 3 4 5 6
Insertion Sort : Insertion sort algorithm sorts a set of values by inserting values into an existing
sorted file. Compare the second element with first ,if the first element is greater than second, place it
before the first one . Otherwise place is just after the first one. Compare the third value with second. If
the third value is greater than the second value then place it just after the second. Otherwise place the
second value to the third place. And compare third value with the first value. If the third value is greater
than the first value place the third value to the second place, otherwise place the first value to second
place. And place the third value to first place and so on.
Ans.: 5,1,6,2,4,3.
K= variable.
Pass 1:
5 1 6 2 4 3
1 5 6 2 4 3
1 5 2 6 4 3
1 2 5 6 4 3
1 2 4 5 6 3
1 2 3 4 5 6
Radix Sort : Radix sort is based on the value of the actual digits in the positional
representation of the number being sorted. In this we perform the following action on the list for
each digit, beginning with the most significant digit.
These actions are repeated for each digit , starting with the least significant digit and ending with
the most significant digit. Thus when all the number in the list are sorted on a most signification
digit , number that have the same signification position but different digits in the less significant
position are already sorted on the less signification position.
Ans.: pass 1.
Queue
[0] 20
[1]
[2] 2 22
[3]
[4] 14
[5] 5
[6] 16
[7]
[8] 8
Queue
[0] 2 5 8
[1] 14 16
[2] 20 22
[3]
[4]
[5]
[6]
[7]
[8]
Pass 2: 2,5,8,14,16,20,22,
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int a[]={1,3,5,3,7,10,11,3};
int x=3;
int first=0;
int count=0;
int o=0;
for(int i=0;i<8;i++)
if(x==a[i])
if(o==0)
{
first=i;
count++;
o++;
getch();
return 0;
Output:
Practical No.4
Aim: Write a program in c++ to delete particular element from an array of 10
integers.
Code:
#include<iostream.h>
#include<conio.h>
void main()
int i,a[10],no,pos;
clrscr();
for(i=0;i<10;i++)
cin>>a[i];
cin>>pos;
if(pos>10)
}
else
for(i=pos;i<=9;i++)
a[i]=a[i+1];
for(i=0;i<9;i++)
cout<<"\n"<<a[i];
getch();
Output:
Practical No.5
Aim: Consider two single dimensional array of size 20 and 3 respectively. Write a
program in c++ to display all the element which are common in both arrays.
Code:
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int a[]={1,3,5,3,7,10,11,3};
int x=3;
int first=0;
int count=0;
int o=0;
for(int i=0;i<8;i++)
if(x==a[i])
if(o==0)
first=i;
}
count++;
o++;
getch();
return 0;
Output:
Practical No.6
Aim: 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.
Code:
Lower Triangular:
Code:
#include <stdio.h>
#include<iostream.h>
#include<conio.h>
int main()
int A[3][3];
/*
*/
{
scanf("%d", &A[row][col]);
/*
isLower = 1;
/*
*/
isLower = 0;
}
/*
*/
if(isLower == 1)
/*
*/
if(A[row][col]!=0)
printf("\n");
}
}
else
return 0;
Output:
Upper Triangular Matrix:
Code:
#include <stdio.h>
#include<iostream.h>
#include<conio.h>
int main()
int A[3][3];
/*
*/
scanf("%d", &A[row][col]);
}
/*
*/
isUpper = 1;
/*
*/
isUpper = 0;
/*
if(A[row][col] != 0)
printf("\n");
else
}
return 0;
Output:
Diagonal Matrix:
Code:
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a[3][3],i,j;
cout<<"\n\n";
cout<<"enter elements";
cout<<"\n\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<"\n";
cin>>a[i][j];
}
diagnol(a);
getch();
int i,j;
cout<<"\n\n";
cout<<"diagnol 1";
cout<<"\n\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j)
cout<<a[i][j]<<"\t";
cout<<"\n\n";
cout<<"diagnol 2";
cout<<"\n\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i+j==3-1)
cout<<a[i][j]<<"\t";
Output:
Tridiagonal Matrix:
Code
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
int i,j,k;
clrscr();
int mat[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
for(i=0;i<5;i++)
for(j=0;j<5;j++)
printf("%d\t",mat[i][j]);
printf("\n");
}
printf("\nTridiagnal matrix:\n\n");
printf("%d\t%d\n",mat[0][0],mat[0][1]);
for(i=1;i<5;i++)
for(k=1;k<i;k++)
printf("\t");
if(i==4)
printf("%d\t%d\n",mat[4][3],mat[4][4]);
else
for(j=i;j<5;j++)
if(i==j)
printf("%d\t%d\t%d",mat[i][j-1],mat[i][j],mat[i][j+1]);
printf("\n");
}
getch();
Output:
Practical No.7
Aim: Write a menu driven program for stack contain following function
PUSH
POP
DISPLAY
PEEK
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class node
public:
int data;
node *next;
};
class stack
public:
node *top;
};
void display(stack s)
node *p;
p=s.top;
while(p!=NULL)
cout<<"\n"<<p->data;
p=p->next;
node *p;
p=new node;
p->data=n;
p->next=s->top;
s->top=p;
{
int n;
node *p;
if(s->top==NULL)
return -1;
else
p=s->top;
n=p->data;
s->top=p->next;
delete p;
return n;
void main()
stack s1;
int n,c;
s1.top=NULL;
do
clrscr();
cout<<"MENU \n";
cout<<"2. POP\n";
cout<<"3. DISPLAY\n";
cout<<"4. PEEK\n";
cin>>c;
switch (c)
case 1:
cin>>n;
push(&s1, n);
display(s1);
getch();
break;
case 2:
n=pop(&s1);
if(n!=-1)
display(s1);
getch();
break;
case 3:
display(s1);
getch();
break;
while(c!=4);
getch();
}
Output:
Practical No.8
Aim: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)
Code:
i) (A-B)*(D/E)
INFIX: (LVR)
. -C-
C
. /
C
A
.
B
.
D
. . E
PREFIX :(VLR)
. *
. -C-
C
. /
C
.A .
B D
. . E
2- (A+B^D)/(E-F)+G
INFIX: (LVR)
PREFIX: (LVR)
. +
. /
.
G
. B
. -
. +
. .
^ E F
.
. A D
3. A*(B+D)/E-F*(G+H/K
INFIX: (LVR)
PREFIX: (VLR)
.
Practical No.9
Aim: Write a program in c++ to implement queue using Array .
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class node
int data;
node *next;
public:
void display();
void insert();
void delet();
};
int t,count=0;
void node::display()
if(front==NULL)
cout<<"\n\n\t\t There are no elements in the queue \n\t\t press enter tp return to main menu:";
else
cout<<"\n --------------------------------------\n";
temp=front;
while(temp!=NULL)
cout<<"\n\t "<<temp->data;
temp=temp->next;
void main()
int ch,y=1;
node n;
clrscr();
while(1)
{
cout<<"\n\n\n\t\t MAIN MENU\n";
cout<<"\n\t**********";
cout<<"\n \t 4. Exit.\n";
cout<<"\n\t****************\n";
cin>>ch;
switch(ch)
case 1:
n.insert();
n.display();
break;
case 2:
n.display();
getch();
break;
case 3:
n.delet();
n.display();
break;
case 4:
exit(0);
break;
default:
getch();
void node::insert()
temp=new node;
cin>>temp->data;
temp->next=NULL ;
if(front==NULL)
front=temp;
else
ptr=front;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
void node::delet()
if(front==NULL)
cout<<"\n\n There are no element in link list \n press enter to return to main menu";
else
ptr=front;
front=front->next;
delete ptr;
}
Output:
Practical No.10
Aim:Consider the single Linked list contains following elements:
Rollno int ,sname char(20),city char(20),course char(3)
Write a program in c++ to represent linked list with the above elements.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
struct stud {
char name[25];
} *head, *tail;
void creat();
void display();
void addbeg();
void addend();
void addany();
void delet();
void rev();
void main()
clrscr();
creat();
display();
addbeg();
display();
addend();
display();
addany();
display();
delet();
display();
getch();
display();
getch();
void creat()
char nm[25];
scanf("%d\n", &n);
scanf("%d\n", &rl);
fflush(stdin);
gets(nm);
fflush(stdin);
if (k == 0) {
head = p;
head->roll = rl;
strcpy(head->name, nm);
head->mtot = mt;
head->next = NULL;
k = 1;
tail = head;
} else {
head->next = p;
head = head->next;
head->roll = rl;
strcpy(head->name, nm);
head->mtot = mt;
head->next = NULL;
}
}
void display()
p1 = tail;
printf("\n ----------------------------------------------");
p1 = p1->next;
void addbeg()
char nm[25];
scanf("%d\n", &rl);
fflush(stdin);
gets(nm);
scanf("%d\n", &mt);
fflush(stdin);
p->roll = rl;
strcpy(p->name, nm);
p->mtot = mt;
p->next = tail;
tail = p;
void addend()
char nm[25];
scanf("%d\n", &rl);
fflush(stdin);
gets(nm);
scanf("%d\n", &mt);
fflush(stdin);
p->roll = rl;
strcpy(p->name, nm);
p->mtot = mt;
p->next = NULL;
head->next = p;
head = p;
void addany()
char nm[25];
printf("\n Enter the position where you want to add the node::");
scanf("%d\n", &n);
fflush(stdin);
if (n <= 0) {
getch();
exit(1);
scanf("%d\n", &rl);
fflush(stdin);
gets(nm);
scanf("%d\n", &mt);
fflush(stdin);
p->roll = rl;
strcpy(p->name, nm);
p->mtot = mt;
p1 = tail;
n = n - 1;
c++;
if (c == n) {
p->next = p1->next;
p1->next = p;
k = 1;
break;
p1 = p1->next;
if (k == 0)
void delet()
char nm[25];
fflush(stdin);
p1 = tail;
// p=head1;
k++;
if (p1->roll == rl) {
if (k == 1)
tail = p1->next;
else
p->next = p1->next;
free(p1);
break;
p = p1;
p1 = p1->next;
if (k == 0)
}
void rev()
char nm[25];
p1 = tail;
p->roll = p1->roll;
strcpy(p->name, p1->name);
p->mtot = p1->mtot;
p->next = temp;
temp = p;
p1 = p1->next;
free(tail);
tail = p1;
tail = temp;
}
Output:
Practical No.11
Aim: Write menu driven program which create and display the circular linked list .
Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
public:
int data;
node *next;
};
class cirqueue
public:
node *rear;
};
ptr->data=n;
ptr->data=n;
if(q->rear==NULL)
q->rear=ptr;
ptr->next=ptr;
else
ptr->next=q->rear->next;
q->rear->next=ptr;
q->rear=ptr;
void display(cirqueue q)
node *p,*r;
r=p=q.rear->next;
if(p==NULL)
cout<<"\n Circular queue is empty ";
else
do
cout<<"\n "<<p->data;
p=p->next;
while(p!=r);
void main()
cirqueue q1;
int n,c;
q1.rear=NULL;
do
clrscr();
cin>>c;
switch(c)
case 1:
cin>>n;
addq(&q1, n);
display(q1);
getch();
break;
case 2:
display(q1);
getch();
break;
default:
exit(1);
while(c!=4) ;
Output:
Practical No.12
Aim:Create binary search tree 15,2,25,45,35,23,100,5.
Ans:
Step 1: node= 15
Step 2:
Step 3:
Practical No.13
Aim:.Given two binary tree, write a program that finds whether
-The two binary trees are similar.
-The two binary trees are mirror images of each other.
a. The two binary trees are similar
#include <stdlib.h>
#include<iostream.h>
#include<conio.h>
struct node
int data;
};
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
return 1;
return
identicaltrees(a->right, b->right)
);
return 0;
int main()
{
struct node *root1 = newnode(1);
root1->left = newnode(2);
root1->right = newnode(3);
root1->left->left = newnode(4);
root1->left->right = newnode(5);
root2->left = newnode(2);
root2->right = newnode(3);
root2->left->left = newnode(4);
root2->left->right = newnode(5);
if(identicaltrees(root1, root2))
else
getchar();
return 0;}output:
Practical No.13.2
B. The two binary trees are mirror images of each other
Code:
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#define true 1
#define false 0
struct Node
int data;
};
return true;
if (a==NULL || b == NULL)
return false;
areMirror(a->right, b->left);
node->data = data;
return(node);
void main()
clrscr();
Node *a = newNode(1);
Node *b = newNode(1);
a->left = newNode(2);
a->right = newNode(3);
a->left->left = newNode(4);
a->left->right = newNode(5);
b->left = newNode(3);
b->right = newNode(2);
b->right->left = newNode(5);
b->right->right = newNode(4);
Output:
Practical No.14
Aim: Write a program to traverse the graph using BFS method .
Code:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int adj[10][10],n;
int visited[10];
void bfs(int v)
int q[10],front=-1,rear=-1,i;
visited[v]=1;
q[++rear]=v;
while(front!=rear)
v=q[++front];
cout<<v;
for(i=0;i<n;i++)
if(!visited[i]&&adj[v][i])
{
visited[i]=1;
q[++rear]=i;
void main()
int i,j,m,a,b,v;
char c;
clrscr();
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
adj[i][j]=0;
for(i=1;i<=m;i++)
adj[a][b]=1;
adj[b][a]=1;
do{
clrscr();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cout<<setw(4)<<adj[i][j];
cout<<"\n";
cin>>v;
for(i=0;i<n;i++)
visited[i]=0;
bfs(v);
cin>>c;
}
while(c!='N');
getch();
Output:
Practical No.15
Aim: Write a program to traverse the graph using DFS method .
Code.:
#include<iostream.h>
#include<conio.h>
int i,j,a,b;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
adj[i][j]=0;
for(i=0;i<m;i++)
cin>>a>>b;
adj[a][b]=1;
adj[b][a]=1;
int visited[10]={0};
void dfs(int adj[][10],int n ,int v)
int i;
visited[v]=1;
cout<<v;
for(i=0;i<n;i++)
if(adj[v][i]&&!visited[i])
dfs(adj,n,i);
void main()
int adj[10][10];
int n,m,i,j,v;
clrscr();
cin>>n>>m;
graph(adj,n,m);
cin>>v;
dfs(adj,n,v);
getch();
Output: