Data Structure Lab Programs
Data Structure Lab Programs
Part A
int fib(int i)
{
if(i==0)
return 0;
if(i==1)
return 1;
else
return (fib(i-1)+fib(i-2));
}
Output:
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:
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:
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:
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 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:
#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]);
}
#include <stdlib.h>
int main()
int a[100],i,j,n;
scanf("%d",&n);
scanf("%d",&a[i]);
insort(a,n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
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]);
}
Output:
#include <stdio.h>
#include <stdlib.h>
#define max 5
int top=-1;
int stack[max];
int main()
{
int choice;
while(1)
{
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
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
#include <stdio.h>
#include <stdlib.h>
#define max 5
int queue[max];
int front=-1;
int rear=-1;
int main()
{
int choice;
while(1)
{
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
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
1. enqueue
2. dequeue
3. display
4.exit
enter your choice
1
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