Some Dsa Programs
Some Dsa Programs
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<<"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
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);
}
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
{
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);
}
return(0);
}
return(0);
}
#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');
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>
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!";
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!!!!";
}
}
}
};
int main() {
int choice, exit_p = 1;
Queue obj;
do {
cout<< "\n\n Queue Main Menu";
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>
#include<stdlib.h>
struct cqueue
int item[size];
int front,rear;
}s;
void menu();
p->front=size-1;
p->rear=size-1;
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 x;
if(p->front==p->rear)
printf("Queue Underflow");
exit(1);
else
if(p->front==size-1)
p->front=0;
else
p->front=p->front+1;
x=p->item[p->front];
return 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");
j=1;
if(j==0)
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)
/*;*/
void menu()
printf("\nMenu:\n1.Insert\n2.Delete\n3.Search\n4.Change\n5.Display\n6.Exit");
int i;
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();
scanf("%d",&a);
switch(a)
case 1://insert
scanf("%d",&x);
enqueue(&s,x);
break;
case 2://delete
dequeue(&s);
break;
case 3://search
scanf("%d",&x);
search(&s,x);
break;
case 4://change
scanf("%d\t%d",&o,&n);
change(&s,o,n);
break;
case 5://display
display(&s);
break;
case 6://exit
exit(0);
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>
int data;
}node;
}snglist;
l->head=NULL;
l->tail=NULL;
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;
l->head=p;
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;
node *current=l->head;
node *p=(node*)malloc(sizeof(node));
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;
node *current=l->head;
int data;
if(l->head==NULL)
exit(1);
data=l->head->data;
l->head=l->head->next;
free(current);
return data;
node *current=l->head;
node *pred=current;
int data;
if(l->head==NULL)
exit(1);
while(current->next!=NULL)
pred=current;
current=current->next;
data=current->data;
l->tail=pred;
pred->next=NULL;
free(current);
return data;
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;
i++;
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)
node *current=l->head;
int data;
while(current!=NULL)
data=current->data;
addfirst(a,data);
current=current->next;
display(a);
int i;
node *current=l->head;
while(current->next!=NULL)
i=i+1;
current=current->next;
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;
display(l);
void menu()
printf("\n---Menu---");
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();
scanf("%d",&a);
switch(a)
case 1://insert
scanf("%d",&x);
addfirst(&l,x);
break;
case 2://insend
scanf("%d",&x);
addlast(&l,x);
break;
case 3://insat
scanf("%d",&x);
addinorder(&l,x);
break;
case 4://deletefirst
delfirst(&l);
break;
case 5://deletelast
delend(&l);
break;
case 6://display
display(&l);
break;
case 7://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;
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;
};
else
{
l->head->previous=p;
p->next=l->head;
l->head=p;
}
}
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 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);
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;
}
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
---Menu---
1.Insert-Add at first
2.Insend-Add at last
3.Insat-Add in order
6.Display
7.Exit
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
6.Display
7.Exit
Program:-
#include <stdio.h>
#define max 5
int main()
{
int i,n,arr[max],s;
int num;
int position;
PROGRAM:-
#include<stdio.h>
#include<conio.h>
int i, j, temp;
temp = arr[i];
arr[j] = arr[j-1];
arr[j] = temp;
int main()
scanf("%d",&n);
scanf("%d",&arr[i]);
sort(arr, n);
printf("After sorting:");
printf("\n");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
first = middle + 1;
found=1;
break;
else
last = middle - 1;
return 0;
Output:-
24
22
21
28
After sorting: 21 22 24 26 28
26 found at location 4
Program:-
#include<stdio.h>
int main(){
scanf("%d",&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;
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
Output:-
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()
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\n", array[c]);
return 0;
OUTPUT:-
Enter 5 integers
60
50
43
25
51
25
43
50
51
60
Program:=
#include<iostream>
#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;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
merge_sort(0,n-1);
for(i=0;i<n;i++)
cout<<"\t"<<arr[i];
int m;
if(i<j)
m=(i+j)/2;
merge_sort(i,m);
merge_sort(m+1,j);
merge(i,m,m+1,j);
int t[50];
int i=a,j=c,k=0;
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)
t[k]=arr[j];
k++;
j++;
for(i=a,j=0;i<=d;i++,j++)
arr[i]=t[j];
OUTPUT:-
No. of elements:
60
25
50
43
51
25 43 50 51 60
Program:-
#include<stdio.h>
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);
int main(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
OUTPUT:-
Enter 5 elements: 60
50
43
51
25
PROGRAM:-
#include<iostream>
#include <list>
class Graph
int V;
list<int> *adj;
public:
Graph(int V);
};
Graph::Graph(int V)
this->V = V;
adj[v].push_back(w);
void Graph::BFS(int s)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
list<int>::iterator i;
while(!queue.empty())
s = queue.front();
queue.pop_front();
if (!visited[*i])
visited[*i] = true;
queue.push_back(*i);
int main()
int n,s,no_of_edges,vi,vj;
cin>>n;
Graph g(n);
cin>>no_of_edges;
for(i=0;i<no_of_edges;i++)
printf("Enter an edge(u,v):");
cin>>vi;
cin>>vj;
g.addEdge(vi,vj);
cin>>s
g.BFS(s);
return 0;
OUTPUT:-
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
2031
PRACTICAL 11:- Write a program to implement depth first search (DFS) graph
traversal algorithm.
PROGRAM:-
#include<stdio.h>
#include<stdlib.h>
int vertex;
}node;
node *G[20];
int visited[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
int main()
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;
void read_graph()
int i,vi,vj,no_of_edges;
scanf("%d",&n);
for(i=0;i<n;i++)
G[i]=NULL;
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);
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
OUTPUT:-
Enter an edge(u,v):0 1
Enter an edge(u,v):0 2
Enter an edge(u,v):1 2