DATA STRUCTURE PRACTICALS
DATA STRUCTURE PRACTICALS
}
printf("\nEnter elements of second Matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
printf("\nSum of Matrix A and B is %d \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
-----------------------------------------------------------
8. Write a program to arrange elements in ascending order using BUBBLE SORT.
// Write a program to arrange elements in ascending order using BUBBLE SORT
#include<stdio.h>
#include<conio.h>
int main()
{
void bsort(int a[],int);
int a[10],i,n;
printf("enter n ");
scanf("%d",&n);
printf("enter array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bsort(a,n);
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
return 0;
}
void bsort(int a[], int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j+1]<a[j])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
------------------------------------------------------
9. Write a program to find factorial using Recursion.
// Write a program to find factorial using Recursion
#include<stdio.h>
int main()
{
int n,f;
int fact(int);
printf("Enter n : ");
scanf("%d",&n);
f=fact(n);
printf("\nFactorial of %d is %d ",n,f);
return 0;
}
int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
----------------------------------------------------
10. Write a program to generate n terms of Fibonacci series using Recursion.
// Write a program to generate n terms
// of Fibonacci series using Recursion
#include<stdio.h>
int main()
{
int i,n,f;
int fib(int);
printf("Enter n : ");
scanf("%d",&n);
printf("Fibonacci series is :\n");
for(i=0;i<n;i++)
{
f=fib(i);
printf("\t%d",f);
}
return 0;
}
int fib(int n)
{
int f;
if(n==0 | n==1)
f=n;
else
f=fib(n-1)+fib(n-2);
return f;
}
------------------------------------------------------------
11. Write a program to find solution TOWER OF HANOI problem using recursion.
//TOWER OF HANOI USING RECURSION
#include<stdio.h>
void hanoi(int n,char beg,char aux,char end);
void move();
int m=0;
int main()
{
int n;
char beg='A',aux='B',end='C';
printf("\nEnter number of disks: ");
scanf("%d",&n);
hanoi(n,beg,aux,end);
move();
return 0;
}
void hanoi(int n,char beg,char aux,char end)
{
if(n!=0)
{
hanoi(n-1,beg,end,aux);
printf("\nMove Disk %d from %c => %c\n",n,beg,end);
hanoi(n-1,aux,beg,end);
m++;
}
}
void move()
{
printf("\n No. of moves = %d ",m);
}
---------------------------------------------------------
12. Write a program for Static implementation of Stack.
//STATIC IMPLEMENTATION OF stack
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
void push();
void pop();
void traverse();
int s[MAXSIZE];
int top=-1; char ch;
int main()
{
int choice;
break;
case 3:
traverse();
break;
default :
printf("\ninvalid choice\n");
break;
}
printf("\nDo you want to continue y/n" );
ch=getch();
}while(ch=='y' || ch=='Y');
return 0;
}
void push()
{
int x;
if(top==MAXSIZE-1)
{
printf("stack full");
getch();
}
else
{
printf("enter x");
scanf("%d",&x);
top=top+1;
s[top]=x;
}
}
void pop()
{
int x;
if(top==-1)
{
printf("stack empty");
getch();
}
else
{
x=s[top];
printf("deleted value %d ",x);
top=top-1;
}
void traverse()
{
int i;
if(top==-1)
{
printf("empty stack");
getch();
}
else
{
for(i=top;i>=0;i--)
{
printf("%d\n",s[i]);
}
}
}
---------------------------------------------------
13. Write a program for Dynamic implementation of Stack.
//Dynamic Implementation of Stack.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int n;
struct node *next;
}*top=NULL,*t=NULL,*c=NULL;
void push();
void pop();
void traverse();
char ch;
void main()
{
int choice;
break;
case 3:
traverse();
break;
default :
printf("invalid choice");
break;
}
printf("\nDo you want to continue y/n" );
ch=getch();
}while(ch=='y' || ch=='Y');
}
void push()
{
t=(struct node*)malloc(sizeof(struct node));
}
void traverse()
{
if(top==NULL)
printf("empty stack");
else
{
t=top;
printf("elements are \n");
while(t!=NULL)
{
printf("\n%d",t->n);
t=t->next;
}
}
}
------------------------------------------------
14. Write a program for Static implementation of Queue DS.
//STATIC IMPLEMENTATION OF Simple queue
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
void insertq();
void deleteq();
void traverseq();
int q[MAXSIZE];
int f=-1,r=-1; char ch;
void main()
{
int choice,x;
break;
case 3:
traverseq();
break;
default :
printf("invalid choice");
break;
}
printf("\nDo you want to continue y/n" );
ch=getch();
}while(ch=='y' || ch=='Y');
return 0;
}
void insertq()
{
int x;
if(r==MAXSIZE-1)
{
printf("queue full");
getch();
}
else
{
printf("enter x");
scanf("%d",&x);
r=r+1;
q[r]=x;
if(f==-1)
f=0;
}
}
void deleteq()
{
int x;
if(f==-1)
{
printf("queue empty");
getch();
}
else
{
x=q[f];
printf("deleted value %d ",x);
if(f==r)
{f=-1;r=-1;}
else
f=f+1;
}
void traverseq()
{
int i;
if(f==-1)
{
printf("empty queue");
getch();
}
else
{
for(i=f;i<=r;i++)
{
printf("%d ",q[i]);
}
}
}
------------------------------------------------
do
{
printf("\nenter choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: qinsert();
break;
case 2: qdelete();
break;
case 3: qtraverse();
break;
default: printf("\nInvalid choice");
break;
}
printf("\nDo u want to continue ");
ch=getch();
}while(ch=='y' || ch=='Y');
return 0;
}
void qinsert()
{
void qdelete()
{
if(f==NULL)
printf("Queue is empty");
else
{
c=f;
printf("deleted value is %d ",c->n);
if(f==r)
{
f=NULL;
r=NULL;
}
else
{
f=f->next;
}
free(c);
}
}
void qtraverse()
{
if(f==NULL)
printf("empty queue");
else
{
c=f;
printf("\n Queue Status is\n");
while(c!=NULL)
{
printf("%d->",c->n);
c=c->next;
}
}
}
--------------------------------------------------------
for(i=f;i<=r;i++)
printf("%d ",q[i]);
if(f>r)
{
for(i=f;i<=MAXSIZE-1;i++)
printf("%d ",q[i]);
for(i=0;i<=r;i++)
printf("%d ",q[i]);
}
}
}
-----------------------------------------------------------------
17. Write a program to arrange elements in ascending order using Insertion SORT.
// Write a program to arrange elements in ascending order using INSERTION SORT
#include<stdio.h>
#include<conio.h>
int main()
{
void isort(int a[],int);
int a[10],i,n;
printf("enter n ");
scanf("%d",&n);
printf("enter array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
isort(a,n);
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
return 0;
}
void isort(int a[], int n)
{
int i,j,t,k;
for(i=1;i<n;i++)
{
t=a[i];
k=i-1;
while(t<a[k] && k>=0)
{
a[k+1]=a[k];
k--;
}
a[k+1]=t;
}
}
---------------------------------------------------
18. Write a program to arrange elements in ascending order using Selection SORT.
// Write a program to arrange elements in ascending order using SELECTION SORT
#include<stdio.h>
#include<conio.h>
int main()
{
void ssort(int a[],int);
int a[10],i,n;
printf("enter n ");
scanf("%d",&n);
printf("enter array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ssort(a,n);
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
return 0;
}
void ssort(int a[], int n)
{
int i,j,t,k;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
-------------------------------------------------------
19. Write a program to arrange elements in ascending order using Quick SORT.
// Write a program to arrange elements in ascending order using QUICK SORT
#include<stdio.h>
#include<conio.h>
int main()
{
void qsort(int a[],int,int);
int a[10],i,n;
printf("enter n ");
scanf("%d",&n);
printf("enter array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
qsort(a,1,n);
printf("sorted array is\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
return 0;
}
void qsort(int a[], int low, int high)
{
int key,i,j,t;
if(low<high)
{
key=low;
i=low;
j=high;
while(i<j)
{
while(a[i]<a[key]&& i<high)
i++;
while(a[j]>a[key])
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[key];
a[key]=a[j];
a[j]=t;
qsort(a,low,j-1);
qsort(a,j+1,high);
}
}
------------------------------------------------
20. Write a program to arrange elements in ascending order using Merge SORT.
// Write a program to arrange elements in ascending order using MERGE SORT
#include<stdio.h>
#include<conio.h>
void msort(int a[],int,int);
void merge(int a[],int,int,int);
int main()
{
int a[10],i,n,low,high;
printf("enter n ");
scanf("%d",&n);
printf("enter array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
low=1; high=n;
msort(a,low,high);
printf("sorted array is\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
return 0;
}
void msort(int a[], int low, int high)
{
int mid;
if(low != high)
{
mid=(low+high)/2;
msort(a,low,mid);
msort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[], int low, int mid, int high)
{
int h,i,j,b[10],k,p;
i=low; k=low; j=mid+1;
while((i<=mid) && (j<=high))
{
if(a[i] <= a[j])
{
b[k]=a[i];
i=i+1;
k=k+1;
}
else{
b[k]=a[j];
j=j+1;
k=k+1;
}
}
if(i>mid)
{
for(p=j;p<=high;p++)
{
b[k]=a[p];
k=k+1;
}
}
if(j>high)
{
for(p=i;p<=mid;p++)
{
b[k]=a[p];
k=k+1;
}
}
for(i=low;i<=high;i++)
a[i]=b[i];
}
-----------------------------------------------------------