You are on page 1of 47

Sasi Institute Of Technology & Engineering

Data Structures & Algorithms Lab


-----------------------------------------------------------------------------------------------------------------------------Prog 1: Write C++ programs to implement the Stack ADT using an array.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------void main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT :
1.push 2.pop 3.display
Enter ur choice2
stack under flow
1.push 2.pop 3.display
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display
Enter ur choice2
deleted3
1.push 2.pop 3.display
Enter ur choice1
enter the element5
inserted5
1.push 2.pop 3.display
Enter ur choice3
52
1.push 2.pop 3.display
Enter ur choice4

4.exit
4.exit

4.exit

4.exit
4.exit

4.exit
4.exit

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 2 : Write C++ programs to implement the Queue ADT using an array
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------main()
{
int ch;
queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet();
break;
case 3: qu.display();
break;
case 4: exit(0);
}
}
return (0);
}

OUTPUT :
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16
1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16 1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21
1.insert 2.delet 3.display 4.exit
Enter ur choice3
22 16
1.insert 2.delet 3.display 4.exit
Enter ur choice

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 3 : Write C++ programs to implement the Stack ADT using a singly linked list
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *next;
int data;
};
class stack : public node
{
node *head;
int tos;
public:
stack()
{
tos=-1;
}
void push(int x)
{
if (tos < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else
{
node *temp,*temp1;
temp=head;
if(tos >= 4)
{
cout <<"stack over flow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
5

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
main()
{
stack s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n enter a element";
cin >> ch;
s1.push(ch);
break;
case 2: s1.pop();
break;
case 3: s1.display();
break;
case 4: exit(0);
}
}
return (0);
}
6

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------OUTPUT :
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
stack under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 4 : Write C++ programs to implement the Queue ADT using a singly linked list.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *next;
int data;
};
class queue : public node
{
node *head;
int front,rare;
public:
queue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if (rare < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
rare ++;
}
else
{
node *temp,*temp1;
temp=head;
if(rare >= 4)
{
cout <<"queue over flow";
return;
}
rare++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------void display()
{
node *temp;
temp=head;
if (rare < 0)
{
cout <<" queue under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( rare < 0)
{
cout <<"queue under flow";
return;
}
if(front == rare)
{
front = rare =-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
queue s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n enter a element";
cin >> ch;
s1.push(ch); break;
case 2: s1.pop();break;
case 3: s1.display();break;
case 4: exit(0);
}
}
9

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------return (0);
}
OUTPUT :
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
queue under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

10

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 5 : Write a C++ program to implement circular queue ADT using an array.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class cqueue
{
int q[5],front,rare;
public:
cqueue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if(front ==-1 && rare == -1)
{
q[++rare]=x;
front=rare;
return;
}
else if(front == (rare+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rare= (rare+1)%5;
q[rare]=x;
}
void pop()
{
if(front==-1 && rare==-1)
{
cout <<"under flow";
return;
}
else if( front== rare )
{
front=rare=-1;
return;
}
front= (front+1)%5;
}
void display()
{
int i;
if( front <= rare)
{
for(i=front; i<=rare;i++)
cout << q[i]<<" ";
11

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rare;i++)
{
cout << q[i]<< " ";
}
}
}
};
main()
{
int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.push(ch);
break;
case 2: q1.pop();
break;
case 3: q1.display();
break;
case 4: exit(0);
}
}
}
OUTPUT
1.INSERT 2.DELETE
Enter ur choice1
enter element4
1.INSERT 2.DELETE
Enter ur choice1
enter element5
1.INSERT 2.DELETE
Enter ur choice1
enter element3
1.INSERT 2.DELETE
Enter ur choice3
453

3.DISPLAY 4.EXIT
3.DISPLAY 4.EXIT
3.DISPLAY 4.EXIT
3.DISPLAY 4.EXIT

12

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
53
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice4

13

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 6 : Write C++ program to implement the deque (double ended queue) ADT using a doubly
linked list.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
int data;
class node *next;
class node *prev;
};
class dqueue: public node
{
node *head,*tail;
int top1,top2;
public:
dqueue()
{
top1=0;
top2=0;
head=NULL;
tail=NULL;
}
void push(int x)
{
node *temp;
int ch;
if(top1+top2 >=5)
{
cout <<"dqueue overflow";
return ;
}
if( top1+top2 == 0)
{
head = new node;
head->data=x;
head->next=NULL;
head->prev=NULL;
tail=head;
top1++;
}
else
{
cout <<" Add element 1.FIRST 2.LAST\n enter ur choice:";
cin >> ch;
if(ch==1)
{
14

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------top1++;
temp=new node;
temp->data=x;
temp->next=head;
temp->prev=NULL;
head->prev=temp;
head=temp;
}
else
{
top2++;
temp=new node;
temp->data=x;
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
}
}
void pop()
{
int ch;
cout <<"Delete 1.First Node 2.Last Node\n Enter ur choice:";
cin >>ch;
if(top1 + top2 <=0)
{
cout <<"\nDqueue under flow";
return;
}
if(ch==1)
{
head=head->next;
head->prev=NULL;
top1--;
}
else
{
top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display()
{
int ch;
node *temp;
cout <<"display from 1.Staring 2.Ending\n Enter ur choice";
cin >>ch;
if(top1+top2 <=0)
{
15

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------cout <<"under flow";
return ;
}
if (ch==1)
{
temp=head;
while(temp!=NULL)
{
cout << temp->data <<" ";
temp=temp->next;
}
}
else
{
temp=tail;
while( temp!=NULL)
{
cout <<temp->data << " ";
temp=temp->prev;
}
}
}
};
main()
{
dqueue d1;
int ch;
while (1)
{
cout <<"1.INSERT 2.DELETE 3.DISPLAU 4.EXIT\n Enter ur choice:";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter element";
cin >> ch;
d1.push(ch);
break;
case 2: d1.pop();
break;
case 3: d1.display();
break;
case 4: exit(1);
}
}
}

16

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------OUTPUT :
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element4
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element5
Add element 1.FIRST 2.LAST
enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:1
enter element6
Add element 1.FIRST 2.LAST
enter ur choice:2
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
5 4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:2
Delete 1.First Node 2.Last Node
Enter ur choice:1
1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:3
display from 1.Staring 2.Ending
Enter ur choice1
4 6 1.INSERT 2.DELETE 3.DISPLAU 4.EXIT
Enter ur choice:4

17

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 7 : Write a C program to implement Linear Search
#include<stdio.h>
void linsea(int a[],int n,int key);
void main()
{
int a[10],n,i,key;
clrscr();
printf("enter n : ");
scanf("%d",&n);
printf("enter elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search : ");
scanf("%d",&key);
linsea(a,n,key);
getch();
}
void linsea(int a[],int n,int key)
{
int i,flag=1;
for(i=1;i<n;i++)
{
if(a[i]==key)
{
printf("element %d is found at %d location",key,i+1);
flag=0;
break;
}
}
if(flag)
{
printf("search unsuccessful");
}
getch();
}
OUTPUT :
enter n : 5
enter elements : 1 4 5 2 3
enter the element to search : 5
element 5 is found at 3 location

18

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 8 : Write a C program to implement Binary Search
#include<stdio.h>
void binsea(int a[],int n,int key,int low,int high);
void main()
{
int a[10],n,i,key,low,high;
clrscr();
printf("enter n : ");
scanf("%d",&n);
printf("enter elements in sorted order only : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search : ");
scanf("%d",&key);
low=0;
high=n-1;
binsea(a,n,key,low,high);
getch();
}
void binsea(int a[],int n,int key,int low,int high)
{
int i,flag=1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(key<a[mid])
high=mid-1;
else if(key>a[mid])
low=mid+1;
else if(key==a[mid])
{
printf("search success");
printf("%d found at %d location",key,mid+1);
flag=0;
break;
}
}
if(flag)
{
printf("search unsuccessful");
}
}
OUTPUT :
enter n : 5
enter elements in sorted order only : 1 2 3 4 5
enter the element to search : 4
search success
4 found at 4 location
19

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 9 : Write a C program to implement Bubble Sort
#include<stdio.h>
void bubblesort(int a[],int n);
void main()
{
int a[10],n,I;
clrscr();
printf("enter n : ");
scanf("%d",&n);
printf("enter elements in sorted order only : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubblesort(a,n);
getch();
}
void bubblesort(int a[],int n)
{
int i,j,limit,temp;
limit=n-1;
for(i=0;i<limit;i++)
{
for(j=0;j<limit-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the sorted array is :");
for(i=0;i<n;i++)
printf("%2d",a[i]);
}
OUTPUT :
enter n : 5
enter elements in sorted order only : 1 5 3 4 2
the sorted array is : 1 2 3 4 5

20

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 10 : Write a C program to implement Selection Sort
#include<stdio.h>
#include<conio.h>
void selectionsort(int a[],int n);
void main()
{
int a[10],n,i;
clrscr();
printf("enter n : ");
scanf("%d",&n);
printf("enter elements in sorted order only : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selectionsort(a,n);
getch();
}
void selectionsort(int a[],int n)
{
int i,j,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>=a[j])
{
min=j;
}
}
if(min!=j)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("the sorted array is :");
for(i=0;i<n;i++)
printf("%2d",a[i]);
}
OUTPUT :
enter n : 5
enter elements in sorted order only : 1 4 2 5 3
the sorted array is : 1 2 3 4 5

21

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 11 : Write a C program to implement Insertion Sort
#include<stdio.h>
#include<conio.h>
void insertionsort(int a[],int n);
void main()
{
int a[10],n,i;
clrscr();
printf("enter n : ");
scanf("%d",&n);
printf("enter elements in sorted order only : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insertionsort(a,n);
getch();
}
void insertionsort(int a[],int n)
{
int i,j,k;
for(j=1;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0&&k<a[i];i--)
{
a[i+1]=a[i];
}
a[i+1]=k;
printf("\nElement %d inserted in appropriate location : \n",k);
for(i=0;i<n;i++)
printf("%2d",a[i]);
}
printf("\nthe sorted array is : ");
for(i=0;i<n;i++)
printf("%2d",a[i]);
}
OUTPUT :
enter n : 5
enter elements in sorted order only : 3 1 5 2 4
Element 1 inserted in appropriate location :
13524
Element 5 inserted in appropriate location :
13524
Element 2 inserted in appropriate location :
12354
Element 4 inserted in appropriate location :
12345
the sorted array is : 1 2 3 4 5
22

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 12 : Write a C program to implement Quick Sort
#include<stdio.h>
void qsort(int a[],int left,int right);
int n;
main()
{
int a[20],i,l,r;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
l=0;
r=n-1;
qsort(a,l,r);
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void qsort(int b[],int left,int right)
{
int i,j,k,p,temp,finished;
if(right>left)
{
i=left;
j=right;
p=b[left];
printf("\n P,the partioning element is: %d\n",p);
finished=0;
while(!finished)
{
do
{
++i;
}while((b[i]<=p)&&(i<=right));
while((b[j]>=p)&&(j>left))
{
--j;
}
if(j<i)
finished=1;
else
{
printf("\nswapping %d and %d. \n",b[i],b[j]);
temp=b[i];
b[i]=b[j];
b[j]=temp;
for(k=0;k<n;k++)
printf("%5d",b[k]);
23

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------printf("\n");
}
}
printf("\n I is greater than J,");
printf("so b[left] and b[j] are swapped.");
printf("Swapping %d and %d.\n\n",b[left],b[j]);
temp=b[left];
b[left]=b[j];
b[j]=temp;
for(k=0;k<n;k++)
printf("%5d",b[k]);
printf("\n");
printf("\nsub file one is : elements %d to %d.\n",left,j-1);
qsort(b,left,j-1);
printf("\nsub file two is : elements %d to %d.\n",i,right);
qsort(b,i,right);
}
else
{
if(right!=left)
printf("Right (%d) is less than Left(%d).\n",right,left);
else
{
printf("Right (%d) is less than Left(%d).\n",right,left);
printf("subfile has only one element.\n");
}
}
return;
}

24

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------OUTPUT :
enter how many elements do u want to enter: 5
enter the array elements: 3 1 5 2 4
P,the partioning element is: 3
swapping 5 and 2.
3 1 2 5 4
I is greater than J,so b[left] and b[j] are swapped.Swapping 3 and 2.
2 1 3 5 4
sub file one is : elements 0 to 1.
P,the partioning element is: 2
I is greater than J,so b[left] and b[j] are swapped.Swapping 2 and 1.
1 2 3 5 4
sub file one is : elements 0 to 0.
Right (0) is less than Left(0).
subfile has only one element.
sub file two is : elements 2 to 1.
Right (1) is less than Left(2).
sub file two is : elements 3 to 4.
P,the partioning element is: 5
I is greater than J,so b[left] and b[j] are swapped.Swapping 5 and 4.
1 2 3 4 5
sub file one is : elements 3 to 3.
Right (3) is less than Left(3).
subfile has only one element.
sub file two is : elements 5 to 4.
Right (4) is less than Left(5).
final sorted array is: 1 2 3 4 5

25

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 13 : Write a C program to implement Merge Sort on two sorted arrays.
#include<stdio.h>
void merge_sort(int a[],int b[],int m,int n);
main()
{
int a[10],m,n,b[10],p;
static int i;
clrscr();
printf("enter the size of the first array : \n");
scanf("%d",&m);
printf("enter the elements into the array in sorted order only: \n");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("enter the size of the second array : \n");
scanf("%d",&n);
printf("enter the elements into the array in sorted order onlyf : \n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
merge_sort(a,b,m,n);
getch();
}
void merge_sort(int a[],int b[],int m,int n)
{
int c[25],p;
static int i,j,k;
i=0;
j=0;
while(i<m && j<n)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else if(a[i]>b[j])
{
c[k]=b[j];
k++;
j++;
}
else
{
c[k]=a[i];
i++;
k++;
}
}
if(i<m)
{
for(p=i;p<m;p++)
26

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------{
c[k]=a[i];
k++;
i++;
}
}
else if(j<n)
{
for(p=j;p<n;p++)
{
c[k]=b[j];
k++;
j++;
}
}
printf("after sorting the elements are\n");
for(i=0;i<k;i++)
printf("%d ",c[i]);
}
OUTPUT :
enter the size of the first array :
5
enter the elements into the array in sorted order only:
13579
enter the size of the second array :
5
enter the elements into the array in sorted order only :
2 4 6 8 10
after sorting the elements are
1 2 3 4 5 6 7 8 9 10

27

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 14 : Write a C program to implement Heap Sort
#include<stdio.h>
main()
{
int a[25],n,i;
clrscr();
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter %d elements into the array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
heap(a,i,a[i]);
}
heapsort(a,n);
printf("sorted elements are\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
heap(int a[],int pos,int k)
{
int f;
f=(pos-1)/2;
while(pos>0 && k>a[f])
{
a[pos]=a[f];
pos=f;
f=(pos-1)/2;
}
a[pos]=k;
}
heapsort(int a[],int n)
{
int k,key,i,j;
for(i=n-1;i>0;i--)
{
key=a[0];
a[0]=a[i];
a[i]=key;
for(j=0;j<i;j++)
{
heap(a,j,a[j]);
}
}
}
OUTPUT :
enter the size of the array : 5
enter 5 elements into the array 3 1 5 2 4
sorted elements are 1 2 3 4 5
28

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 15 : Write a C++ program to implement BFS for a given Graph.
#include<stdio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
main()
{
int m;
printf("enterno of vertices");
scanf("%d",&n);
printf("ente no of edges");
scanf("%d",&m);
printf("\nEDGES \n");
for(k=1;k<=m;k++)
{
scanf("%d",&i);
scanf("%d",&j);
cost[i][j]=1;
}
printf("enter initial vertex");
scanf("%d",&v);
printf("Visitied vertices\n");
printf(" %d ",v);
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
printf(" %d ",v);
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
Enter no.of vertices:4
Enter no.of edges:5
EDGES:
1
2
1
3
2
3
3
4
2
4
Enter initial vertex:1
Visited vertices:1
2

29

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 16 : Write a C program to implement DFS for a given Graph.
#include<stdlib.h>
#include<stdio.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
printf("enterno of vertices");
scanf("%d",&n);
printf("ente no of edges");
scanf("%d",&m);
printf("\nEDGES \n");
for(k=1;k<=m;k++)
{
scanf("%d",&i);
scanf("%d",&j);
cost[i][j]=1;
}
printf("enter initial vertex");
scanf("%d",&v);
printf("Visited vertices");
printf(" %d ",v );
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;top++;
}
v=stk[--top];
printf(" %d ",v );
k++;
visit[v]=0; visited[v]=1;
}
}
OUTPUT
Enter no.of vertices:4
Enter no.of edges:5
EDGES:
1
2
1
3
2
3
3
4
2
4
Enter initial vertex:1
Visited vertices:1
2

3
30

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 17 : Write C program to implement operations of Binary Search Tree.
a)Insert an element into a binary search tree
b)Delete an element from a binary search tree
c)Search for a key element in a binary search tree
#include<stdio.h>
#include<stdlib.h>
int ch;
struct node
{
int data;
struct node *left,*right;
};typedef struct node bst;
bst *root;
struct node *insert(bst *r,int val)
{
if(r==NULL)
{
r=(bst*)malloc(sizeof(bst));
r->data=val;
r->left=NULL;
r->right=NULL;
}
else
{
if(r->data<val)
r->right=insert(r->right,val);
else
r->left=insert(r->left,val);
}
return(r);
}
struct node *delet(bst *r,int val)
{
bst *d,*x,*p,*i;
int f=0;
x=r;
p=NULL;
while(x!=NULL)
{
if(x->data==val)
{
d=x;
f=1;
break;
}
else
{
if(val<x->data)
31

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------{
p=x;
x=x->left;
}
else
{
p=x;
x=x->right;
}
}
}
if(f==0)
printf("\nelement not found");
else
{
if(d->left!=NULL&&d->right!=NULL)
{
p=d;
i=d->left;
while(i->right!=NULL)
{
p=i;
i=i->right;
}
d->data=i->data;
d=i;
}
if(d->left!=NULL&&d->right==NULL)
{
if(p==NULL)
r=d->left;
else
if(d==p->left)
p->left=d->left;
else
p->right=d->right;
}
if(d->left==NULL&&d->right!=NULL)
{
if(p==NULL)
r=d->right;
else
if(d==p->left)
p->left=d->right;
else
p->right=d->right;
}
if(d->left==NULL&&d->right==NULL)
{
if(p==NULL)
r=NULL;
else
32

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------if(d==p->left)
p->left=NULL;
else
p->right=NULL;
}
printf("\n element is deleted\n");
return r;
}
return 0;
}
int search(bst *r,int val)
{
if(r==NULL||r==0)
printf("element not found\n");
else
{
if(val<r->data)
search(r->left,val);
else
if(val>r->data)
search(r->right,val);
else
printf("element found");
}
}
int main()
{
int element;
root=NULL;
printf("\nMENU\n 1.insertion 2.deletion 3.search 4.quit");
do
{
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter the element you want to insert");
scanf("%d",&element);
root=insert(root,element);
break;
case 2: printf("\nenter the element tobe deleted");
scanf("%d",&element);
root=delet(root,element);
break;
case 3: printf("\neneter the element to be searched");
scanf("%d",&element);
search(root,element);
break;
case 4: exit(1);
}
}while(ch!=4);
}
33

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------OUTPUT
Menu
1.Insertion
2.Deletion
3.Search
4.Quit
Enter your choice 2
Enter the element to delete:5
Element not found

//Deletion

Enter your choice 1


Enter element to insert:5

//Insertion

Enter your choice 3


Enter the element search:5
Element found

//Search

Enter your choice 2


Enter the element to delete:5
Element is deleted

//Deletion

Enter your choice 3


Enter the element search:5
Element not found

//Search

Enter your choice 4

//Quit

34

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 18 : Write a C Program that uses non-recursive functions to traverse a binary tree in Pre-order,
In-order and Post-order for given expressions.
#include<stdio.h>
#include<stdlib.h>
#define null 0
struct btnode
{
struct btnode *leftc,*rightc;
int data;
};
void insert(struct btnode **, int);
void inorder(struct btnode *);
void preorder(struct btnode *);
void postorder(struct btnode *);
main()
{
struct btnode *bt;
int i=1,req,num;
bt=null;
printf("Specify no.of nodes in the tree:");
scanf("%d",&req);
while(i<=req)
{
printf("Enter node%d: ",i);
scanf("%d",&num);
insert(&bt,num);
i++;
}
printf("The preorder traversal is:");
preorder(bt);
printf("\nThe inorder traversal is:");
inorder(bt);
printf("\nThe postorder traversal is:");
postorder(bt);
printf("\n");
}
void insert(struct btnode **sr, int num)
{
if(*sr==null)
{
(*sr)=malloc(sizeof(struct btnode));
(*sr)->leftc=null;
(*sr)->data=num;
(*sr)->rightc=null;
return;
}
else
35

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------{
if(num<(*sr)->data)
insert(&((*sr)->leftc),num);
else
insert(&((*sr)->rightc),num);
}
return;
}
void postorder(struct btnode *currentnode)
{
if(currentnode!=NULL)
{
postorder(currentnode->leftc);
postorder(currentnode->rightc);
printf("%d\t",currentnode->data);
}
}
void preorder(struct btnode * currentnode)
{
if(currentnode!=NULL)
{
printf("%d\t", currentnode ->data);
preorder(currentnode ->leftc);
preorder(currentnode ->rightc);
}
}
void inorder(struct btnode * currentnode)
{
if(t!=NULL)
{
inorder(currentnode ->leftc);
printf("%d\t", currentnode ->data);
inorder(currentnode ->rightc);
}
}
OUTPUT:
Specify no.of nodes in the tree:3
Enter node1: 2
Enter node 2:1
Enter node 3:3
The preorder traversal is: 2
The inorder traversal is: 1
The postorder traversal is:1

1
2
3

3
3
2

36

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 19 : Write a C Program to implement all the functions of a Dictionary(ADT) using Hashing
#include<stdlib.h>
#include<stdio.h>
void insert(int);
void search(int);
void delete_ele(int);
int index=-1;
typedef struct list
{
int data;
struct list *next;
}node_type;
node_type *ptr[10],*root[10],*temp[10];
void insert(int key)
{
index=(key%10);
ptr[index]=(node_type*)malloc(sizeof(node_type));
ptr[index]->data=key;
if(root[index]==NULL)
{
root[index]=ptr[index];
root[index]->next=NULL;
temp[index]=ptr[index];
}
else
{
temp[index]=root[index];
while(temp[index]->next!=NULL)
temp[index]=temp[index]->next;
temp[index]->next=ptr[index];
}
}
void search(int key)
{
int flag=0;
index=(key%10);
temp[index]=root[index];
while(temp[index]!=NULL)
{
if(temp[index]->data==key)
{
printf("\nSearched element is found!!");
flag=1;
break;
}

37

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------else
temp[index]=temp[index]->next;
}
if (flag==0)
printf("\nsearch key not found.......");
}
void delete_ele(int key)
{
index=(key%10);
temp[index]=root[index];
while(temp[index]->data!=key && temp[index]!=NULL)
{
ptr[index]=temp[index];
temp[index]=temp[index]->next;
}
ptr[index]->next=temp[index]->next;
printf("\n %d has been deleted.", temp[index]->data);
temp[index]->data=-1;
temp[index]=NULL;
free(temp[index]);
}
main()
{
int val,ch,n,num,i;
for(i=0;i<10;i++)
{
root[i]=NULL;
ptr[i]=NULL;
temp[i]=NULL;
}
do
{
printf("\nMENU:\n1.Create");
printf("\n2.Search for a value\n3.Delete an value");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the number of elements to be inserted:");
scanf("%d",&n);
printf("\nEnter the elements to be inserted:");
for(i=0;i<n;i++)
{
scanf("%d",&num);
insert(num);
}
break;

38

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------case 2:printf("\nEnter the element to be searched:");
scanf("%d",&n);
search(n);
break;
case 3:printf("\nEnter the element to be deleted:");
scanf("%d",&n);
delete_ele(n);
break;
default:printf("\nInvalid choice....");
}
}while(ch!=4);
}
OUTPUT
MENU:
1.Create
2.Search
3.Delete
4.Exit
Enter your choice:1
Enter no.of elements to insert:3
Enter elements:5 6 8

//Create

Enter your choice:2


Enter element to serach:6
Searched element is found

//Search

Enter your choice:2


Enter element to serach:7
Searched element not found

//Search

Enter your choice:3


Enter element to delete:8
8 has been deleted

//Delete

Enter your choice:3


Enter element to delete:8
Deletion is not possible

//Delete

Enter your choice:4


Ended the Execution

//Exit

39

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 20 : Write a C program to implementation of the operations of AVLTrees
#include <stdio.h>
#include <stdlib.h>
struct AvlNode;
typedef struct AvlNode *AvlTree;
AvlTree MakeEmpty( AvlTree T );
AvlTree Find( int X, AvlTree T );
AvlTree Insert( int X, AvlTree T );
struct AvlNode
{
int Element;
AvlTree Left, Right;
int Height;
};
AvlTree MakeEmpty( AvlTree T )
{
if( T != NULL )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NULL;
}
AvlTree Find( int X, AvlTree T )
{
if( T == NULL )
return NULL;
if( X < T->Element )
return Find( X, T->Left );
else
if( X > T->Element )
return Find( X, T->Right );
else
return T;
}
int Height( AvlTree P )
{
if( P == NULL )
return -1;
else
return P->Height;
}
int Max( int Lhs, int Rhs )
{
return Lhs > Rhs ? Lhs : Rhs;
}
40

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------AvlTree SingleRotateWithLeft( AvlTree K2 )
{
AvlTree K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1;
K1->Height = Max( Height( K1->Left ), K2->Height ) + 1;
return K1;
}
AvlTree SingleRotateWithRight( AvlTree K1 )
{
AvlTree K2;
K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
K1->Height = Max( Height( K1->Left ), Height( K1->Right ) ) + 1;
K2->Height = Max( Height( K2->Right ), K1->Height ) + 1;
return K2;
}
AvlTree DoubleRotateWithLeft( AvlTree K3 )
{
K3->Left = SingleRotateWithRight( K3->Left );
return SingleRotateWithLeft( K3 );
}
AvlTree DoubleRotateWithRight( AvlTree K1 )
{
K1->Right = SingleRotateWithLeft( K1->Right );
return SingleRotateWithRight( K1 );
}
AvlTree Insert( int X, AvlTree T )
{
if( T == NULL )
{
T = malloc( sizeof( struct AvlNode ) );
if( T == NULL )
printf( "Out of space!!!" );
else
{
T->Element = X; T->Height = 0;
T->Left = T->Right = NULL;
}
}
else if( X < T->Element )
{
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T );
41

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------else
T = DoubleRotateWithLeft( T );
}
else if( X > T->Element )
{
T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T= SingleRotateWithRight( T );
else
T = DoubleRotateWithRight( T );
}
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
}
void inorder(AvlTree r)
{
if (r)
{
inorder(r->Left);
printf("%d-->",r->Element);
inorder(r->Right);
}
}
main()
{
int ch,tmp,f;
AvlTree T=MakeEmpty(NULL),P;
printf("\n1.Insert a node\n2.Search for a node\n3.Exit");
while(1)
{
printf("\nEnter u r choice");
scanf("%d",&ch);
switch(ch)
{
case 1:

printf("\n Enter Number to be inserted : ");


scanf("%d",&tmp);
T=Insert(tmp,T);
printf(" After Insertion tree is: ");
inorder(T);
break;

case 2 :
if (!T)
printf("\nTree Empty ");
else
{
42

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------printf("\nEnter data to search :");
scanf("%d",&tmp);
if(P=Find(tmp,T))
printf("\nElement found");
else
printf("nElement not found");
}
break;
case 3 :

exit(0);

}
}
}
OUTPUT:
AVL Tree Operations
1.Insert a node 2.Search for a node 3.Exit
Enter u r choice1
Enter Number to be inserted : 1
After Insertion tree is: 1-->

\\Insert

Enter u r choice1
Enter Number to be inserted : 54
After Insertion tree is: 1-->54-->

\\Insert

Enter u r choice1
Enter Number to be inserted : 23
After Insertion tree is: 1-->23-->54-->

\\Insert

Enter u r choice1
Enter Number to be inserted : 34
After Insertion tree is: 1-->23-->34-->54-->

\\Insert

Enter u r choice1
Enter Number to be inserted : 56
After Insertion tree is: 1-->23-->34-->54-->56-->

\\Insert

Enter u r choice2
Enter data to search :34
Element found

\\Search

Enter u r choice2
Enter data to search :50
Element not found

\\Search

Enter u r choice3

\\Exit

43

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 21 : Write a C Program to verify whether the given string is Palindrome or not using Stack
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
typedef struct
{
char stackary[MAX];
int count,max,top;
} stack;
stack *createstack(int max);
int pushstack(stack *stack,char);
void dispstack(stack *);
stack * createstack(int max)
{
stack *s;
s=(stack *)malloc(sizeof(stack));
if(!s)
return NULL;
else
{
s->count=0;
s->top=-1;
s->max=max;
return s;
}
}
int pushstack(stack * s,char datain)
{
if(s->count==s->max)
return -1;
else
{
s->count++;
s->top++;
s->stackary[s->top]=datain;
return 1;
}
}
void dispstack(stack *s)
{
int i;
printf("\n stack is\n");
for(i=s->count-1;i>=0;i--)
printf("%c",s->stackary[i]);
}

44

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------main()
{
int ch,i,j,n,length,flag=1;
char item;
stack *s=createstack(20);
printf("how many elements do you want to insert : ");
scanf("%d",&ch);
printf("insert elements : ");
for(i=0;i<ch;i++)
{
scanf(" %c",&item);
n=pushstack(s,item);
if(!n)
printf("\nstack overflow\n");
}
dispstack(s);
length=s->top;
j=length;
for(i=0;i<=(length/2);)
{
if(s->stackary[i]==s->stackary[j])
{
i++;
j--;
}
else
{
flag=0;
break;
}
}
if(flag==0)
printf("\nthe string is not palindrome");
else
printf("\nthe string is palindrome");
}
OUTPUT:
How many elements do you want to insert :3
Insert elements :a
s
a
Stack is: asa
The String is Palindrome
How many elements do you want to insert :3
Insert elements :a
s
b
Stack is: asb
The String is not Palindrome
45

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------Prog 22 : Write a C Program to verify whether the given string is Palindrome or not using Queue
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
typedef struct
{
char qary[MAX];
int count,max,front, rear;
} queue;
queue *createq(int max);
int insertq(queue *queue,char);
void dispqueue(queue *);
queue * createq(int max)
{
queue *q;
q=(queue *)malloc(sizeof(queue));
if(!q)
return NULL;
else
{
q->count=0;
q->front=0;
q->rear=0;
q->max=max;
return q;
}
}
int insertq(queue * q,char datain)
{
if(q->count==q->max)
return -1;
else
{
q->count++;
q->rear++;
q->qary[q->rear]=datain;
return 1;
}
}
void dispq(queue *q)
{
int i;
printf("\n Queue is : \t");
for(i=1;i<=q->count;i++)
printf("%c",q->qary[i]);
}

46

Sasi Institute Of Technology & Engineering


Data Structures & Algorithms Lab
-----------------------------------------------------------------------------------------------------------------------------main()
{
int ch,i,j,n,length,flag=1;
char item;
queue *q=createq(20);
printf("how many elements do you want to insert : ");
scanf("%d",&ch);
printf("\ninsert elements : ");
for(i=0;i<ch;i++)
{
scanf(" %c",&item);
n=insertq(q,item);
if(!n)
printf("\nqueue overflow\n");
}
dispq(q);
length=q->rear;
j=length;
for(i=1;i<=(length/2);)
{
if(q->qary[i]==q->qary[j])
{
i++;
j--;
}
else
{
flag=0;
break;
}
}
if(flag==0)
printf("\nthe string is not palindrome\n");
else
printf("\nthe string is palindrome\n");
}
OUTPUT:
How many elements do you want to insert :3
Insert elements :d
a
d
Queue is: dad
The String is Palindrome
How many elements do you want to insert :3
Insert elements :d
a
b
Queue is: dab
The String is not Palindrome
47

You might also like