0% found this document useful (0 votes)
9 views

2025 Data Structure.pdf Removed

The document outlines various algorithms and programs for data structures and algorithms, including Binary Search, Stack Operations, Queue Operations, Binary Tree Traversal, Breadth First Search, and Depth First Search. Each section includes an aim, algorithm steps, program coding in C++, and results indicating successful execution. The document serves as a comprehensive guide for implementing these fundamental concepts in programming.

Uploaded by

cocojoy32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

2025 Data Structure.pdf Removed

The document outlines various algorithms and programs for data structures and algorithms, including Binary Search, Stack Operations, Queue Operations, Binary Tree Traversal, Breadth First Search, and Depth First Search. Each section includes an aim, algorithm steps, program coding in C++, and results indicating successful execution. The document serves as a comprehensive guide for implementing these fundamental concepts in programming.

Uploaded by

cocojoy32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Table of the content

S.No Date particulars Page Remarks


.No

1 03.01.25 Binary Search 01

2 20.01.25 Stack Operation 05

3 28.01.25 Queue Operation 11

4 05.02.25 Binary Tree Traversal 16

5 14.02.25 Breadth First Search 23

6 24.02.25 Depth First Search 29

7 03.03.25 Merge Sort 33

8 05.03.25 Quick Sort 40


Ex.No:01 BINARY SEARCH
Date:03.01.25

Aim:
To search an element from the sorted using binary search.
Algorithm:
Step 1: Start the program.
Step2: Declare the necessary variables.
Step 3: Read the elements.
Step 4: Find the middle value in the sorted list.
Step 5: Compare the search element with the middle element in the
list.
Step 6: If comparison is satisfied then display the element and its
position.
Step 7: Otherwise check whether search element is smaller or larger
than the middle element.
Step 8: If smaller than repeat the above steps for the left sublists of
the middle element.
Step 9: If larger than repeat the above steps for the right sublists of
the middle element.
Step 10: Repeat the process until the search element is found,
otherwise display that the element is not in the list.
Step 11: Save the program, compile and execute it
Step12:Stop the program.

1
Program coding:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,a[10],t,first=0,last,mid,temp;
clrscr();
cout<<”\t\t\t Binary Search”;
cout<<”\n Enter the no of elements:”;
cin>>last;
cout<<”\n Enter the elements:”;
for(i=0;i<last;i++)
cin>>a[i];
cout<<”The sorted array is”;
for(i=0;i<last;i++)
{
for(j=i+1;j<last;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

2
}
}
for(i=0;i<last;i++)
cout<<”\t”<<a[i];
cout<<”\n Enter the number to be search”;
cin>>t;
mid=(first+last)/2;
while(first<=last)
{
if(a[mid]<t)
first=mid+1;
else if(a[mid]==t)
{
cout<<”\n The number”<<t<<”found at position”<<mid+1;
break;
}
else
last=mid-1;
mid=(first+last)/2;
}
if(first>last)
cout<<”\n The number”<<t<<”is not found in the given array”;
getch();
}

3
Output:

Result:
Thus the above program was executed successfully.

4
Ex.No:02 STACK OPERATION
Date:20.01.25

Aim:
To implement the push and pop operation in stack.
Algorithm:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Declare all the functions.
Step 4: Display menu with list of operations and make function calls
to perform operation selected.
Step 5: The selected operation is push.
I. check whether the stack is full.
II. If it is full then display stack is full and terminate the function
III. If it is not full then increment top value by 1 and set the value
Step 6: The selected operation is pop.
I. To check whether the stack is empty.
II. If it is empty then display stack is empty and terminate the
function
III. If it is not empty then decrement top value by 1 and delete
the value.
Step 7: The selected operation is display.
I. To check whether the stack is empty.
II. If it is empty terminate the function.
III. If it is not empty then define a variable i=0 to display the stack[i]

5
and increment one by one upto top using
for loop.
Step 8 : Save a progam,compile and execute it.
Step 9 : Stop the program.
Program coding:
#include<iostream.h>
#include <conio.h>
int a[10],ch,val,top=-1,n=10;
void push(int val)
{
if(top>=n-1)
cout<<”Stack Overflow”;
else
{
top++;
a[top]=val;
}
}
void pop()
{
if(top<=-1)
cout<<”Stack Underflow”;
else
{

6
cout<<”The popped element is”<<a[top];
top--;
}
}
void display()
{
if(top>=0)
{
cout<<”Stack element are”;
for(int i=0;i<=top;i++)
{
cout<<a[i]<<” ”;
}
}
else
cout<<”Stack is empty”;
}
void main()
{
clrscr();
cout<<”\n\t Stack Operations”;
cout<<”\n 1.Push”;
cout<<”\n 2.Pop”;
cout<<”\n 3.Display”;

7
cout<<”\n 4.Exit”;
do
{
cout<<”\n Enter your choice”;
cin>>ch;
switch(ch)
{
case 1:
{
cout<<”Enter the value”;
cin>>val;
push(val);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}

8
case 4:
{
break;
}
default:
{
cout<<”Invalid choice”;
break;
}
}
}while(ch!=4);
getch( );
}

9
Output:

Result:
Thus,the above program was executed successfully.

10
Ex.No:3 QUEUE OPERATIONS
Date:28.01.25

Aim:
To implement enqueue and dequeue operation in Queue.
Algorithm:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Declare all the functions.
Step 4: Display menu with list of operations and make function calls
to perform
operation selected.
Step 5: The selected operation is insert.
I. To check whether the queue is full.
II. If it is full, then display queue is full and terminate the function.
III. If it is not full, then increment rear value by 1 and set queue.
Step 6: The selected operation is delete.
I. To check whether the queue is Empty.
II. If it is empty then queue is empty and terminate the function.
III. If it is not empty then increment front value by 1 and delete the
value.
Step 7 : The selected operation is display.
I. To check whether the queue is empty.
II. If it is empty terminate the function.

11
III. If it is not empty then define the variable i=front, to display the
queue value and increment one by one upto rear using for loop.
Step 8: Save the program, compile and execute it.
Step 9: Stop the program.
Program coding:
#include<conio.h>
int a[10],n=10,front=-1,rear=-1;
void insert()
{
int val;
if(rear==n-1)
cout<<”Queue Overflow”<<endl;
else
{
if(front==-1)
front=0;
cout<<”Enter the element”;
cin>>val;
rear++;
a[rear]=val;
}
}
void del()

12
{
if(front==-1||front>rear)
{
cout<<”Queue Underflow”;
return;
}
else
{
cout<<”Element deleted from queue”<<a[front];
front++;
}
}
void display()
{
if(front==-1||front>rear )
cout<<”Queue is empty”<<endl;
else
{
cout<<”Queue elements are:”;
for(int i=front;i<=rear;i++)
cout<<a[i]<<” “;
cout<<endl;
}
}

13
void main()
{
int ch;
clrscr();
cout<<”\t\t\t Queue operations”;
cout<<”\n 1.Insert”;
cout<<”\n 2.Delete”;
cout<<”\n 3.Display”;
cout<<”\n 4.Exit”;
do
{
cout<<”\n Enter your choice”;
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;

14
case 4:
break;
default:
cout<<”\n Invalid choice”;
break;
}
}while(ch!=4);
getch();
}
Output:

Result:
Thus,the above program was executed successfully.

15
Ex.No:04 BINARY TREE TRAVERSAL
Date:05.02.25

Aim:
To perform Traversal operations in binary tree.
Algorithm:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Create a binary search tree by adding nodes to the tree.
Step 4: Enter the value for the root node and the other node.
Step 5: If the node value is less than the root node then assign it to
the left subtree node.
Step 6: If the node value is greater than the root node then assign it
to the right subtree node.
Step 7: The selected operation is preorder traversal,
I. Process the root node first before processing the left and right
subtree nodes.
Step 8: The selected operation is inorder traversal,
I. Process the root node after processing left subtree then
process the right subtree nodes.
Step 9: The selected operation is postorder traversal,
I. Process the root node after processing left and right subtree nodes
Step 10: Display the nodes in traversal order.

16
Program Coding:
#include<iostream.h>
#include<conio.h>
#define NULL 0
struct st
{
int data;
struct st *left;
struct st *right;
};
struct st *root=NULL, *temp;
void insert();
void preorder(struct st *);
void inorder(struct st *);
void postorder(struct st *);
void main()
{
int ch;
clrscr();
cout<<"\n\t Binary Tree Traversals";
cout<<"\n 1.Insert";
cout<<"\n 2.Preorder";
cout<<"\n 3.Inorder";
cout<<"\n 4.Postorder";

17
cout<<"\n 5.Exit";
do
{
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
break;
default:
cout<<"Invalid choice";
break;

18
}
}while(ch!=5);
}
void insert()
{
struct st *nc,*pnode;
int v;
cout<<"\n Enter the value";
cin>>v;
temp=new st;
temp->data=v;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
nc=root;
while(nc!=NULL)
{
pnode=nc;
if(v<nc->data)

19
nc=nc->left;
else
nc=nc->right;
}
if(v<pnode->data)
{
pnode->left=temp;
}
else
pnode->right=temp;
}
}
void preorder(struct st *temp)
{
if(temp!=NULL)
{
cout<<" "<<temp->data;
preorder(temp->left);
postorder(temp->right);
}
}
void inorder(struct st *temp)
{
if(temp!=NULL)

20
{
inorder(temp->left);
cout<<" "<<temp->data;
inorder(temp->right);
}
}
void postorder(struct st *temp)
{
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
cout<<" "<<temp->data;
}
}

21
Output:

Result:
Thus , the above program was executed successfully.

22
Ex.No:05 BREADTH FIRST SEARCH
Date:14.02.25

Aim :
To perform graph traversal using breadth first search.
Algorithm :
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Read the number of vertices and adjacency matrix.
Step 4 : Start traverse from 0 and visit its child nodes.
Step 5 : Store the traverse, the order in which they are visited.
Step 6 : Use a queue to store the node and mark it as visited until its
neighbours
are marked.
Step 7 : The node that was inserted first will be verified first and so
on.
Step 8 : Repeat for all the vertex.
Step 9 : Save the program, compile and execute it.
Step 10: stop the program.
Program coding:
#include<iostream.h>
#include<conio.h>
#define TRUE 1

23
#define FALSE 0
class queue
{
private:
int front,rear,*queue1,maxsize;
public:
queue(int maxqueuesize=20);
int isempty();
void insert(int);
int delet();
void display();
};
queue::queue(int maxqueuesize)
{
maxsize=maxqueuesize;
queue1=new int[maxsize];
front=rear=1;
}
int queue::isempty()
{
if(front==rear)
return TRUE;
else
return FALSE;

24
}
void queue::insert(int vno)
{
queue1[++rear]=vno;
}
int queue::delet()
{
int val; val=queue1[+
+front]; return(val);
}
void queue::display()
{
for(int i=front+1;i<rear;i++)
cout<<queue1[i];
}
class graph
{
public:
int a[10][10],visited[10];
int cnt,i,j,k,nxtedge;
void input(int);
void bfs(int,int);
};
void graph::input(int n)

25
{
cout<<”\n\tEnter the adjacency matrix”;
for(i=0;i<n;i++)
{
cout<<”\n\t”;
for(j=0;j<n;j++)
cin>>a[i][j];
}
for(i=0;i<n;i++)
{
visited[i]=0;
cnt=0;
}
}
void graph::bfs(int v,int n1)
{
queue q;
visited[v]=1;
cout<<”0”;
q.insert(v); while(!
q.isempty())
{
v=q.delet();
for(i=0;i<n1;i++)

26
{
if(a[v][i]==1)
if(visited[i]==0)
{
q.insert(i);
visited[i]=1;
cout<<i<<” “;
}
}
}
}
void main()
{
clrscr();
int n;
cout<<”\t\t\t Breadth First Serach”;
cout<<”\n\tEnter the number of vertices”;
cin>>n;
graph g;
g.input(n);
cout<<”\n\t BFS is done as follows”;
g.bfs(0,n);
getch();
}

27
Output:

Result:
Thus ,the above program was executed successfully.

28
Ex.No:06 DEPTH FIRST SEARCH
Date :24.02.25

Aim :
To perform graph traversal using breadth first search.
Algorithm :
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Read the number of vertices and adjacency matrix.
Step 4 : Start traverse from 0 and visit its child nodes.
Step 5 : Store the traverse, the order in which they are visited.
Step 6 : Use a queue to store the node and mark it as visited until its
neighbours are marked.
Step 7 : The node that was inserted first will be verified first and so
on .
Step 8 : Repeat for all the vertex.
Step 9 : Save the program, compile and execute it.
Step 10: stop the program.
Program coding:
#include<iostream.h>
#include<conio.h>
class graph
{
public:

29
int a[10][10],visited[10],cnt,i,j,k,n,nxtedge;
void input(int);
int dfs(int,int);
};
void graph::input(int n)
{
cout<<”\n\tEnter the adjacency matrix”;
for(i=1;i<=n;i++)
{
cout<<”\n\t”;
for(j=1;j<=n;j++)
cin>>a[i][j];
}
for(i=1;i<=n;i++)
visited[i]=0;
cnt=0;
}
int graph::dfs(int v,int n1)
{
for(i=v;i<=n1;i++)
{
if(visited[i]==0)
{
visited[i]=1;

30
cout<<i<<” “;
cnt+=1;
if(cnt==n1)
return 0;
}
else
{
i=nxtedge;
j+=1;
goto label;
}
for(j=1;j<=n1;j++)
{
label: if(a[i]
[j]==1)
{
nxtedge=i;
dfs(j,n1);
}
}
}
return 0;
}
void main()

31
{
clrscr();
int n;
cout<<”\t\t\tDepth First Search”;
cout<<”\nEnter number of vertices”;
cin>>n;
graph g;
g.input(n);
cout<<”\nDFS is done as follows”;
g.dfs(1,n);
getch();
}

Output:

Result:
Thus,the above program was executed successfully.

32
Ex.No:07 MERGE SORT
Date:03.03.25

Aim :
To sort a list of numbers using merge sort.
Algorithm :
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Read the elements for the list.
Step 4 : Divide the unsorted list into n sub-list, each comparing 1
element.
Step 5 : Compare the adjacent list of two data values and merge
them into a list
of data values.
Step 6 : Repeat this process til there is only one sorted list.
Step 7 : Display the sorted list.
Step 8 : Save the program, compile and execute it.
Step 9: Stop the program.
Program coding:
#include<iostream.h>
#include<conio.h>
class mergesort
{
private:

33
int *x,*y,*z;
int item1,item2;
public:
mergesort(int,int);
void input1(int []);
void input2(int[]);
void bsort(int[],int);
void display();
void sort();
};
mergesort::mergesort(int p,int q)
{
item1=p;
item2=q;
x=new int[item1];
y=new int[item2];
z=new int[item1+item2];
}
void mergesort::input1(int a[])
{
for(int i=0;i<item1;i++)
x[i]=a[i];
bsort(x,item1);
cout<<”\nThe sorted first array”;

34
for(i=0;i<item1;i++)
cout<<x[i]<<” “;
}
void mergesort::input2(int b[])
{
for(int i=0;i<item2;i++)
y[i]=b[i];
bsort(y,item2);
cout<<”\nThe sorted second array”;
for(i=0;i<item2;i++)
cout<<y[i]<<” “;
}
void mergesort::bsort(int m[],int n)
{
int swap=1;
for( int i=0;i<n&&swap==1;i++)
{
swap=0;
for( int j=0;j<n-(i+1);j++)
{
if(m[j]>m[j+1])
{
int temp;
temp=m[j];

35
m[j]=m[j+1];
m[j+1]=temp;
swap=1;
}
}
}
}
void mergesort::display()
{
for(int i=0;i<item1+item2;i++)
cout<<z[i]<<” “;
}
void mergesort::sort()
{
int i,j,k;
i=j=k=0;
while((i<item1)&&(j<item2))
{
if(x[i]<y[j])
{
z[k]=x[i];
i++;
k++;
}

36
else
{
if(x[i]>y[j])
{
z[k]=y[j];
j++;
k++;
}
else
{
z[k]=x[i];
i++;
j++;
k++;
}
}
}
while(i<item1)
{
z[k]=x[i];
i++;
k++;
}
while(j<item2)

37
{
z[k]=y[j];
j++;
k++;
}
}
void main()
{
int e1[100],n1,e2[100],n2,i;
clrscr();
cout<<”\t\t\t Merge sort”;
cout<<”\nEnter number of elements for first array”;
cin>>n1;
cout<<”\nEnter the elements”;
for(i=0;i<n1;i++)
cin>>e1[i];
cout<<”\nEnter number of elements for second array”;
cin>>n2;
cout<<”\nEnter the elements”;
for(i=0;i<n2;i++)
cin>>e2[i];
mergesort m(n1,n2);
m.input1(e1);
m.input2(e2);

38
m.sort();
cout<<”\nThe sorted merged array is”;
m.display();
getch();

Output:

Result:
Thus ,the above program was executed successfully.
39
Ex.No:08 QUICK SORT
Date:05.03.25

Aim :
To sort a list of numbers using quick sort.
Algorithm :
Step 1 : Start the program.
Step 2 : Declare the necessary variables.
Step 3 : Read the elements for the array.
Step 4 : First points to the first element in the array and last points to
the last element.
Step 5 : Compare the array elements with other element and swap
the elements for sorting.
Step 6 : Recursively apply the quick sort method to sort the element
in the array.
Step 7 : Display the sorted list.
Step 8 : Save the program, compile and execute it.
Program coding:
#include<iostream.h>
#include<conio.h>
void quicksort(int [],int,int);
int partition(int [],int,int);
void main()
{
int a[10],n,i;
40
clrscr();
cout<<”\t\t\tQuick sort”;
cout<<”\nEnter the number of elements”;
cin>>n;
cout<<”\nEnter the element”;
for(i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<”\nArray after sorting”;
for(i=0;i<n;i++)
cout<<a[i]<<” “;
getch();
}
void quicksort(int a[],int l,int u)
{
int j;
if(l<u)
{
j=partition(a,l,u);
quicksort(a,l,j-1);
quicksort(a,j+1,u);
}
}
int partition(int a[],int l,int u)

41
{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}

42
Output:

Result:
Thus, the above program was executed successfully.

43

You might also like