DATA STRUCTURES AND ALGORITHM 1721BECE30050
Practical 1 Write a menu driven program to perform the following operations on the
STACK using an array. 1. Push 2.Pop 3. Peep 4. Change 5. Display the contents 6. Exit
#include<stdio.h>
#include<iostream>
using namespace std;
#include<stdlib.h>
int s[9],n,x,i;
int top;
int main()
{
stack:
//while(1);
//{
cout<<"\n1. push \n2.pop \n3.peep \n4.change \n5.display \n6.exit";
cout<<"\n select any one option from the above to perform operation:";
cin>>n;
switch(n)
{
case 1:if(top>11)
cout<<"\n stack overflow";
else
{
cout<<"\n enter the element to be inserted:";
cin>>x;
top=top+1;
s[top]=x;
cout<<"ELEMENT IS SUCEESSFULLY INSERTED";
} goto stack;
break;
case 2:if(top==0)
cout<<"\n STACK IS ON UNDERFLOW";
else
{
top=top-1;
cout<<"\n ELEMENT DELETED\n";
cout<<"\n DELETED ELEMENT IS :"<<s[top+1];
}goto stack;
break;
case 3:cout<<"enter position of the element to be returned";
cin>>i;
if(top-i+1<=0)
SUMIT KHAMAR(C.E. Dept) Page 1
DATA STRUCTURES AND ALGORITHM 1721BECE30050
{
cout<<"STACK IS UNDERFLOW ON PEEP";
}
else
cout<<s[i];
goto stack;
break;
case 4: cout<<"enter the position of the element to be changed:";
cin>>i;
if(top-i+1<=0)
{
cout<<"UNDERFLOW";
}
else
{
cout<<"enter element to be inserted";
cin>>x;
s[top-i+1]=x;
}goto stack;
break;
case 5:for(int j=top;j>0;--j)
{
cout<<s[j]<<"\n";
}goto stack;
break;
case 6:exit(0);
default: cout<<"invalid choice";
cout<<"\n ENTER THE CHOICE AGAIN!!!!";
goto stack;
break;
}
//}
return 0;
}
Practical 2 Write a program to convert an infix expression into reverse polish (postfix)
notation with parenthesis.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 50
SUMIT KHAMAR(C.E. Dept) Page 2
DATA STRUCTURES AND ALGORITHM 1721BECE30050
typedef struct stack
{
int data[MAX];
int top;
}stack;
int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *); //value of the top element
void infix_to_postfix(char infix[],char postfix[]);
void main()
{
char infix[30],postfix[30];
printf("Enter an infix expression(eg: 5+2*4): ");
gets(infix);
infix_to_postfix(infix,postfix);
printf("\nPostfix expression: %s",postfix);
}
void infix_to_postfix(char infix[],char postfix[])
{
stack s;
char x,token;
int i,j; //i-index of infix,j-index of postfix
init(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
token=infix[i];
if(isalnum(token))
postfix[j++]=token;
else
if(token=='(')
push(&s,'(');
else
if(token==')')
while((x=pop(&s))!='(')
postfix[j++]=x;
else
{
SUMIT KHAMAR(C.E. Dept) Page 3
DATA STRUCTURES AND ALGORITHM 1721BECE30050
while(precedence(token)<=precedence(top(&s))&&!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
push(&s,token);
}
}
while(!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
postfix[j]='\0';
}
int precedence(char x)
{
if(x=='(')
return(0);
if(x=='+'||x=='-')
return(1);
if(x=='*'||x=='/'||x=='%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
SUMIT KHAMAR(C.E. Dept) Page 4
DATA STRUCTURES AND ALGORITHM 1721BECE30050
return(0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
int top(stack *p)
{
return (p->data[p->top]);
}
3 Write a program to solve the problem of Tower of Hanoi (Application of stack
#include<iostream>
using namespace std;
#include<stdio.h>
void tower(int n,char from_rod, char to_rod, char aux_rod )
{
if(n==1)
{
cout<<" \n move disk 1 from "<<from_rod <<" to "<<to_rod;
return;
}
tower(n-1,from_rod,aux_rod,to_rod);
cout<<"\n move disk"<<n<<" from "<<from_rod<<" to "<<to_rod;
tower(n-1,aux_rod,to_rod,from_rod);
}
int main()
{
int n;
cout<<"enter the no. of disk==>";
cin>>n;
tower(n,'A','B','C');
SUMIT KHAMAR(C.E. Dept) Page 5
DATA STRUCTURES AND ALGORITHM 1721BECE30050
return 0;
}
Practical 4 Write a menu driven program to perform the following operations on the
QUEUE using an array. 1. Insert 2. Delete 3. Search 4. Change 5. Display the contents 6.
Exit
#include <iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 100
using namespace std;
class Queue {
private:
int item, i;
int arr_queue[MAX_SIZE];
int rear;
int front;
public:
Queue() {
rear = 0;
front = 0;
}
void insert() {
if (rear == MAX_SIZE)
cout<< "\n## Queue Reached Max!";
else {
cout<< "\nEnter The Value to be Insert : ";
cin>>item;
cout<< "\n## Position : " << rear + 1 << " , Insert Value : " << item;
arr_queue[rear++] = item;
cout<<"\n INSERTED SUCESSFULLY";
}
}
void removeData() {
if (front == rear)
cout<< "\n## Queue is Empty!";
SUMIT KHAMAR(C.E. Dept) Page 6
DATA STRUCTURES AND ALGORITHM 1721BECE30050
else {
cout<< "\n## Position : " << front << " , Remove Value :" <<arr_queue[front];
front++;
cout<<"\n DELETED SUCESSFULLY";
}
}
void display() {
cout<< "\n## Queue Size : " << (rear - front);
for (i = front; i< rear; i++)
cout<< "\n## Position : " <<i+1<< " , Value : " <<arr_queue[i];
}
void change()
{
int x;
cout<<"enter the index position to change the element:";
cin>>i;
if(rear-i+1<=0){
cout<<"invalid input!!";
}
else
{
cout<<"enter the value to change:";
cin>>x;
arr_queue[front+i-1]=x;
cout<<"\n changed successfully";
}
}
void search()
{
int m,i,j;
cout<<"enter the value you want to find:";
cin>>m;
for(i=front;i<=rear;i++)
{
if(arr_queue[i]==m)
{
cout<<"\n SEARCH FOUND";
j=1;
}
if(j==0)
{
cout<<"\n SEARCH NOT FOUND!!!!";
}
}
SUMIT KHAMAR(C.E. Dept) Page 7
DATA STRUCTURES AND ALGORITHM 1721BECE30050
}
};
int main() {
int choice, exit_p = 1;
Queue obj;
do {
cout<< "\n\n Queue Main Menu";
cout<< "\n1.Insert \n2.Remove \n3.Display \n4.change \n5.search \n.Others to exit";
cout<< "\nEnter Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
obj.insert();
break;
case 2:
obj.removeData();
break;
case 3:
obj.display();
break;
case 4:
obj.change();
break;
case 5:
obj.search();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return 0;
}
Practical 5:- Write a menu driven program to perform the following operations on the
CIRCULARQUEUE using an array. 1. Insert 2. Delete 3. Search 4. Change
5. Display the contents 6. Exit
#include<stdio.h>
SUMIT KHAMAR(C.E. Dept) Page 8
DATA STRUCTURES AND ALGORITHM 1721BECE30050
#include<stdlib.h>
#define size 100
struct cqueue
int item[size];
int front,rear;
}s;
void init(struct cqueue *);
void enqueue(struct cqueue *,int);
int dequeue(struct cqueue *);
void search(struct cqueue *,int);
void change(struct cqueue *,int,int);
void display(struct cqueue *);
void menu();
void init(struct cqueue *p)
p->front=size-1;
p->rear=size-1;
void enqueue(struct cqueue *p,int x)
SUMIT KHAMAR(C.E. Dept) Page 9
DATA STRUCTURES AND ALGORITHM 1721BECE30050
if((p->rear+1)%size==p->front)
printf("Queue Overflow");
exit(1);
else
if(p->rear==size-1)
p->rear=0;
else
p->rear=p->rear+1;
p->item[p->rear]=x;
int dequeue(struct cqueue *p)
int x;
if(p->front==p->rear)
printf("Queue Underflow");
SUMIT KHAMAR(C.E. Dept) Page 10
DATA STRUCTURES AND ALGORITHM 1721BECE30050
exit(1);
else
if(p->front==size-1)
p->front=0;
else
p->front=p->front+1;
x=p->item[p->front];
return x;
void search(struct cqueue *p,int x)
int i,j=0;
for(i=(p->front+1)%size;i!=(p->rear+1)%size;i=(i+1)%size)
if(p->item[i]==x)
printf("Match Found");
SUMIT KHAMAR(C.E. Dept) Page 11
DATA STRUCTURES AND ALGORITHM 1721BECE30050
j=1;
if(j==0)
printf("No Match Found");
void change(struct cqueue *p,int o,int n)
int i,j=0;
for(i=(p->front+1)%size;i!=(p->rear+1)%size;i=(i+1)%size)
if(p->item[i]==o)
p->item[i]=n;
j=1;
if(j==0)
printf("No Match Found");
SUMIT KHAMAR(C.E. Dept) Page 12
DATA STRUCTURES AND ALGORITHM 1721BECE30050
/*;*/
void menu()
printf("\nMenu:\n1.Insert\n2.Delete\n3.Search\n4.Change\n5.Display\n6.Exit");
void display(struct cqueue *p)
int i;
printf("The Queue is: ");
for(i=(p->front+1)%size;i!=(p->rear+1)%size;i=(i+1)%size)
printf("%d ",p->item[i]);
int main()
int a,x,i,o,n;
char ch;
label:menu();
printf("\nEnter Your Choice: ");
scanf("%d",&a);
SUMIT KHAMAR(C.E. Dept) Page 13
DATA STRUCTURES AND ALGORITHM 1721BECE30050
switch(a)
case 1://insert
printf("Enter Value: ");
scanf("%d",&x);
enqueue(&s,x);
break;
case 2://delete
dequeue(&s);
break;
case 3://search
printf("Enter Value to search: ");
scanf("%d",&x);
search(&s,x);
break;
case 4://change
printf("Enter the old value and new value: ");
scanf("%d\t%d",&o,&n);
change(&s,o,n);
break;
case 5://display
display(&s);
break;
case 6://exit
exit(0);
SUMIT KHAMAR(C.E. Dept) Page 14
DATA STRUCTURES AND ALGORITHM 1721BECE30050
break;
default:
printf("Invalid input");
if(a!=6)
goto label;
return 0;
6 Write a menu driven program to perform the following operations on a Singly Linked
list.
1. Insert 6. Search
2. Insend 7. Sort
3. Insat 8. Count
4. Delete 9. Display
5. Reverse 10. Exit
#include<stdio.h>
#include<stdlib.h>
typedef struct node
int data;
struct node *next;
}node;
SUMIT KHAMAR(C.E. Dept) Page 15
DATA STRUCTURES AND ALGORITHM 1721BECE30050
typedef struct linkedlist
struct node *head;
struct node *tail;
}snglist;
void init(snglist *l)
l->head=NULL;
l->tail=NULL;
void addfirst(snglist *l,int data)
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->next=NULL;
if(l->head==NULL)
l->head=p;
l->tail=p;
else
p->next=l->head;
SUMIT KHAMAR(C.E. Dept) Page 16
DATA STRUCTURES AND ALGORITHM 1721BECE30050
l->head=p;
void addlast(snglist *l,int data)
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->next=NULL;
if(l->head==NULL)
l->head=p;
l->tail=p;
else
l->tail->next=p;
l->tail=p;
void addinorder(snglist *l,int data)
node *current=l->head;
node *p=(node*)malloc(sizeof(node));
SUMIT KHAMAR(C.E. Dept) Page 17
DATA STRUCTURES AND ALGORITHM 1721BECE30050
p->data=data;
p->next=NULL;
if(l->head==NULL)
l->head=p;
l->tail=p;
if(p->data<l->head->data)
addfirst(l,data);
exit(1);
if(l->tail->data<p->data)
addlast(l,data);
exit(0);
while(current->next->data<p->data)
current=current->next;
p->next=current->next;
current->next=p;
SUMIT KHAMAR(C.E. Dept) Page 18
DATA STRUCTURES AND ALGORITHM 1721BECE30050
int delfirst(snglist *l)
node *current=l->head;
int data;
if(l->head==NULL)
printf("The List is Empty");
exit(1);
data=l->head->data;
l->head=l->head->next;
free(current);
return data;
int delend(snglist *l)
node *current=l->head;
node *pred=current;
int data;
if(l->head==NULL)
printf("The List is Empty");
exit(1);
SUMIT KHAMAR(C.E. Dept) Page 19
DATA STRUCTURES AND ALGORITHM 1721BECE30050
while(current->next!=NULL)
pred=current;
current=current->next;
data=current->data;
l->tail=pred;
pred->next=NULL;
free(current);
return data;
void display(snglist *l)
if(l->head==NULL)
printf("List is Empty");
exit(1);
int i=1;
node *current=l->head;
while(current!=NULL)
printf("\n%d.%p[%d|%p]",i,current,current->data,current->next);
current=current->next;
SUMIT KHAMAR(C.E. Dept) Page 20
DATA STRUCTURES AND ALGORITHM 1721BECE30050
i++;
void search(snglist *l,int data)
node *current=l->head;
int i=0;
while(current!=NULL)
if(current->data==data)
printf("Match Found");
i=1;
break;
current=current->next;
if(i==0)
printf("No Match Found");
void reverse(snglist *l,snglist *a)
SUMIT KHAMAR(C.E. Dept) Page 21
DATA STRUCTURES AND ALGORITHM 1721BECE30050
node *current=l->head;
int data;
while(current!=NULL)
data=current->data;
addfirst(a,data);
current=current->next;
printf("The Reversed List is: ");
display(a);
void count(snglist *l)
int i;
node *current=l->head;
while(current->next!=NULL)
i=i+1;
current=current->next;
printf("The number of nodes is: %d",i+1);
SUMIT KHAMAR(C.E. Dept) Page 22
DATA STRUCTURES AND ALGORITHM 1721BECE30050
void sort(snglist *l)
node *current=l->head;
node *p;
int d;
while(current!=NULL)
p=current->next;
while(p!=NULL)
if(current->data>p->data)
d=current->data;
current->data=p->data;
p->data=d;
p=p->next;
current=current->next;
printf("The Sorted List is: ");
display(l);
void menu()
SUMIT KHAMAR(C.E. Dept) Page 23
DATA STRUCTURES AND ALGORITHM 1721BECE30050
printf("\n---Menu---");
printf("\n1.Insert-Add a node in first Position");
printf("\n2.Insend-Add a node in last Position");
printf("\n3.Insat-Insert node in order");
printf("\n4.Delete a node at first");
printf("\n5.Delete a node at last");
printf("\n6.Display-Print the Linked List");
printf("\n7.Search");
printf("\n8.Reverse");
printf("\n9.Count");
printf("\n10.Sort");
printf("\n11.Exit");
int main()
int a,x,i,o,n;
snglist l;
snglist r;
init(&l);
init(&r);
label:menu();
printf("\nEnter Your Choice: ");
scanf("%d",&a);
SUMIT KHAMAR(C.E. Dept) Page 24
DATA STRUCTURES AND ALGORITHM 1721BECE30050
switch(a)
case 1://insert
printf("Enter Value: ");
scanf("%d",&x);
addfirst(&l,x);
break;
case 2://insend
printf("Enter Value: ");
scanf("%d",&x);
addlast(&l,x);
break;
case 3://insat
printf("Enter Value: ");
scanf("%d",&x);
addinorder(&l,x);
break;
case 4://deletefirst
delfirst(&l);
break;
case 5://deletelast
delend(&l);
break;
case 6://display
printf("\nThe List is:");
SUMIT KHAMAR(C.E. Dept) Page 25
DATA STRUCTURES AND ALGORITHM 1721BECE30050
display(&l);
break;
case 7://search
printf("Enter Value to search: ");
scanf("%d",&x);
search(&l,x);
break;
case 8://reverse
reverse(&l,&r);
break;
case 9://count
count(&l);
break;
case 10://sort
sort(&l);
break;
case 11://exit
exit(0);
break;
default:
printf("Invalid input");
if(a!=11)
goto label;
SUMIT KHAMAR(C.E. Dept) Page 26
DATA STRUCTURES AND ALGORITHM 1721BECE30050
return 0;
Practical 7:- Write a menu driven program to perform the following operations on a
Doubly Linked list. 1. Insert 2.Insend 3. Insat 4. Delete 5. Display 6. Exit
Program:-
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *previous;
struct Node *next;
};
struct Doublylist
{
struct Node *head;
struct Node *tail;
};
void init(struct Doublylist *l)
{
l->head=NULL;
l->tail=NULL;
}
void addfirst(struct Doublylist *l,int data)
{
struct Node *p=(struct Node *)malloc(sizeof(struct Node));
p->data=data;
p->next=NULL;
p->previous=NULL;
if(l->head==NULL)
{
l->head=p;
l->tail=p;
}
SUMIT KHAMAR(C.E. Dept) Page 27
DATA STRUCTURES AND ALGORITHM 1721BECE30050
else
{
l->head->previous=p;
p->next=l->head;
l->head=p;
}
}
void addlast(struct Doublylist *l,int data)
{
struct Node *p=(struct Node *)malloc(sizeof(struct Node));
p->data=data;
p->next=NULL;
p->previous=NULL;
if(l->head==NULL)
{
l->head=p;
l->tail=p;
}
else
{
l->tail->next=p;
p->previous=l->tail;
l->tail=p;
}
}
void addinorder(struct Doublylist *l,int data)
{
struct Node *current=l->head;
struct Node *p=(struct Node *)malloc(sizeof(struct Node));
p->data=data;
p->next=NULL;
p->previous=NULL;
if(l->head==NULL)
{
l->head=p;
l->tail=p;
}
if(p->data<current->data)
{
addfirst(l,data);
return;
}
if(p->data>l->tail->data)
{
SUMIT KHAMAR(C.E. Dept) Page 28
DATA STRUCTURES AND ALGORITHM 1721BECE30050
addlast(l,data);
return;
}
while(current->next->data<p->data)
{
current=current->next;
}
p->previous=current;
p->next=current->next;
current->next->previous=p;
current->next=p;
}
int deletefirst(struct Doublylist *l)
{
int v;
struct Node *current=l->head;
if(l->head==NULL)
{
printf("The list is Empty");
exit(0);
}
v=current->data;
l->head=current->next;
current->next=NULL;
free(current);
return v;
}
int deletelast(struct Doublylist *l)
{
int v;
struct Node *current=l->tail;
if(l->head==NULL)
{
printf("The list is Empty");
exit(0);
}
v=current->data;
l->tail=current->previous;
current->previous->next=NULL;
free(current);
return v;
}
void display(struct Doublylist *l)
SUMIT KHAMAR(C.E. Dept) Page 29
DATA STRUCTURES AND ALGORITHM 1721BECE30050
{
int i=1;
struct Node *current=l->head;
if(l->head==NULL)
{
printf("The list is Empty");
return;
}
while(current!=NULL)
{
printf("%d.%p [%p|%d|%p]\n",i,current,current->previous,current->data,current-
>next);
current=current->next;
i++;
}
}
void menu()
{
printf("\n---Menu---\n1.Insert-Add at first\n2.Insend-Add at last\n3.Insat-Add in
order\n4.Delete a node at first\n5.Delete a node at last\n6.Display\n7.Exit\n");
}
int main()
{
int a,x,i,o,n;
struct Doublylist Dlist;
init(&Dlist);
label:menu();
printf("Enter the choice: ");
scanf("%d",&a);
switch(a)
{
case 1://add at first
printf("Enter the data you want to insert: ");
scanf("%d",&x);
addfirst(&Dlist,x);
break;
case 2://add at last
printf("Enter the data you want to insert: ");
scanf("%d",&i);
addlast(&Dlist,i);
break;
case 3://add in order
printf("Enter the data you want to insert: ");
scanf("%d",&x);
SUMIT KHAMAR(C.E. Dept) Page 30
DATA STRUCTURES AND ALGORITHM 1721BECE30050
addinorder(&Dlist,x);
break;
case 4://delete first
o=deletefirst(&Dlist);
printf("The deleted node is: %d",o);
break;
case 5://delete last
n=deletelast(&Dlist);
printf("The deleted node is: %d",n);
break;
case 6://display
printf("The list is:\n");
display(&Dlist);
break;
case 7://exit
exit(0);
break;
default:
printf("Invalid Input");
}
if(a!=7)
{
goto label;
}
return 0;
}
Output of the doubly linked list:-
--Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 1
SUMIT KHAMAR(C.E. Dept) Page 31
DATA STRUCTURES AND ALGORITHM 1721BECE30050
Enter the data you want to insert: 60
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 2
Enter the data you want to insert: 50
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 1
Enter the data you want to insert: 43
---Menu---
SUMIT KHAMAR(C.E. Dept) Page 32
DATA STRUCTURES AND ALGORITHM 1721BECE30050
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 3
Enter the data you want to insert: 60
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 4
The deleted node is: 43
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
SUMIT KHAMAR(C.E. Dept) Page 33
DATA STRUCTURES AND ALGORITHM 1721BECE30050
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 5
The deleted node is: 60
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
7.Exit
Enter the choice: 6
The list is:
1.00000000001B6C50 [00000000001B6C90|60|00000000001B6C70]
2.00000000001B6C70 [00000000001B6C50|50|0000000000000000]
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
4.Delete a node at first
5.Delete a node at last
6.Display
SUMIT KHAMAR(C.E. Dept) Page 34
DATA STRUCTURES AND ALGORITHM 1721BECE30050
7.Exit
Enter the choice: 7
Practical 8(a):- Write a program to implement Searching Algorithms Sequential search
Program:-
#include <stdio.h>
#define max 5
int main()
{
int i,n,arr[max],s;
int num;
int position;
printf("\nEnter array elements:\n");
for(i=0;i< max;i++)
scanf("%d",&arr[i]);
printf("\nNow enter element to search :");
scanf("%d",&num);
for(i=0;i<max;i++)
{
if(arr[i]==num){s=1; position=i;}
}
if(s==1)
{
printf("%d element found in array at position %d",arr[position],position+1);
}
else
{
printf("element not found in the array!!");
}
return 0;
}
PRACTICAL 8(b):- Write a program to implement Searching Algorithms using
BINARY SEARCH
SUMIT KHAMAR(C.E. Dept) Page 35
DATA STRUCTURES AND ALGORITHM 1721BECE30050
PROGRAM:-
#include<stdio.h>
#include<conio.h>
void sort(int *arr, int n) {
int i, j, temp;
for (i = 1; i < n; i++) {
temp = arr[i];
for (j =i ; j > 0 && arr[j-1] > temp; j--) {
arr[j] = arr[j-1];
arr[j] = temp;
int main()
int n, i, *arr, search, first, last, middle,found;
printf("Enter total number of elements :");
scanf("%d",&n);
arr = (int *)malloc(sizeof (int) * n);
printf("Enter %d number :", n);
for (i=0; i<n; i++)
scanf("%d",&arr[i]);
SUMIT KHAMAR(C.E. Dept) Page 36
DATA STRUCTURES AND ALGORITHM 1721BECE30050
sort(arr, n);
printf("After sorting:");
for (i = 0; i < n; i++)
printf("%3d ", arr[i]);
printf("\n");
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
if(arr[middle] < search)
first = middle + 1;
else if(arr[middle] == search)
printf("%d found at location %d\n", search, middle+1);
found=1;
break;
else
SUMIT KHAMAR(C.E. Dept) Page 37
DATA STRUCTURES AND ALGORITHM 1721BECE30050
last = middle - 1;
middle = (first + last)/2;
if(first >last && found==0)
printf("Not found! %d is not present in the list.",search);
return 0;
Output:-
Enter total number of elements :5
Enter 5 number :26
24
22
21
28
After sorting: 21 22 24 26 28
Enter a number to find :26
26 found at location 4
Practical 9(a):- Write a program to implement following sorting algorithms
Selection sort
SUMIT KHAMAR(C.E. Dept) Page 38
DATA STRUCTURES AND ALGORITHM 1721BECE30050
Program:-
#include<stdio.h>
int main(){
int i, j, count, temp, number[25];
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
printf("Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
SUMIT KHAMAR(C.E. Dept) Page 39
DATA STRUCTURES AND ALGORITHM 1721BECE30050
return 0;
Output:-
How many numbers u are going to enter?: 6
Enter 6 elements: 28
27
26
60
50
25
Sorted elements: 25 26 27 28 50 60
Practical 9(b):- Write a program to implement following sorting algorithms using Bubble sort
Program:-
#include <stdio.h>
int main()
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
SUMIT KHAMAR(C.E. Dept) Page 40
DATA STRUCTURES AND ALGORITHM 1721BECE30050
for (d = 0 ; d < n - c - 1; d++)
if (array[d] > array[d+1]) /* For decreasing order use < */
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
OUTPUT:-
Enter number of elements
Enter 5 integers
60
50
43
25
51
SUMIT KHAMAR(C.E. Dept) Page 41
DATA STRUCTURES AND ALGORITHM 1721BECE30050
Sorted list in ascending order:
25
43
50
51
60
Practical 9(C):-Write a program to implement following sorting algorithms using merge
sort
Program:=
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
void merge_sort(int,int);
void merge(int,int,int,int);
int arr[50];
int main()
int i,n;
cout<<"\n No. of elements:"<<endl;
cin>>n;
cout<<"\n Enter elements for sorting:"<<endl;
for(i=0;i<n;i++)
SUMIT KHAMAR(C.E. Dept) Page 42
DATA STRUCTURES AND ALGORITHM 1721BECE30050
cin>>arr[i];
merge_sort(0,n-1);
cout<<"\nsorted array using merge sort:"<<endl;
for(i=0;i<n;i++)
cout<<"\t"<<arr[i];
void merge_sort(int i,int j)
int m;
if(i<j)
m=(i+j)/2;
merge_sort(i,m);
merge_sort(m+1,j);
merge(i,m,m+1,j);
void merge(int a,int b,int c,int d)
SUMIT KHAMAR(C.E. Dept) Page 43
DATA STRUCTURES AND ALGORITHM 1721BECE30050
int t[50];
int i=a,j=c,k=0;
while(i<=b && j<=d)
if(arr[i]<arr[j])
t[k]=arr[i];
k++;
i++;
else
t[k]=arr[j];
k++;
j++;
}while(i<=b)
t[k]=arr[i];
k++;
i++;
while(j<=d)
SUMIT KHAMAR(C.E. Dept) Page 44
DATA STRUCTURES AND ALGORITHM 1721BECE30050
t[k]=arr[j];
k++;
j++;
for(i=a,j=0;i<=d;i++,j++)
arr[i]=t[j];
OUTPUT:-
No. of elements:
Enter elements for sorting:
60
25
50
43
51
sorted array using merge sort:
25 43 50 51 60
Practical 9(D):-Write a program to implement following sorting algorithms using quick
sort
Program:-
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
SUMIT KHAMAR(C.E. Dept) Page 45
DATA STRUCTURES AND ALGORITHM 1721BECE30050
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
SUMIT KHAMAR(C.E. Dept) Page 46
DATA STRUCTURES AND ALGORITHM 1721BECE30050
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
OUTPUT:-
How many elements are u going to enter?: 5
SUMIT KHAMAR(C.E. Dept) Page 47
DATA STRUCTURES AND ALGORITHM 1721BECE30050
Enter 5 elements: 60
50
43
51
25
Order of Sorted elements: 25 43 50 51 60
PRACTICAL10 :-Write a program to implement breadth first search (BFS) graph
traversal algorithm
PROGRAM:-
#include<iostream>
#include <list>
using namespace std;
class Graph
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};
Graph::Graph(int V)
SUMIT KHAMAR(C.E. Dept) Page 48
DATA STRUCTURES AND ALGORITHM 1721BECE30050
this->V = V;
adj = new list<int>[V];
void Graph::addEdge(int v, int w)
adj[v].push_back(w);
void Graph::BFS(int s)
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty())
SUMIT KHAMAR(C.E. Dept) Page 49
DATA STRUCTURES AND ALGORITHM 1721BECE30050
s = queue.front();
cout << s << " ";
queue.pop_front();
for (i = adj[s].begin(); i != adj[s].end(); ++i)
if (!visited[*i])
visited[*i] = true;
queue.push_back(*i);
int main()
int n,s,no_of_edges,vi,vj;
cout<<"enter the number of vertices";
cin>>n;
Graph g(n);
for( int i=0;i<n;i++)
cout<<"Enter number of edges:";
cin>>no_of_edges;
SUMIT KHAMAR(C.E. Dept) Page 50
DATA STRUCTURES AND ALGORITHM 1721BECE30050
for(i=0;i<no_of_edges;i++)
printf("Enter an edge(u,v):");
cin>>vi;
cin>>vj;
g.addEdge(vi,vj);
cout<<"enter the starting vertex";
cin>>s
g.BFS(s);
return 0;
OUTPUT:-
enter the number of vertices4
Enter number of edges:6
Enter an edge(u,v):0 1
Enter an edge(u,v):0 2
Enter an edge(u,v):3 3
Enter an edge(u,v):1 2
Enter an edge(u,v):2 0
Enter an edge(u,v):2 3
SUMIT KHAMAR(C.E. Dept) Page 51
DATA STRUCTURES AND ALGORITHM 1721BECE30050
enter the starting vertex2
THE GRAPH TRAVERSA IS:-
2031
PRACTICAL 11:- Write a program to implement depth first search (DFS) graph
traversal algorithm.
PROGRAM:-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
struct node *next;
int vertex;
}node;
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) in te adjacency list
void DFS(int);
int main()
SUMIT KHAMAR(C.E. Dept) Page 52
DATA STRUCTURES AND ALGORITHM 1721BECE30050
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
void DFS(int i)
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
SUMIT KHAMAR(C.E. Dept) Page 53
DATA STRUCTURES AND ALGORITHM 1721BECE30050
void read_graph()
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
G[i]=NULL;
//read edges and insert them in G[]
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
SUMIT KHAMAR(C.E. Dept) Page 54
DATA STRUCTURES AND ALGORITHM 1721BECE30050
void insert(int vi,int vj)
node *p,*q;
//acquire memory for the new node
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
//insert the node in the linked list number vi
if(G[vi]==NULL)
G[vi]=q;
else
//go to end of the linked list
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
SUMIT KHAMAR(C.E. Dept) Page 55
DATA STRUCTURES AND ALGORITHM 1721BECE30050
OUTPUT:-
Enter number of vertices:3
Enter number of edges:3
Enter an edge(u,v):0 1
Enter an edge(u,v):0 2
Enter an edge(u,v):1 2
THE GRAPH TRAVERSAL IS:-
SUMIT KHAMAR(C.E. Dept) Page 56