Data structure lab programs
Part A
1. programe to find GCD (hcf) using recursive function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1,n2,res;
printf("enter a number ");
scanf("%d%d",&n1,&n2);
res=hcf(n1,n2);
printf("hcf =%d ",res);
}
int hcf(int n1, int n2)
{
if(n1==0)
return n2;
if(n2==0)
return n1;
else
return hcf(n2,n1%n2);
}
Output:
2. programe to generate n Fibonacci series using recursive function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
printf("enter a number ");
scanf("%d",&n); for(i=0;i<n;i+
+) printf("%d\n",fib(i));
}
int fib(int i)
{
if(i==0)
return 0;
if(i==1)
return 1;
else
return (fib(i-1)+fib(i-2));
}
Output:
3. program to display pascal triangle using binomial function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,res;
printf("enter the number of rows");
scanf("%d",&n);
for(j=0;j<n;j++)
{
for(i=0;i<=j;i++)
{
res=fact(j)/(fact(j-i)*fact(i));
printf("%d\t",res);
}
printf("\n");
}
return 0;
}
int fact(int m)
{
if (m==0)
return 1;
else
return m*fact(m-1);
}
Output:
4. Program to implement Tower of
hannoi. #include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("enter the number of disk\n");
scanf("%d",&n);
tower(n,'A','C','B');
}
void tower(int n,int s, int d, int t)
{
if(n==1)
{
printf("move disk %d from %c to %c\n", n,s,d);
return;
}
else
tower(n-1,s,t,d);
printf("move disk %d from %c to %c\n",n,s,d);
tower(n-1,t,d,s);
}
Output:
5. Program to implement dynamic array and find largest and smallest element
of the array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size, *arr,max,min,i;
printf("enter the array size\n");
scanf("%d",&size);
arr=(int*) calloc(size,sizeof(int));
printf("\narray elemnts\n");
for(i=0;i<size;i++)
scanf("%d",arr+i);
max=arr[0];
min=arr[0];
for(i=0;i<size;i++)
{
if(max<arr[i])
{
max=arr[i];
}
}
for(i=0;i<size;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
printf("largest element is %d \n",max);
printf("smallest element is %d \n",min);
Output:
6. Program to read the names of cities and arrange them alphabetically
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str[5][20],t[20];
int i,j;
printf("enter any 5 city names\n");
for(i=0;i<5;i++)
scanf("%s",&str[i]);
for(i=1;i<5;i++)
{
for(j=0;j<5;j++)
{
if(strcmp(str[j-1],str[j])>0)
{
strcpy(t,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],t);
}
}
}
printf("names in alphabetical order \n");
for(i=0;i<5;i++)
printf("%s\n",str[i]);
}
Output:
7. program to search an element using linear search technique
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ele,i,a[100],size;
printf("enter the size of array\n");
scanf("%d",&size);
printf("enter the elements\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("enter the search element\n");
scanf("%d",&ele); for(i=0;i<size;i+
+)
if(a[i]==ele)
break;
if(i<size)
printf("element found at position %d",i+1);
else
printf("element not found");
}
Output:
PART - B
1. Program to search an element using recursive binary search technique
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ele,i,a[100],size,res;
printf("enter the size of array\n");
scanf("%d",&size);
printf("enter the elements\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("enter the search element\n");
scanf("%d",&ele);
res=bs(a,ele,0,size-1);
if(res==-1)
printf("element not found");
else
printf("element is found at index %d",res);
}
int bs(int a[], int ele, int low,int high)
{
int mid;
if(high>=low)
{
mid=low+(high-low)/2;
if(a[mid]== ele)
return mid;
else if(a[mid]>ele)
return bs(a,ele,low,mid-1);
else
return bs(a,ele,mid+1,high);
}
return -1;
}
Output:
2. Program to sort the given list using bubble sort technique
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ele,i,a[100],n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a,n);
printf("elements after sorting \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int bubble(int a[], int n)
{
int temp, i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
Output:
3. Program to sort the given list using selection sort technique
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ele,i,a[100],n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a,n);
printf("elements after sorting \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int selection(int a[], int n)
{
int small,temp,i,j;
for(i=0;i<n-1;i++)
{
small=i;
for(j=i+1;j<n;j++)
{
if(a[small]>a[j])
{
small=j;
}
if(small!=i)
{
temp=a[i];
a[i]=a[small];
a[small]=temp;
}
}
}
}
Output:
4. Program to sort the given list using insertion sort technique.
#include <stdio.h>
#include <stdlib.h>
int main()
int a[100],i,j,n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insort(a,n);
printf("array elements after sorting\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
void insort(int a[],int n)
int i,j,temp;
for(i=0;i<n;i++)
temp=a[i];
for(j=i;((j>0)&& (a[j-1]>temp));j--)
a[j]=a[j-1];
a[j]=temp;
Output:
5. Program to sort elements using Quick sort.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[20],i,n;
printf("enter the size of array \n");
scanf("%d",&n);
printf("enter the elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf(" \n array after sorting is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int partition(int a[] ,int low, int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j = high;
while(1)
{
while(i<high && key >=a[i])
{
i++;
}
while(key < a[j])
{
j--;
}
if(i<j)
{temp = a[i] ;
a[i]=a[j] ;
a[j]=temp;
}
else
{
temp= a[low];
a[low]=a[j] ;
a[j] = temp;
return j ;
}
}
}
void quicksort(int a[ ] ,int low,int high)
{
int j;
if(low < high)
{
j= partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a, j+1,high);
}
}
Output:
6. Program to implement stack
#include <stdio.h>
#include <stdlib.h>
#define max 5
int top=-1;
int stack[max];
int main()
{
int choice;
while(1)
{
printf("\n1. push \n 2. pop \n 3. display \n 4.exit\n ");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void push()
{
int item;
if(top==max-1)
{
printf("stack overflow\n");
}
else
{
printf("enter the item to be inserted\n");
scanf("%d",&item);
top++;
stack[top]=item;
}
}
void pop()
{
if(top==-1)
{
printf("stack underflow\n");
}
else
{
printf("\n poped item is
%d",stack[top]); top--;
}
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
}
else
{
printf("\nelements of stack are\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
Output:
1. push
2. pop
3. display
4.exit
enter your choice
1
enter the item to be inserted
10
1. push
2. pop
3. display
4.exit
enter your choice
1
enter the item to be inserted
20
1. push
2. pop
3. display
4.exit
enter your choice
1
enter the item to be inserted
30
1. push
2. pop
3. display
4.exit
enter your choice
1
enter the item to be inserted
40
1. push
2. pop
3. display
4.exit
enter your choice
1
enter the item to be inserted
50
1. push
2. pop
3. display
4.exit
enter your choice
1
stack overflow
1. push
2. pop
3. display
4.exit
enter your choice
3
elements of stack are
50
40
30
20
10
1. push
2. pop
3. display
4.exit
enter your choice
2
poped item is 50
1. push
2. pop
3. display
4.exit
enter your choice
2
poped item is 40
1. push
2. pop
3. display
4.exit
enter your choice
2
poped item is 30
1. push
2. pop
3. display
4.exit
enter your choice
2
poped item is 20
1. push
2. pop
3. display
4.exit
enter your choice
2
poped item is 10
1. push
2. pop
3. display
4.exit
enter your choice
2
stack underflow
1. push
2. pop
3. display
4.exit
enter your choice
3
stack is empty
1. push
2. pop
3. display
4.exit
enter your choice
4
Process returned 0 (0x0) execution time : 39.498 s
7..Program to implement simple queue.
#include <stdio.h>
#include <stdlib.h>
#define max 5
int queue[max];
int front=-1;
int rear=-1;
int main()
{
int choice;
while(1)
{
printf("\n1. enqueue \n 2. dequeue \n 3. display \n 4.exit\n ");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void enqueue()
{
int item;
if(rear == max-1)
{
printf("queue is overflow");
}
else
{
if(front==-1)
front=0;
printf("\n enter item to be inserted \n");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
}
}
void dequeue()
{
if(front ==-1 || front >rear)
{
printf("queue is underflow\n");
}
else
{
printf("element deleted from queue is %d \n",queue[front]);
front=front+1;
}
}
void display()
{
int i;
if(front ==-1 || front >rear)
{
printf("queue is empty\n");
}
else
{
printf("elements in queue are\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}
Output:
4. enqueue
5. dequeue
6. display
4.exit
enter your choice
1
enter item to be inserted
10
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
enter item to be inserted
20
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
enter item to be inserted
30
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
enter item to be inserted
40
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
enter item to be inserted
50
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
queue is overflow
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
3
elements in queue are
10
20
30
40
50
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
element deleted from queue is 10
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
element deleted from queue is 20
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
element deleted from queue is 30
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
element deleted from queue is 40
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
element deleted from queue is 50
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
2
queue is underflow
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
3
queue is empty
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
4
Process returned 0 (0x0) execution time : 39.194 s
Press any key to continue.