Practical 10 To 20
Practical 10 To 20
SLIPNO:-10
Q1.ImplementaBinarysearchtree(BST)library(btree.h)withoperations–
create,insert,inorder.
Writeamenudrivenprogramthatperformstheaboveoperati
ons.[15Marks] ANSWER:-
***************//btree.h*/*****************
******** #include<stdio.h>
#include<stdlib.h>#
define MAXSIZE
20
typedefstructnode
{
intinfo;structnode
*left,*right;
}NODE;
NODE*createbst(NODE*root)
{
NODE*newnode,*temp,
*parent; int i,n,num;
printf("how many
nodes");
scanf("%d",&n);
printf("enterthe%delements:"
,n); for(i=1;i<=n;i++)
{
newnode=(NODE*)malloc(sizeof(NOD
E));printf("\n enter data");
scanf("%d",&num);newnode-
>info=num; newnode-
>left=newnode->right=NULL;
if(root==NULL)
{
root=newnode;continue;
}
temp=root;while(temp!=NULL)
{
parent=temp;
if(num<temp-
>info)
temp=temp-
>left;else
temp=temp-
>right;
}
if(num<parent->info)
parent-
>left=newnode;else
parent-
>right=newnode;
}
return(root);
}
NODE*search(NODE*root,intkey)
{
NODE*temp=root;while(temp!=NULL)
{
if(key==temp-
>info) return
temp; else
if(key<temp->info)
temp=temp-
>left;else
temp=temp-
>right;
}
returnNULL;
}
NODE*insertbst(NODE*root,i
ntnum){ NODE
*newnode,*temp=root,*parent
;
newnode=(NODE*)malloc(sizeof(NODE));newnode-
>info=num; newnode->left=newnode-
>right=NULL; if(root==NULL)
{
root=newnode;
returnroot;
}
while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
elsetemp=temp->right;
}
if(num<parent->info) parent-
>left=newnode; else parent-
>right=newnode;
return(root);
}
voidpreorder(NODE*root)
{
NODE*temp=root;if(temp!=NULL)
{
printf("%d\t",temp->info);
preorder(temp->left);
preorder(temp->right);
}
}
voidinorder(NODE*root)
{
NODE*temp=root;if(temp!=NULL)
{
inorder(temp->left);
printf("\t%d",temp->info);
inorder(temp->right);
}
}
*************************88ass1a1.c*******************************
*****
#include<stdio.h>#
include"btree.h"v
oid main()
{
NODE*root=NULL,
*temp1; int ch,num;
do
{
printf("\n1:CreateBST");
printf("\n2:Insert");
printf("\n3;search");
printf("\n4:Inodrder");
printf("\n5:Preorder"
);
printf("\n6:Postorder
");
printf("\nenteryourc
hoice");
scanf("%d",&ch);
switch(ch)
{
case1:root=createbst(root);break;
case2:printf("enteravaluetoi
nsert"); scanf("%d",&num);
root=insertbst(root,num);
break; case 4: inorder(root);
break; case
5:preorder(root); break;
}
}while(ch!=7);
}
Q2.WriteaCprogramthatacceptstheverticesandedgesofagraph.Createadjace
ncylistand display the adjacency list. [15 Marks]
ANSWER:-
#include<stdio.h>
typedefstructnode
{
intvertex;
structnode*next;
}NODE;
NODE*list[10];
voidcreatelist(intn)
{
inti,j,ans;
structnode*temp,*new
node; for(i=0;i<n;i++)
{
list[i]=NULL;
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("isthereanyedgebetween%dand%d(1/
0):",i+1,j+1);
scanf("%d",&ans);
if(ans==1)
{
newnode=(NODE*)malloc(size
of(NODE)); newnode-
>vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
}
}
}
}
}//endof function
voiddisplist(intn)
{
structnode*te
mp; int i;
printf("\nTheadjacencylistis:\n");
for(i=0;i<n;i++)
{
printf("\nv%d-
>",i+1); temp=list[i];
while(temp)
{
printf("v%d->",temp-
>vertex); temp=temp-
>next;
}
printf("NULL");
}
}//endof function
voidmain()
{
int n;
printf("enternumberofvertic
es"); scanf("%d",&n);
printf("\nEnterdatafora
djacency list"); createlist(n);
displist(n);
SLIPNO:-11
Q1.WriteaCprogramfortheimplementationofFloydWarshall’salgorithmforf
indingallpairs shortest path using adjacency cost matrix. [15 Marks]
ANSWER:-
#include<stdio.h
>#defineINF999
99
voidprintSolution(intgraph[5][5],intnum)
{
inti,j;
printf("\ndistancebetweeneachpairsofver
tices:\n"); for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][j]==INF)
printf("%7s","INF");
elseprintf("%7d",graph[i][j]);
}
printf("\n");
}
}
voidfloydwarshall(intgraph[5][5],intnum)
{
inti,j,k;
for(k=0;k<num;k++)
{
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][k]+graph[k][j]<graph[
i][j])
graph[i][j]=graph[i][k]+graph[k][j]; }
}
}
printSolution(graph,num);
}
intmain()
{
int
graph[5][5],num,i,j;
printf("\nHowmanyver
tices:");
scanf("%d",&n
um);
for(i=0;i<num;i++)
{
graph[i][i
]=0;
for(j=0;j<num;j++)
{
if(i!=j)
{
printf("EnteredgecostbetV%d&V%d(99999ifedge
not
present)",i+1,j+1);
scanf("%d",&graph[i][j]);
}
}
}
printSolution(graph,num
); floydwarshall(graph,num);
return0;
}
Q2.WriteaCprogramthatacceptstheverticesandedgesofagraph.Createadja
cencylist anddisplay the adjacency list. [15 Marks]
ANSWER:-
#include<stdio.h>
typedefstructnode
{
intvertex;str
uct node
*next;
}NODE;
NODE*list[10];
voidcreatelist(intn)
{
int i,j,ans;
stru
ct node *temp,*newnode;
for(i=0;i<n;i++)
{
list[i]=NULL;
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("isthereanyedgebetween%dand%d(1/
0):",i+1,j+1);
scanf("%d",&an
s); if(ans==1)
{
newnode=(NODE*)malloc(size
of(NODE)); newnode-
>vertex=j+1;
newnode->next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
}
}
}
}
}//endof function
voiddisplist(intn)
{
structnode*temp;
int i;
printf("\nTheadjacencylistis:\n");
for(i=0;i<n;i++)
{
printf("\nv%d->",i+1);
temp=list[i
]; while(temp)
{
printf("v%d->",temp-
>vertex); temp=temp-
>next;
}
printf("NULL");
}
}//endof function
voidmain()
{
int n;
printf("enternumberofvertices");
scanf("%d",&n);printf("\
nEnterdata for adjacency
list"); createlist(n);
displist(n);
SLIPNO:-12
Q1.ImplementaBinarysearchtree(BST)library(btree.h)withoperations–
create,insert,preorder. Write a menu driven program that performs the
above operations. [15 Marks]
ANSWER:-
#include<stdio.h>
#include<stdlib.h>#
define MAXSIZE
20
typedefstructnode
{intinfo;struct node
*left,*right;
}NODE;
NODE*createbst(NODE*root)
{
NODE*newnode,*temp,
*parent; int i,n,num;
printf("how many
nodes");
scanf("%d",&n);
printf("enterthe%delements:"
,n); for(i=1;i<=n;i++)
{
newnode=(NODE
*)malloc(sizeof(NODE));
printf("\nenterdata");scanf("
%d",&num); newnode-
>info=num; newnode-
>left=newnode-
>right=NULL;
if(root==NULL)
{
root=newnode;
continue;
}
temp=root;while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
elsetemp=temp->right;
}
if(num<parent->info)
parent-
>left=newnode;else
parent-
>right=newnode;
}
return(root);
}
NODE*insertbst(NODE*root,intnum)
{
NODE*newnode,*temp=root,*parent;
newnode=(NODE*)malloc(sizeof(NODE));newnode-
>info=num; newnode->left=newnode-
>right=NULL; if(root==NULL)
{
root=newnode
; return root;
}
while(temp!=NULL)
{
parent=temp;
if(num<temp->info)
temp=temp->left;
elsetemp=temp->right;
}
if(num<parent->info) parent-
>left=newnode; else parent-
>right=newnode;
return(root);
}
voidpreorder(NODE*root)
{
NODE*temp=root;if(temp!=NULL)
{
printf("%d\t",temp->info);preorder(temp-
>left);preorder(temp->right);
}
}
****************************[BST.c]*****************
*** ass1a1.c #include<stdio.h>
#include"btr
ee.h" void
main()
{
NODE*root=NULL,
*temp1; int ch,num;
do
{
printf("\n1:CreateBST");
printf("\n2:Insert");
printf("\n3;search");
printf("\n4:Inodrder");
printf("\n5:Preorder"
);
printf("\n6:Postorder
");
printf("\nenteryourc
hoice");
scanf("%d",&ch);
switch(ch)
{
case1:root=createbst(root);break;
case2:printf("enteravaluetoins
ert"); scanf("%d",&num);
root=insertbst(root,num);
break; case 5:preorder(root);
break;
}
}while(ch!=6);
}
Q2.WriteaCprogramfortheimplementationofTopologicalsor
ting.[15Marks] Answer:-
#include<stdio.h>
voidmain()
{
int a,b,u,v,i,j,e,n;
int
cost[10][10];
int
visited[10]={0},min,minc
ost=0; printf("enter no
of vertices");
scanf("%d",&n);
printf("\nentercostforve
rices");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enterthecostforvertices%d-
>%d(cost/999):",i+1,j+1); scanf("%d",&cost[i][j]);
}
}
visited[0]=
1; printf("\n");
for(e=0;e<n;e++)
{
//findedgewithsmallestweight
for(i=0,min=999;i<n;i++)
for(j=0;j<n;j++)
{
if(cost[i][j]==0)
cost[i][j]=999;
if(cost[i][j]<min)
if(visited[i]
!=0)
{
min=cost[i]
[j]; a=u=i;
b=v=j;
}
}
if((visited[u]==0)||(visited[v]==0))
{
printf("\nEdge%d:(%d%d)cost:%d",e+
1,a+1,b+1,min); mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;//markedgesothatitisnoteslected
}
printf("\nMinimumcost=%d",mincost);
}
SLIPNO:-13
Q1.WriteaCprogramfortheImplementationofKruskal’sMinimumspanningt
reealgorithm.[15 Marks]
ANSWER:-
#include<stdi
o.h>
typedefstruct
{
intsrc,dest,weight;
}edge;
edgegraph[12]={1,2,5,1,3,3,2,3,4,2,4,6,2,5,2,3,4,5,3,6,6,4,5,8,4,6,6,5,6,3,5,7,5,6
,7,4}; int
MSTvertices[10];
voidsort(edgegraph[],intnE)
{
int i,pass;edge
temp;
for(pass=1;pass<
nE- 1;pass++)
{
for(i=0;i<nE-pass;i++)
{
if(graph[i].weight>graph[i+1].weight)
{
temp=graph[i];
graph[i]=graph[i+1];
graph[i+1]=temp;
}
}
}
}
intfind(int v,intnV)
{
int k;
for(k=0;k<nV;k++)
{
if(MSTvertices[
k]==v) return 1;
}
return0;
}
voidkruskalMST(intnV,int nE)
{
edgemst[14];
inti=1,j=2,k=1,count=1,mincost=0,first=0,second=0;
//sortedgesinascendingor
der sort(graph,nE);
mst[0]=graph[0];
MSTvertices[0]=graph[0].src;
MSTvertices[1]=graph[0].dest;
mincost=graph[0].weight;
while(count<=nV- 1)
{
first=find(graph[i].src,nV);//chkiffirstvertexisinselect
edlist second=find(graph[i].dest,nV); //chk ifsecond vertex
is in selected list
if(!(first&&second))
{
mst[k++]=graph[i]
; count++;
mincost=mincost+graph[i].w
eight; if(first)
MSTvertices[j++]=grap
h[i].dest; else
MSTvertices[j++]=graph[i].src;
}
i++;
}
printf("Theedgeintheminimumspanningt
reeare\n"); for(i=0;i<nV-1;i++)
printf("%d--
%d==%d\n",mst[i].src,mst[i].dest,mst[i].weight);
printf("minimum
costof spanning tree:%d",mincost);
}
voidmain()
{
intnV=7,nE=12;kruskalMST(nV,nE);
}
Q2.WriteaCprogramthatacceptstheverticesandedgesofagraphandstoreitasa
n
adjacencymatrix.ImplementfunctiontotraversethegraphusingBreadthFirstS
earch(BFS)traversal. [15MARKS]
ANSWER:-
#include<stdio.h>
#defineMAXSIZE20
typedefstruct
{
intdata[MAX
SIZE]; int
front,rear;
}QUEUE;
voidaddq(QUEUE*pq,intn)
{
pq->data[++pq->rear]=n;
}
intremoveq(QUEUE*pq)
{
returnpq->data[++pq->front];
}
voidinitq(QUEUE*pq)
{
pq->front=pq->rear=-1;
}
intisempty(QUEUE*pq)
{
return(pq->rear==pq->front);
}
voidbfs(intm[10][10],intn)
{
inti,j,v,w;
intvisited[20]
={0};
QUEUE q;
initq(&q);
printf("\nBFS");
v=1;
visited[v]=
1; addq(&q,v);
while(!isempty(&q))
{
v=removeq(&q);
printf("v%d\t",v); for(w=1;w<=n;w++)
{ if((m[v][w]==1)&&(visited[w]==0))
{
addq(&q,w);
visited[w]=1;
}
}
}//endofwhile
}//endofbfs
voidmain()
{
int m[10][10],n,v,i,j;
printf("Howmanyver
tices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Isthereedgebetween%d-
>%d(1/0):",i,j); scanf("%d",&m[i][j]);
}
}
printf("Adjacencymatri
xis\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\nBFSTraversal");bfs(m,n);
}
SLIPNO:-14
Q1.WriteaCprogramfortheimplementationofFloydWarshall’salgorithmforf
indingallpairs shortest path using adjacency cost matrix. [15 Marks]
ANSWER:-
#include<stdio.h
>#defineINF999
99
voidprintSolution(intgraph[5][5],intnum)
{
inti,j;
printf("\ndistancebetweeneachpairsofver
tices:\n"); for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][j]==INF)
printf("%7s","INF");
elseprintf("%7d",graph[i][j]);
}
printf("\n");
}
}
voidfloydwarshall(intgraph[5][5],intnum)
{
inti,j,k;
for(k=0;k<num;k++)
{
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][k]+graph[k][j]<graph[
i][j])
graph[i][j]=graph[i][k]+graph[k][j];
}
}
}
printSolution(graph,num);
}
intmain()
{int graph[5][5],num,i,j;
printf("\nHow
many
vertices:");
scanf("%d",&n
um);
for(i=0;i<num;i
++)
{
graph[i][i]=0;
for(j=0;j<num;j++
)
{
if(i!=j)
{
printf("EnteredgecostbetV%d&V%d(99999ifedge
not
present)",i+1,j+1);
scanf("%d",&graph[i][j]);
}
}
}
printSolution(graph,num
);
floydwarshall(graph,nu
m);
return0;
}
Q2.WriteaCprogramwhichusesBinarysearchtreelibraryanddisplaysnodes
ateachlevel,and total levels in the tree. [15 Marks]
ANSWER:-
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typed
ef struct node
{
intinfo;
structnode*left,*right;
}NODE;
typedefstruct
{
NODE*data[MAXSIZE];
intfront,rear;
}QUEUE;
voidinitq(QUEUE*pq)
{
pq->front=pq->rear=-1;
}
voidaddq(QUEUE*pq,NODE*tnode)
{
pq->data[++pq->rear]=tnode;
}
NODE*removeq(QUEUE*pq)
{
returnpq->data[++pq->front];
}
intisempty(QUEUE*pq)
{
return(pq->front==pq->rear);
}
intisfull(QUEUE*pq)
{
return(pq->rear==MAXSIZE-1);
}
NODE*createbst(NODE*root)
{
NODE*newnode,*temp,
*parent; int i,n,num;
printf("how many
nodes");
scanf("%d",&n);
printf("enterth
e%d
elements:",n);
for(i=1;i<=n;i+
+)
{
newnode=(NODE*)malloc(size
of(NODE));
scanf("%d",&num);
newnode->info=num;
newnode->left=newnode->right=NULL;
if(root==NULL)
{
root=newnode;
continue;
}
temp=root;
while(temp!=NULL)
{
parent=temp;
if(num<temp-
>info)
temp=temp->left;
else
temp=temp->right;
}
if(num<parent->info) parent-
>left=newnod
e; else
parent->right=newnode;
}
return(root);
}
voidlevelorder(NODE*root)
{
inti,level=0;
NODE*temp,*marker=NULL;
QUEUEq; int
total=0,cnt=0;
initq(&q);
addq(&q,root);
addq(&q,marke
r);
printf("\nLevel%d---
>",level);
while(!isempty(&q))
{
temp=removeq(
&q);
if(temp==marker)
{
printf("\t
cnt=%d",cnt);
cnt=0;
level++;
if(!isempty(&q))
{
addq(&q,marker);
printf("\nLevel%d--->",level);
}
}
else
{
printf("%d\t",tem
p->info); if(temp-
>left)
addq(&q,temp->left);
if(temp->right)addq(&q,temp-
>right);
total=total
+1; cnt=cnt+1;
}
}
printf("\ntotalnodes=%d",total);
}
/**********************************************ass2a1.c************
****************
*****/
#include<stdi
o.h>
#include<stdl
ib.h>#include
"bst.h"
voidmain()
{
NODE
*root=NULL;
root=createbst(
root);
printf("\nLevelordertraversa
lis"); levelorder(root);
}
SLIPNO:-15
Q1.WriteaCprogramfortheImplementationofPrim’sMinimumspanningtr
eealgorithm.[15 Marks]
ANSWER:-
#include<stdio.h>
voidmain()
{
int
a,b,u,v,i,j,e,n;
intcost[10][10
];int
visited[10]={0},min,mincost=0;
printf("enternoofvertices");
scanf("%d",&n);
printf("\nenter cost for
verices");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enterthecostforvertices%d-
>%d(cost/999):",i+1,j+1); scanf("%d",&cost[i][j]);
}
}
visited[0]=
1; printf("\n");
for(e=0;e<n;e++)
{
//findedgewithsmallestweight
for(i=0,min=999;i<n;i++)
for(j=0;j<n;j++)
{
if(cost[i][j]==0)
cost[i][j]=999;
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i]
[j]; a=u=i;
b=v=j;
}
}
if((visited[u]
==0)||(visi
t
ed[v]==0)
)
{
printf("\nEdge%d:(%d%d)cost:%d",e+
1,a+1,b+1,min); mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;//markedgesothatitisnoteslected
}
printf("\nMinimumcost=%d",mincost);
}
Q2.WriteaCprogramfortheimplementationofDijkstra’sshortestpathalgori
thmforfinding shortest path from a given source vertex using adjacency
cost matrix. [15 Marks]
ANSWER:-
#include<stdi
o.h>
intcost[8][8]={{0,999,999,999,999,999,999,999},{30,0,999,999,999,999,999,999
},
{100,80,0,999,999,999,999,999},{999,999,120,0,999,999,999,999},{999,999,999,
150,0,25,999,999},
{999,999,999,100,999,0,90,140},{999,999,999,999,999,999,0,100},{170,999,999,
999,999,999,999,0}}
;
voiddijkstra(int v,intn)
{
int i,j,u,w,count,min; int
dist[10],visited[10]={0}; visited[v]=1;//first set vertex V=4 as 1
means Selected vertex or visited vertex
for(i=0;i<n;i++)
dist[i]=cost[v][i];
count
=2;
while(count
<n)
{
min=999;
for(i=0;i<n;i
++)
if(visited[i]==0&&dist[i]<min)
{
min=dist
[i]; u=i;
}
visited[u]
=1;
for(w=0;w<n;w++)
if(dist[u]+cost[u][w]<dist[w])
dist[w]=dist[u]+cost[u][w];
count++;
}
printf("\nShortestdistancesfromvertex%dare:-
\n",v); for(i=0;i<n;i++)
printf("%d\t",dist[i]);
}
voidmain()
{
dijkstra(4,8);//4meansstartingvertesi.evertexV4
}
SLIP NO:16
Q1.WriteaCprogramfortheimplementationofFloydWarshall’salgorithmforf
indingallpairs shortest path using adjacency cost matrix. [15 Marks]
ANSWER:-
#include<stdio.h
>#defineINF999
99
voidprintSolution(intgraph[5][5],intnum)
{
inti,j;
printf("\ndistancebetweeneachpairsofver
tices:\n"); for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][j]==INF)
printf("%7s","INF");
elseprintf("%7d",graph[i][j]);
}
printf("\n");
}
}
voidfloydwarshall(intgraph[5][5],intnum)
{
inti,j,k;
for(k=0;k<num;k++)
{
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(graph[i][k]+graph[k][j]<graph[
i][j])
graph[i][j]=graph[i][k]+graph[k][j];
}
}
}
printSolution(graph,num);
}
intmain()
{
intgraph[5][5],num,i,j;print
f("\nHow many vertices:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
graph[i][i
]=0;
for(j=0;j<num;j++)
{
if(i!=j)
{
printf("EnteredgecostbetV%d&V%d(99999ifedge
not
present)",i+1,j+1);
scanf("%d",&graph[i][j]);
}
}
}
printSolution(graph,num
); floydwarshall(graph,num);
return0;
}
Q2.WriteaprogramtosortnrandomlygeneratedelementsusingHeapsortm
ethod.[15Marks] ANSWER:-
#include<stdio.h>
voi
d display(int a[],int
n)
{
int i;
for(i=0;i<n;i
++)
printf("%d\t",a[i]);
}
voidheapify(inta[],inttop,intlast)
{
intj,temp,ke
y;
key=a[top];
j=2*top+1;
if((j<last)&&(a[j]<
a[j+1])) j=j+1;
if((j<=last)&&(key<a[j]))
{
temp=a[top];
a[top]=a[j];
a[j]=temp;
heapify(a,j,last);
}
}
voidbuildheap(inta[],intn)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(a,i,
n-1);
}
voidheapsort(inta[],intn)
{
inti,temp,top=0,last;
buildheap(a,n);
printf("\nInitialh
eap=");
display(a,n);
for(last=n-1;last>=1;last--)
{
temp=a[top]; a[top]=a[last];
a[last]=temp; printf("\n
After iteration %d::",n-last);
display(a,
n);
heapify(a,top,la
st-1);
}
}
voidgenerate(inta[],intn)
{
int i;
for(i=0;i<n;i
++)
{
a[i]=rand()%100;
}
}
voidmain()
{
inta[50],n;
printf("enternoofelemn
et"); scanf("%d",&n);
generate(a,n);
printf("arrayelementsbeforesorti
ngare"); display(a,n);
heapsort(a,n);
printf("\nThesortedelements
are::"); display(a,n);
}
SLIP17:-
Q1.Writeamenudrivenprogramtoimplementhashtableusingarray(insert
,delete,display). Use any of the above-mentioned hash functions. In case
of collision apply linear probing. [15 Marks]
ANSWER:-
#include<stdio
.h>
inthf(intkey,in
ti)
{
return(key%10+i)%10;
}
voidinsert(intHT[10],intkey)
{
inti,index;
for(inti=0;i<10;i++)
{
index=hf(ke
y,i);
if(HT[index]
==-1)
{
HT[index]=ke
y; return;
}
}
printf("Couldnotinsertkey%d",key);
}
intsearch(intHT[10],intkey)
{
int i,index;
for(i=0;i<10;i++
)
{
index=hf(key,i);
if(HT[index]==k
ey) return
index;
}
return-1;
}
voiddeletekey(intHT[10],intkey)
{
intindex;
index=search(H
T,key);
if(index==-1)
printf("key%dnotfoun
d",key); else
HT[index]=-1;//deletekey
}
voidshowTable(intHT[10])
{
int i;
for(i=0;i<10;i++)
printf("%d[%d]\n",i,HT[i]);
}
intmain()
{
intHT[10],choice,key,
index; for(int
i=0;i<10;i++)
HT[i]=-1;
do
{
printf("1:INSERT\n2:SEARCH\n3:DELETE\n4:EXIT\n");
printf("Enteryourch
oice:-");
scanf("%d",&choice
);
switch(choice)
{
case1:
printf("Enterthekeytobeinsert
ed:-"); scanf("%d",&key);
insert(HT,k
ey);
showTable(
HT);
break;
case2:
printf("Enterthekeytobesearc
hed:-"); scanf("%d",&key);
index=search(H
T,key);
if(index==-1)
printf("%dnotfoun
d",key); else
printf("%dfoundatposition%d"
,key,index); break;
case3:
printf("Enterthekeytobedelete
d:-"); scanf("%d",&key);
deletekey(HT,ke
y);
showTable(HT);
}
}while(choice!=4
); return 0;
}
Q2.WriteaprogramtosortnrandomlygeneratedelementsusingHeapsortm
ethod.[15Marks] ANSWER:-
#include<stdio.h>
voiddisplay(inta[],intn)
{
int i;
for(i=0;i<n;i+
+)
printf("%d\t
",a[i]);
}
voidheapify(inta[],inttop,intlast)
{
intj,temp,ke
y;
key=a[top];
j=2*top+1;
if((j<last)&&(a[j]<
a[j+1])) j=j+1;
if((j<=last)&&(key<a[j]))
{
temp=a[top
];
a[top]=a[j];
a[j]=temp;
heapify(a,j,last);
}
}
voidbuildheap(inta[],intn)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(a,i,n-1);
}
voidheapsort(inta[],intn)
{
inti,temp,top=0,las
t; buildheap(a,n);
printf("\nInitialh
eap=");
display(a,n);
for(last=n-1;last>=1;last--)
{
temp=a[top];
a[top]=a[last
];
a[last]=temp
;
printf("\nAfteriteration%d
::",n-last); display(a,n);
heapify(a,top,last-1);
}
}
voidgenerate(inta[],intn)
{
int i;
for(i=0;i<n;i
++)
{
a[i]=rand()%100;
}
}
voidmain()
{
inta[50],n;
printf("enternoofelemn
et"); scanf("%d",&n);
generate(a,n);
printf("arrayelementsbeforesorti
ngare"); display(a,n);
heapsort(a,n);
printf("\nThesortedelements
are::"); display(a,n);
}
SLIPN018:-
Q1.WriteaCprogramthatacceptstheverticesandedgesofagraphandstoresitas
anadjacency matrix. Display the adjacency matrix. [15 Marks]
ANSWER:-
#include<stdio
.h>void main()
{
int m[10][10],n,v,i,j;
printf("Howmanyver
tices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Isthereedgebetween%d->%d(1/0):",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("Adjacencymatri
xis\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}
Q2.WriteaCprogramfortheImplementationofPrim’sMinimumspanningtree
algorithm.[15 Marks]
ANSWER:-
#include<stdio.h>
voidmain()
{
inta,b,u,v,i,j,e
,n; int
cost[10][10];
intvisited[10]={0},min,mi
ncost=0; printf("enter no
of vertices");
scanf("%d",&n);
printf("\nentercostforve
rices");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enterthecostforvertices%d-
>%d(cost/999):",i+1,j+1); scanf("%d",&cost[i][j]);
}
}
visited[0]=
1;
printf("\n
");
for(e=0;e<n;e++)
{
//findedgewithsmallestweight
for(i=0,min=999;i<n;i
++) for(j=0;j<n;j++)
{
if(cost[i][j]==0)
cost[i][j]=999;
if(cost[i][j]<mi
n) if(visited[i]!=0)
{
min=cost[i]
[j]; a=u=i;
b=v=j;
}
}
if((visited[u]==0)||(visited[v]==0))
{
printf("\nEdge%d:(%d%d)cost:%d",e+1,a+1,
b+1,min); mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;//markedgesothatitisnoteslected
}
printf("\nMinimumcost=%d",mincost);
}
SLIPNO19:-
Q1.ImplementaBinarysearchtree(BST)library(btree.h)withoperations –
create,insert,in order. Write a menu driven program that performs the
above operations. [15 Marks]
ANSWER:-
//btree.h*/
#include<stdio.h
>
#include<stdlib.
h>#defineMAXS
IZE20
typedefstructno
de
{
intinfo;
structnode*left,*right;
}NODE;
NODE*createbst(NODE*root)
{
NODE*newnode,*temp,
*parent; int i,n,num;
printf("howmanyn
odes");
scanf("%d",&n);
printf("enterthe%delements:"
,n); for(i=1;i<=n;i++)
{
newnode=(NODE*)malloc(size
of(NODE)); printf("\n enter
data");
scanf("%d",&n
um); newnode-
>info=num;
newnode->left=newnode-
>right=NULL;
if(root==NULL)
{
root=newnode;
continue;
}
temp=root;
while(temp!=NULL)
{
parent=temp;
if(num<temp-
>info)
temp=temp-
>left;
else
temp=temp->right;
}
if(num<parent-
>info) parent-
>left=newnode;
else
parent->right=newnode;
}
return(root);
}
NODE*insertbst(NODE*root,intnum)
{
NODE
*newnode,*temp=root,*parent
;
newnode=(NODE*)malloc(size
of(NODE)); newnode-
>info=num;
newnode->left=newnode-
>right=NULL;
if(root==NULL)
{
root=newnode
; return root;
}
while(temp!=NULL)
{
parent=temp;
if(num<temp-
>info)
temp=temp-
>left;
else
temp=temp->right;
}
if(num<parent-
>info) parent-
>left=newnode;
else
parent->right=newnode;
return(root);
}
voidinorder(NODE*root)
{
NODE*temp=
root;
if(temp!=NUL
L)
{
inorder(temp-
>left);
printf("\t%d",tem
p->info);
inorder(temp-
>right);
}
}
**********************ass1a1.c************************************
*************** #include<stdio.h>
#include"btr
ee.h" void
main()
{
NODE*root=NULL,
*temp1; int ch,num;
do
{
printf("\n1:CreateBS
T");
printf("\n2:Insert");
printf("\n4:Inodrder"
);
printf("\nenteryourc
hoice");
scanf("%d",&ch);
switch(ch)
{
case
1:root=createbst(root)
; break;
case2:printf("enteravaluetoins
ert"); scanf("%d",&num);
root=insertbst(root,num);
break;
case3:inorder(ro
ot); break;
}
}while(ch!=4);
}
Q2.WriteaCprogramthatacceptstheverticesandedgesofagraph.Createadja
cencylist anddisplay the adjacency list. [15 Marks]
ANSWER:-
#include<stdio.h>
typedefstructnode
{
intvertex;
structnode*next;
}NODE;
NODE*list[10];
voidcreatelist(intn)
{
inti,j,ans;
structnode*temp,*newn
ode; for(i=0;i<n;i++)
{
list[i]=NULL;
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("isthereanyedgebetween%dand%d(1/0):",i+
1,j+1);
scanf("%d",&
ans); if(ans==1)
{
newnode=(NODE*)malloc(size
of(NODE)); newnode-
>vertex=j+1;
newnode-
>next=NULL;
if(list[i]==NULL)
list[i]=temp=newnode;
else
{
temp-
>next=newnode;
temp=newnode;
}
}
}
}
}
}//endof function
voiddisplist(intn)
{
structnode*te
mp; int i;
printf("\nTheadjacencylistis:\n
"); for(i=0;i<n;i++)
{
printf("\nv%d-
>",i+1);
temp=list[i];
while(temp)
{
printf("v%d->",temp-
>vertex); temp=temp-
>next;
}
printf("NULL");
}
}//endof function
voidmain()
{
int n;
printf("enternumberofvertices
"); scanf("%d",&n);
printf("\nEnterdataforadjacen
cylist"); createlist(n);
displist(n);
}
SLIPNO20:-
Q1.WriteaCprogramfortheImplementationofKruskal’sMinimumspanningt