Date post: | 01-Mar-2016 |
Category: |
Documents |
Upload: | pavithraram |
View: | 216 times |
Download: | 0 times |
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 1/22
S.JAYASHRI
412512104039
PRIM’S ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int edge[10][2];
oid !"in$
%
int &'i!sint cost[10][10](int n$;
int i()(*(n(cost[10][10];
int totcost+0;
cl'sc'$;
&'int,-n ente' the no o, e'tices : -$;
sc"n,-/d-(n$;
&'int,-n n+/d-(n$;
&'int,-nente' the cost o, the !"t'i n -$;
,o'i+1;i<+n;i$
,o')+1;)<+n;)$
i,i++)$
%
cost[i][)]+0;
3
else
%
&'int,-n cost ,'o! edge /d to /d t -(i()$;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 2/22
S.JAYASHRI
412512104039
sc"n,-/d-(cost[i][)]$;
3
,o'i+1;i<+n;i$
%
,o')+1;)<+n;)$
&'int,-/d-(cost[i][)]$;
&'int,-n-$;
3
totcost+&'i!scost(n$;
&'int,-n tot"l cost o, !ini!u! s"&nning t'ee is /d-(totcost$;
getch$;
3
int &'i!sint cost[10][10](int n$
%
int closest[10](lo4cost[10](!in(i()(*(totcost+0;
,o'i+2;i<+n;i$
%
lo4cost[i]+cost[1][i];
closest[i]+1;
3
,o'i+2;i<+n;i$
%
!in+1000;
*+2;
,o')+2;)<+n;)$
i,lo4cost[)]<!inlo4cost[)]5+0$
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 3/22
S.JAYASHRI
412512104039
%
!in+lo4cost[)];
*+);
3
i,!in++1000$
%
&'int,-n the g'"&h is not connected -$;
eit0$;
3
edge[i61][1]+closest[*];
edge[i61][2]+*;
&'int,-n /d 6> /d-( edge[i61][1](edge[i61][2]$;
totcost+cost[edge[i61][1]][edge[i61][2]];
lo4cost[*]+0;
,o')+2;)<+n;)$
%
i,cost[*][)]<lo4cost[)]$
i,lo4cost[)]>0$
%
lo4cost[)]+cost[*][)];
closest[)]+*;
3
3
3
'etu'ntotcost$;
3
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 4/22
S.JAYASHRI
412512104039
OUTPUT:
ente' the no o, e'tices : 7
n+7
ente' the cost o, the !"t'i
cost ,'o! edge 1 to 2 1
cost ,'o! edge 1 to 7 2
cost ,'o! edge 2 to 1 7
cost ,'o! edge 2 to 7 8
cost ,'o! edge 7 to 1 9
cost ,'o! edge 7 to 2
012
708
90
1 6> 2
1 6> 7
tot"l cost o, !ini!u! s"&nning t'ee is 7
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 5/22
S.JAYASHRI
412512104039
PRIORITY QUEUE USING HEAPS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<!"lloc.h>
#include<&'ocess.h>
st'uct he"&st'uct
%int c"&"cit;
int sie;
int =";
3;t&ede, st'uct he"&st'uct =&;
& initi"lieint !"(int !in$
%& h;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 6/22
S.JAYASHRI
412512104039
i,!"<!in$
&'int,-n &'io'it ueue is s!"ll-$;
h+st'uct he"&st'uct=$!"llocsieo,st'uct he"&st'uct$$;
i,h++?@$
&'int,-n out o, s&"ce-$;
h6>c"&"cit+!";
h6>sie+0;
h6>"[0]+!in;
'etu'n h;
3oid inse'tint (& h$
%int i;
i,h6>sie++h6>c"&"cit$
%&'int,-n &'io'it ueue is ,ull-$;
3else
,o'i+h6>sie;h6>"[iB2]>;iB+2$
%h6>"[i]+h6>"[iB2];
3h6>"[i]+;
3int del!in& h$
%int i(!ind"t"(l"st("(child;
i,h6>sie++0$
%&'int,-n &'io'it ueue is e!&t-$;
'etu'n h6>"[0]$;
3!ind"t"+h6>"[i];
l"st+h6>"[h6>sie66];
,o'i+1;i=2<+h6>sie;i+child$
%child+i=2;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 7/22
S.JAYASHRI
412512104039
i,child5+h6>sie h6>"[child1]<h6>"[child]$
child;
i,l"st>h6>"[child]$
%h6>"[i]+h6>"[child];
3Clse b'e"*;
3h6>"[i]+l"st;
'etu'n !ind"t";
3oid dis&l"& h$
%int i;
,o'i+1;i<+h6>sie;i$
%&'int,-n the d"t" is /d-(h6>"[i]$;
33oid !"in$
%& h;
int (((u(;
ch"' ch;
cl'sc'$;
&'int,-ente' the !" no o, ele!ent in the &'io'it ueue-$;
sc"n,-/d-($;
&'int,-ente' the !in no o, ele!ent in the &'io'it ueue-$;
sc"n,-/d-($;
h+initi"lie($;
!enu:
&'int,-n&'io'it ueue-$;
&'int,-n 1.inse'tn 2.deleten 7.dis&"ln 8.eitn-$;
&'int,-ente' ou' choice-$;
sc"n,-/d-(u$;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 8/22
S.JAYASHRI
412512104039
s4itchu$
%c"se 1:
&'int,-ente' the d"t"t-$;
sc"n,-/d-($;
inse't(h$;
b'e"*;
c"se 2:
&'int,-nthe deleted ele!ent is /dn-($;
b'e"*;
c"se 7: dis&l"h$;
b'e"*;
c"se 8: eit0$; 3goto !enu; 3
OUTPUT:
ente' the !" no o, ele!ent in the &'io'it ueue7
ente' the !in no o, ele!ent in the &'io'it ueue2
&'io'it ueue
1.inse't
2.delete
7.dis&"l
8.eit
ente' ou' choice1
ente' the d"t" 10
&'io'it ueue
1.inse't
2.delete
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 9/22
S.JAYASHRI
412512104039
7.dis&"l
8.eit
Cnte' ou' choice1
ente' the d"t" 20
1.inse't
2.delete
7.dis&"l
8.eit
ente' ou' choice1
ente' the d"t" 70
&'io'it ueue
1.inse't
2.delete
7.dis&"l
8.eit
ente' ou' choice2
the deleted ele!ent is 70
&'io'it ueue
1.inse't
2.delete
7.dis&"l
8.eit
ente' ou' choice7
the d"t" is 10
the d"t" is 20
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 10/22
S.JAYASHRI
412512104039
IMPLEMENTATION OF HASHING USING LINEAR PROBING
P ROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#de,ine MAD 10
oid dis&l"int "[]$;
oid !"in$
%
int "[MAD](nu!(*e(i;
ch"' "ns;
int c'e"teint$;oid line"'E&'obint [](int(int$(dis&l"int []$;
cl'sc'$;
&'int,-n collision h"ndling b line"' &'obing-$;
,o'i+0;i<MAD;i$
"[i]+ 61;
do
%&'int,-n ente' the nu!be'-$;
sc"n,-/d-(nu!$;
*e+c'e"tenu!$;
line"'E&'ob"(*e(nu!$;
&'int,-n Fo u 4ish to continueBn$-$;
"ns+getche$;34hile"ns++HH$;
dis&l""$;
getch$;
3
int c'e"teint nu!$
%int *e;
*e+nu!/10;
'etu'n *e;
3
oid line"'E&'obint "[MAD](int *e(int nu!$
%
int ,l"g(i(count+0;
BBoid dis&l"int "[]$;
,l"g+0;
i,"[*e]++61$
"[*e]+nu!;
else
%
i+0;
4hilei<MAD$
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 11/22
S.JAYASHRI
412512104039
%
i,"[i]5+ 61$
count;
i;
3
i,count++MAD$
%
&'int,-nh"sh t"ble is ,ull-$;
dis&l""$;
getch$;
eit1$;
3
,o'i+*e1;i<MAD;i$
i,"[i]++ 61$
%
"[i]+nu!;,l"g+1;
b'e"*;
3
,o'i+0;i<*e,l"g++0;i$
i,"[i]++ 61$
%
"[i]+nu!;
,l"g+1;
b'e"*;
3
3
3oid dis&l"int "[MAD]$
%
int i;
&'int,-n the h"sh t"ble is...n-$;
,o'i+0;i<MAD;i$
&'int,-n /d /d-(i("[i]$;
3
Ou!u:
Iollision h"ndling b line"' &'obing
Cnte' the nu!be' 0
Fo u 4ish to continueBn$
ente' the nu!be' 2
Fo u 4ish to continueBn$
Cnte' the nu!be' 1
Fo u 4ish to continueBn$
Cnte' the nu!be' J
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 12/22
S.JAYASHRI
412512104039
Fo u 4ish to continueBn$
Cnte' the nu!be' 7
Fo u 4ish to continueBn$
Cnte' the nu!be' 8
Fo u 4ish to continueBn$
Cnte' the nu!be' 9
Fo u 4ish to continueBn$
Cnte' the nu!be' K
Fo u 4ish to continueBn$
Cnte' the nu!be'
Fo u 4ish to continueBn$
Cnte' the nu!be' L
Fo u 4ish to continueBn$
Cnte' the nu!be' 11
"sh t"ble is ,ull
Nhe h"sh t"ble is...
0 0
1 1
2 2 7 7
8 8
9 9
L L
J J
K K
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 13/22
S.JAYASHRI
412512104039
INSERTION IN A"L TREE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<"lloc.h>
t&ede, st'uct node st;
st'uct node
%
ch"' n"!e[20];
int l'(inde;
st =le,t(='ight(=&"';
3=he"d(=te!&;
oid "dd$;
int chec*st=$;
oid indeing$;
oid 'ot"tion int( int$;
oid dis&l"$;
st =st*[70];
int to&+61;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 14/22
S.JAYASHRI
412512104039
e!&t$;
oid &ushst=$;
st= &o&$ ;
oid !"inoid$
%
int i()(n;
ch"' ch(n"!e[20];
cl'sc'$;
he"d+st=$!"llocsieo,st$$;
he"d6>le,t+?@;
he"d6>'ight+?@;
he"d6>&"'+?@;
he"d6>inde+0;
&'int,-ntente' the nu!be' o, nodes to be inse'ted: -$;
sc"n,-/d-(n$;
&'int,-nntCnte' the nodes :: nntt-$;
sc"n,-/s-(he"d6>n"!e$;
,o'i+1 ;i<n;i$
%
te!&+st=$!"llocsieo,st$$;
te!&6>le,t+?@;
te!&6>'ight+?@;
te!&6>&"'+?@;
te!&6>inde+0;
&'int,-ntt-$;
sc"n,-/s-(te!&6>n"!e$;
"dd$;
indeing$;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 15/22
S.JAYASHRI
412512104039
3
dis&l"$;
getch$;
3
oid "dd$
%
int ,1;
st =t&(=t&1;
t&+he"d;
4hilet&$
%
t&1+t&;
,1+chec*t&$;
i,,1++1$
t&+t&6>'ight;
else
t&+t&6>le,t;
3
i,,1++1$
%
t&16>'ight+te!&;
te!&6>&"'+t&1;
te!&6>l'+61;
3
else
%
t&16>le,t+te!&;
te!&6>&"'+t&1;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 16/22
S.JAYASHRI
412512104039
te!&6>l'+1;
3
3
chec*st =t&$
%
int ,l"g(i;
,o'i+0;t&6>n"!e[i]5+H0H;i$
%
i,t&6>n"!e[i]<te!&6>n"!e[i]$
%
,l"g+1;
b'e"*;
3
i,t&6>n"!e[i]>te!&6>n"!e[i]$
%
,l"g+0;
b'e"*;
3
3
i,t&6>n"!e[i]++H0H$
i,te!&6>n"!e[i]5+H0H$
,l"g+1;
'etu'n ,l"g;
3
oid indeing$
%
int &"th1+0(&"th2;
4hilete!&6>&"'$
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 17/22
S.JAYASHRI
412512104039
%
te!&6>&"'6>inde+te!&6>l';
&"th2+&"th1;
i,te!&6>l'++1$
&"th1+1;
i,te!&6>l'++61$
&"th1+0;
te!&+te!&6>&"';
i,te!&6>inde<61te!&6>inde>1$
%
'ot"tion&"th1(&"th2$;
b'e"*;
3
i,te!&6>inde++0$
b'e"*;
33
oid 'ot"tionint &"th1(int &"th2$
%
oid $;
oid RR$;
i,&"th1++1$
i,&"th2++1$
$;
else
RR$;
3
oid RR$
%
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 18/22
S.JAYASHRI
412512104039
st =hd(=!(=!';
int ,;
hd+te!&;
!+hd6>'ight;
!'+!6>le,t;
hd6>'ight+!';
!'6>&"'+hd;
!'6>l'+61;
!6>le,t+hd;
i,hd6>&"'$
%
,+hd6>l';
hd+hd6>&"';
i,,++1$
hd6>le,t+!;
else
hd6>'ight+!;
!6>l'+,;
!6>&"'+hd;
3
else
%
he"d+!;
!6>&"'+?@;
3
hd+!6>le,t;
hd6>&"'+!;
hd6>l'+1;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 19/22
S.JAYASHRI
412512104039
!6>inde+0;
hd6>inde+0;
3
oid $
%
st =hd(=!(=!';
int ,;
hd+te!&;
!+hd6>le,t;
!'+!6>'ight;
hd6>le,t+!';
!'6>&"'+hd;
!'6>l'+1;
!6>'ight+hd;
i,hd6>&"'$
%
,+hd6>l';
hd+hd6>&"';
i,,++1$
hd6>le,t+!;
else
hd6>'ight+!;
!6>l'+,;
!6>&"'+hd;
3
else
%
he"d+!;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 20/22
S.JAYASHRI
412512104039
!6>&"'+?@;
3
hd+!6>'ight;
hd6>&"'+!;
hd6>l'+61;
!6>inde+0;
hd6>inde+0;
3
oid dis&l"$
%
oid ino'de'$;
&'int,-nntt A t'ee is +> -$;
&'int,-nntQno'de' dis&l" :: nt-$;
ino'de'$;
3
oid ino'de'$
%
te!&+he"d;
4hilete!&e!&t$$
%
4hilete!&$
%
&ushte!&$;
te!&+te!&6>le,t;3
te!&+&o&$;
&'int,-/s-(te!&6>n"!e$;
te!&+te!&6>'ight;
3
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 21/22
S.JAYASHRI
412512104039
3
3
oid &ushst =node$
%
i,to&++70$
&'int,-ntt"c* is ,ull -$;
else
%
to&;
st*[to&]+node;
3
3
st= &o&$
%
st =node;
i,5e!&t$$
node+?@;
else
%
node+st*[to&];
to&66;
3
'etu'nnode$;
3
e!&t$
%
i,to&++61$
'etu'n0$;
7/18/2019 last 4 pgms with op.
http://slidepdf.com/reader/full/last-4-pgms-with-op 22/22
S.JAYASHRI
412512104039
else
'etu'n1$;
3
OUTPUT:
Cnte' the nu!be' o, nodes to be inse'ted:
Cnte' the nodes:9 7 K
A t'ee is +>
Qno'de' dis&l":
7 9 K