You are on page 1of 35

1

/* Write C++ programs to implement the Stack ADT using an array */


/* Write C++ programs to implement the Queue ADT using an array */
1) /* Write C++ programs to implement the Stack ADT using a singly linkedlist*/
include!iostream"h#
include!conio"h#
include!stdli$"h#
class node
%
pu$lic&
class node *ne't(
int data(
)(
class stack & pu$lic node
%
node **irst+*top+*cur+*n,(
pu$lic&
int count(
stack-)
%
*irst.top./011(
count.2(
)
3oid push-int ')
%
n,.ne, node(
i*-n,../011)
%
cout!!4Stack is *ull4(
return(
)
n,5#data.'(
n,5#ne't./011(
i* -top../011)
%
top.*irst.n,(
count++(
)
else
6
%
top5#ne't.n,(
top.n,(
count++(
)
)
3oid display-)
%
cur.*irst(
i* -count..2)
%
cout !!4 stack under *lo,4(
return(
)
,hile-cur7./011)
%
cout !!cur5#data!!4 4(
cur.cur5#ne't(
)
)
3oid pop-)
%
node *pr(
i*-count..2)
%
cout !!4stack under *lo,4(
return(
)
else
%
cur.*irst(
,hile-cur5#ne't7./011)
%
pr.cur(
cur.cur5#ne't(
)
count55(
i*-count..51)
%
count.2(
return(
)
else
pr5#ne't./011(
delete cur(
) )
)(
main-)
%
8
stack s1(
int ch(
,hile-1)
%
cout !!49n1":0S;9n6":<:9n8"D=S:1A>9n?"@A=T9n enter ur choice&4(
cin ## ch(
s,itch-ch)
%
case 1& cout !!49n enter a element4(
cin ## ch(
s1"push-ch)(
$reak(
case 6& s1"pop-)(
$reak(
case 8& s1"display-)(
$reak(
case ?& e'it-2)(
)
)
)
<0T:0T
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&1
enter a element68
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&1
enter a elementBC
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&8
68 BC
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&6
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&8
68
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&6
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&6
stack under *lo,
1":0S; 6":<: 8"D=S:1A> ?"@A=T
enter ru choice&?
?
2) /*Write C++ programs to implement the Queue ADT using a singly linked
list*/
include!iostream"h#
include!conio"h#
include!stdli$"h#
class node
%
pu$lic&
class node *ne't(
int data(
)(
class Queue & pu$lic node
%
node **ront+*rear+*n,(
pu$lic&
Queue-)
%
*ront.rear./011(
)
3oid QDinsert-int ')
%
n,.ne, node(
i*-n,../011)
%
cout!!4Q is *ull4(
return(
)
n,5#data.'(
n,5#ne't./011(
i* -*ront../011 )
%
*ront.rear.n,(
)
else
%
rear5#ne't.n,(
rear.n,(
)
)
3oid QDdisplay-)
%
i* -rear../011)
%
cout !!4 Eueue under *lo,4(
return(
)
else
F
%
node *temp(
temp.*ront(
,hile-temp 7. /011)
%
cout !!temp5#data!! 4 4(
temp.temp5#ne't(
)
)
)
3oid QDdelete-)
%
i*- *ront../011)
%
cout !!4Eueue under *lo,4(
return(
)
i*-*ront .. rear)
%
*ront . rear ./011(
return(
)
*ront.*ront5#ne't(
)
)(
main-)
%
Queue s1(
int ch(
clrscr-)(
,hile-1)
%
cout
!!49n1"QD=/S@GT9n6"QDD@1@T@9n8"QDD=S:1A>9n?"@A=T9n @nter ru
choice&4(
cin ## ch(
s,itch-ch)
%
case 1&
cout !!49n enter a element4(
cin ## ch(
s1"QDinsert-ch)( $reak(
case 6& s1"QDdelete-)($reak(
case 8& s1"QDdisplay-)($reak(
case ?& e'it-2)(
)
)
)
OUTUT
B
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&1
enter a element 6
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&1
enter a element ?
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&1
enter a element B
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&1
enter a element H
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&8
6 ? B H
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice& 6
1"QD=/S@GT
6"QDD@1@T@
8"QDD=S:1A>
?"@A=T
@nter ru choice&8
? B H
C
8) /* Write C++ program to implement the deEue -dou$le ended Eueue) ADT
using a dou$ly linked list */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
class node
%
pu$lic&
int data(
class node *ne't(
class node *pre3(
)(
class dEueue& pu$lic node
%
node *head+*tail(
int top1+top6(
pu$lic&
dEueue-)
%
top1.2(
top6.2(
head./011(
tail./011(
)
3oid push-int ')%
node *temp(
int ch(
i*-top1+top6 #.F)
%
cout !!4dEueue o3er*lo,4(
return (
)
i*- top1+top6 .. 2)
%
head . ne, node(
head5#data.'(
head5#ne't./011(
head5#pre3./011(
tail.head(
top1++(
)
else
%
cout !!4 Add element 1"I=GST 6"1AST9n enter ur choice&4(
cin ## ch(
H
i*-ch..1)
%
top1++(
temp.ne, node(
temp5#data.'(
temp5#ne't.head(
temp5#pre3./011(
head5#pre3.temp(
head.temp(
)
else
%
top6++(
temp.ne, node(
temp5#data.'(
temp5#ne't./011(
temp5#pre3.tail(
tail5#ne't.temp(
tail.temp(
)
)
)
3oid pop-)
%
int ch(
cout !!4Delete 1"Iirst /ode 6"1ast /ode9n @nter ur choice&4(
cin ##ch(
i*-top1 + top6 !.2)
%
cout !!49nDEueue under *lo,4(
return(
)
i*-ch..1)
%
head.head5#ne't(
head5#pre3./011(
top155(
)
else
%
top655(
tail.tail5#pre3(
tail5#ne't./011(
)
)
J
3oid display-)
%
int ch(
node *temp(
cout !!4display *rom 1"Staring 6"@nding9n @nter ur choice4(
cin ##ch(
i*-top1+top6 !.2)
%
cout !!4under *lo,4(
return (
)
i* -ch..1)
%
temp.head(
,hile-temp7./011)
%
cout !! temp5#data !!4 4(
temp.temp5#ne't(
)
)
else
%
temp.tail(
,hile- temp7./011)
%
cout !!temp5#data !! 4 4(
temp.temp5#pre3(
)
)
)
)(
main-)
%
dEueue d1(
int ch(
,hile -1)%
cout !!41"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T9n @nter ur choice&4(
cin ##ch(
s,itch-ch)
%
case 1& cout !!4enter element4(
cin ## ch(
d1"push-ch)( $reak(
case 6& d1"pop-)( $reak(
case 8& d1"display-)( $reak(
case ?& e'it-1)(
)
))
12
OUTPUT
1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&1
enter element?
1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&1
enter elementF
Add element 1"I=GST 6"1AST
enter ur choice&1
1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&1
enter elementB
Add element 1"I=GST 6"1AST
enter ur choice&6
1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&8
display *rom 1"Staring 6"@nding
@nter ur choice1
F ? B 1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&6
Delete 1"Iirst /ode 6"1ast /ode
@nter ur choice&1
1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&8
display *rom 1"Staring 6"@nding
@nter ur choice1
? B 1"=/S@GT 6"D@1@T@ 8"D=S:1A0 ?"@A=T
@nter ur choice&?
11
?)
/* Write a C++ program to per*orm the *ollo,ing operations&
a) =nsert an element into a $inary search tree"
$) Delete an element *rom a $inary search tree"
c) Search *or a key element in a $inary search tree" */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
3oid insert-int+int )(
3oid delte-int)(
3oid display-int)(
int search-int)(
int search1-int+int)(
int treeK?2L+t.1+s+'+i(
main-)
%
int ch+y(
*or-i.1(i!?2(i++)
treeKiL.51(
,hile-1)
%
cout !!41"=/S@GT9n6"D@1@T@9n8"D=S:1A>9n?"S@AGC;9nF"@A=T9n@nter
your choice&4(
cin ## ch(
s,itch-ch)
%
case 1&
cout !!4enter the element to insert4(
cin ## ch(
insert-1+ch)(
$reak(
case 6&
cout !!4enter the element to delete4(
cin ##'(
y.search-1)(
i*-y7.51) delte-y)(
else cout!!4no such element in tree4(
$reak(
case 8&
display-1)(
cout!!49n4(
*or-int i.2(i!.86(i++)
cout !!i(
cout !!49n4(
$reak(
16
case ?&
cout !!4enter the element to search&4(
cin ## '(
y.search-1)(
i*-y .. 51) cout !!4no such element in tree4(
else cout !!' !! 4is in4 !!y !!4position4(
$reak(
case F&
e'it-2)(
)
)
)
3oid insert-int s+int ch )
%
int '(
i*-t..1)
%
treeKt++L.ch(
return(
)
'.search1-s+ch)(
i*-treeK'L#ch)
treeK6*'L.ch(
else
treeK6*'+1L.ch(
t++(
)
3oid delte-int ')
%
i*- treeK6*'L..51 MM treeK6*'+1L..51)
treeK'L.51(
else i*-treeK6*'L..51)
% treeK'L.treeK6*'+1L(
treeK6*'+1L.51(
)
else i*-treeK6*'+1L..51)
% treeK'L.treeK6*'L(
treeK6*'L.51(
)
else
%
treeK'L.treeK6*'L(
delte-6*')(
)
t55(
)
18
int search-int s)
%
i*-t..1)
%
cout !!4no element in tree4(
return 51(
)
i*-treeKsL..51)
return treeKsL(
i*-treeKsL#')
search-6*s)(
else i*-treeKsL!')
search-6*s+1)(
else
return s(
)
3oid display-int s)
%
i*-t..1)
%cout !!4no element in tree&4(
return()
*or-int i.1(i!?2(i++)
i*-treeKiL..51)
cout !!4 4(
else cout !!treeKiL(
return (
)
int search1-int s+int ch)
%
i*-t..1)
%
cout !!4no element in tree4(
return 51(
)
i*-treeKsL..51)
return s/6(
i*-treeKsL # ch)
search1-6*s+ch)(
else search1-6*s+1+ch)(
)
1?
OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your co!ce"3
no e#e$ent !n tree"
%12345&'()%11121314151&1'1(1)2%21222324252&2'2(2)3%3132
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your co!ce"1
Enter te e#e$ent to !n*ert 1%
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your co!ce"4
Enter te e#e$ent to *e+rc" 1%
1% !* !n 1 ,o*!t!on
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your co!ce"5
1F
F)/* Write a C++ program to implement circular Eueue ADT using an array */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
class cEueue
%
int EKFL+*ront+rare(
pu$lic&
cEueue-)
%
*ront.51(
rare.51(
)
3oid push-int ')
%
i*-*ront ..51 MM rare .. 51)
%
EK++rareL.'(
*ront.rare(
return(
)
else i*-*ront .. -rare+1)NF )
%
cout !!4 Circular Queue o3er *lo,4(
return(
)
rare. -rare+1)NF(
EKrareL.'(
)
3oid pop-)
%
i*-*ront..51 MM rare.. 51)
%
cout !!4under *lo,4(
return(
)
else i*- *ront.. rare )
%
*ront.rare.51(
return(
)
*ront. -*ront+1)NF(
)

1B
3oid display-)
%
int i(
i*- *ront !. rare)
%
*or-i.*ront( i!.rare(i++)
cout !! EKiL!!4 4(
)
else
%
*or-i.*ront(i!.?(i++)
%
cout !!EKiL !! 4 4(
)
*or-i.2(i!.rare(i++)
%
cout !! EKiL!! 4 4(
)
)
)
)(
main-)
%
int ch(
cEueue E1(
,hile- 1)
%
cout!!49n1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T9n@nter ur choice4(
cin ## ch(
s,itch-ch)
%
case 1& cout!!4enter element4(
cin ## ch(
E1"push-ch)( $reak(
case 6& E1"pop-)( $reak(
case 8& E1"display-)( $reak(
case ?& e'it-2)(
)
)
)
1C
<0T:0T
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice1
enter element?
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice1
enter elementF
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice1
enter element8
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice8
? F 8
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice6
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice8
F 8
1"=/S@GT 6"D@1@T@ 8"D=S:1A> ?"@A=T
@nter ur choice?
1H
B)
/* Write a C++ program to implement all the *unctions o* a dictionary -ADT)
using hashing */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
de*ine ma' 12
typede* struct list
%
int data(
struct list *ne't(
)nodeDtype(
nodeDtype *ptrKma'L+*rootKma'L+*tempKma'L(

class Dictionary
%
pu$lic&
int inde'(
Dictionary-)(
3oid insert-int)(
3oid search-int)(
3oid deleteDele-int)(
)(
Dictionary&&Dictionary-)
%
inde'.51(
*or-int i.2(i!ma'(i++)
%
rootKiL./011(
ptrKiL./011(
tempKiL./011(
)
)
3oid Dictionary&&insert-int key)
%
inde'.int-keyNma')(
1J
ptrKinde'L.-nodeDtype*)malloc-siOeo*-nodeDtype))(
ptrKinde'L5#data.key(
i*-rootKinde'L../011)
%
rootKinde'L.ptrKinde'L(
rootKinde'L5#ne't./011(
tempKinde'L.ptrKinde'L(
)
else
%
tempKinde'L.rootKinde'L(
,hile-tempKinde'L5#ne't7./011)
tempKinde'L.tempKinde'L5#ne't(
tempKinde'L5#ne't.ptrKinde'L(
)
)
3oid Dictionary&&search-int key)
%
int *lag.2(
inde'.int-keyNma')(
tempKinde'L.rootKinde'L(
,hile-tempKinde'L7./011)
%
i*-tempKinde'L5#data..key)
%
cout!!49nSearch key is *ound774(
*lag.1(
$reak(
)
else tempKinde'L.tempKinde'L5#ne't(
)
i* -*lag..2)
cout!!49nsearch key not *ound"""""""4(
)
3oid Dictionary&&deleteDele-int key)
%
inde'.int-keyNma')(
tempKinde'L.rootKinde'L(
,hile-tempKinde'L5#data7.key MM tempKinde'L7./011)
%
ptrKinde'L.tempKinde'L(
tempKinde'L.tempKinde'L5#ne't(
)
ptrKinde'L5#ne't.tempKinde'L5#ne't(
cout!!49n4!!tempKinde'L5#data!!4 has $een deleted"4(
tempKinde'L5#data.51(
tempKinde'L./011(
62
*ree-tempKinde'L)(
)
main-)
%
int 3al+ch+n+num(
char c(
Dictionary d(
do
%
cout!!49nP@/0&9n1"Create4(
cout!!49n6"Search *or a 3alue9n8"Delete an 3alue4(
cout!!49n@nter your choice&4(
cin##ch(
s,itch-ch)
%
case 1&cout!!49n@nter the num$er o* elements to $e inserted&4(
cin##n(
cout!!49n@nter the elements to $e inserted&4(
*or-int i.2(i!n(i++)
%
cin##num(
d"insert-num)(
)
$reak(
case 6&cout!!49n@nter the element to $e searched&4(
cin##n(
d"search-n)(
case 8&cout!!49n@nter the element to $e deleted&4(
cin##n(
d"deleteDele-n)(
$reak(
de*ault&cout!!49n=n3alid choice""""4(
)
cout!!49n@nter y to continue""""""4(
cin##c(
),hile-c..QyQ)(
getch-)(
)
OUTPUT
-ENU"
1.Cre+te
2.Se+rc .or + /+#ue
3.De#ete +n /+#ue
Enter your co!ce"1
Enter te nu$0er o. e#e$ent* to 0e !n*erte1"(
Enter te e#e$ent* to 0e !n*erte1"1% 4 5 ( ' 12 & 1
61
Enter y to cont!nue......y
-ENU"
1.Cre+te
2.Se+rc .or + /+#ue
3.De#ete +n /+#ue
Enter your co!ce"2
Enter te e#e$ent to 0e *e+rce1"12
Se+rc 2ey !* .oun133
Enter te e#e$ent to 0e 1e#ete1"1
1 +* 0een 1e#ete1.
Enter y to cont!nue......y
66
H) /* Write C++ programs *or the implementation o* RIS *or a gi3en graph */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
int costK12LK12L+i+S+k+n+EuK12L+*ront+rare+3+3isitK12L+3isitedK12L(
main-)
%
int m(
cout !!4enterno o* 3ertices4(
cin ## n(
cout !!4ente no o* edges4(
cin ## m(
cout !!49n@DT@S 9n4(
*or-k.1(k!.m(k++)
%
cin ##i##S(
costKiLKSL.1(
)
cout !!4enter initial 3erte'4(
cin ##3(
cout !!4Uisitied 3ertices9n4(
cout !! 3(
3isitedK3L.1(
k.1(
,hile-k!n)
%
*or-S.1(S!.n(S++)
i*-costK3LKSL7.2 MM 3isitedKSL7.1 MM 3isitKSL7.1)
%
3isitKSL.1(
EuKrare++L.S(
)
3.EuK*ront++L(
cout!!3 !! 4 4(
k++(
3isitK3L.2( 3isitedK3L.1(
)
)
68
<0T:0T
enterno o* 3erticesJ
ente no o* edgesJ
@DT@S
1 6
6 8
1 F
1 ?
? C
C H
H J
6 B
F C
enter initial 3erte'1
Uisited 3ertices
16 ? F 8 B C H J
6?
J)/* Write C++ programs *or the implementation o* DIS *or a gi3en graph */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
int costK12LK12L+i+S+k+n+stkK12L+top+3+3isitK12L+3isitedK12L(
main-)
%
int m(
cout !!4enterno o* 3ertices4(
cin ## n(
cout !!4ente no o* edges4(
cin ## m(
cout !!49n@DT@S 9n4(
*or-k.1(k!.m(k++)
%
cin ##i##S(
costKiLKSL.1(
)
cout !!4enter initial 3erte'4(
cin ##3(
cout !!4<GD@G <I U=S=T@D U@GT=C@S4(
cout !! 3 !!4 4(
3isitedK3L.1(
k.1(
,hile-k!n)
%
*or-S.n(S#.1(S55)
i*-costK3LKSL7.2 MM 3isitedKSL7.1 MM 3isitKSL7.1)
%
3isitKSL.1(
stkKtopL.S(
top++(
)
3.stkK55topL(
cout!!3 !! 4 4(
k++(
3isitK3L.2( 3isitedK3L.1(
)
)
<0T:0T
enterno o* 3erticesJ
ente no o* edgesJ
@DT@S
1 6
6 8
6 B
1 F
6F
1 ?
? C
F C
C H
H J
enter initial 3erte'1
<GD@G <I U=S=T@D U@GT=C@S1 6 8 B ? C H J F
12)
/* Write C++ programs to implement the :rimVs algorithm to generate a minimum
cost spanning tree */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
int costK12LK12L+i+S+k+n+stkK12L+top+3+3isitK12L+3isitedK12L+u(
main-)
%
int m+c(
cout !!4enterno o* 3ertices4(
cin ## n(
cout !!4ente no o* edges4(
cin ## m(
cout !!49n@DT@S Cost9n4(
*or-k.1(k!.m(k++)
%
cin ##i##S##c(
costKiLKSL.c(
)
*or-i.1(i!.n(i++)
*or-S.1(S!.n(S++)
i*-costKiLKSL..2)
costKiLKSL.81JJJ(
cout !!4<GD@G <I U=S=T@D U@GT=C@S4(
k.1(
,hile-k!n)
%
m.81JJJ(
i*-k..1)
%
*or-i.1(i!.n(i++)
*or-S.1(S!.m(S++)
i*-costKiLKSL!m)
%
m.costKiLKSL(
u.i(
)
6B
)
else
%
*or-S.n(S#.1(S55)
i*-costK3LKSL!m MM 3isitedKSL7.1 MM 3isitKSL7.1)
%
3isitKSL.1(
stkKtopL.S(
top++(
m.costK3LKSL(
u.S(
)
)
costK3LKuL.81JJJ(
3.u(
cout!!3 !! 4 4(
k++(
3isitK3L.2( 3isitedK3L.1(
)
)
OUTPUT
enterno o. /ert!ce*'
ente no o. e14e*)
ED5ES Co*t
1 & 1%
& 5 25
5 4 22
4 3 12
3 2 1&
2 ' 14
5 ' 24
4 ' 1(
1 2 2(
ORDER O6 7ISITED 7ERTICES1 & 5 4 3 2
6C
11)
/* Write C++ programs to implement the WrushalVs algorithm to generate a
minimum cost spanning tree */
include!iostream#
include!conio"h#
include!stdli$"h#
using namespace std(
int costK12LK12L+i+S+k+n+m+c+3isit+3isitedK12L+l+3+count+count1+3st+p(
main-)
%
int dup1+dup6(
cout!!4enter no o* 3ertices4(
cin ## n(
cout !!4enter no o* edges4(
cin ##m(
cout !!4@DT@ Cost4(
*or-k.1(k!.m(k++)
%
cin ##i ##S ##c(
costKiLKSL.c(
costKSLKiL.c(
)
*or-i.1(i!.n(i++)
*or-S.1(S!.n(S++)
i*-costKiLKSL..2)
costKiLKSL.81JJJ(
3isit.1(
,hile-3isit!n)
%
3.81JJJ(
*or-i.1(i!.n(i++)
*or-S.1(S!.n(S++)
i*-costKiLKSL7.81JJJ MM costKiLKSL!3 MM costKiLKSL7.51 )
%
int count .2(
*or-p.1(p!.n(p++)
%
i*-3isitedKpL..i XX 3isitedKpL..S)
count++(
)
i*-count #. 6)
%
*or-p.1(p!.n(p++)
i*-costKiLKpL7.81JJJ MM p7.S)
dup1.p(
*or-p.1(p!.n(p++)
i*-costKSLKpL7.81JJJ MM p7.i)
dup6.p(
6H
i*-costKdup1LKdup6L..51)
continue(
)
l.i(
k.S(
3.costKiLKSL(
)
cout !!4edge *rom 4 !!l !!455#4!!k(
costKlLKkL.51(
costKkLKlL.51(
3isit++(
int count.2(
count1.2(
*or-i.1(i!.n(i++)
%
i*-3isitedKiL..l)
count++(
i*-3isitedKiL..k)
count1++(
)
i*-count..2)
3isitedK++3stL.l(
i*-count1..2)
3isitedK++3stL.k(
)
)
OUTPUT
enter no o. /ert!ce*4
enter no o. e14e*4
ED5E Co*t
1 2 1
2 3 2
3 4 3
1 3 3
e14e .ro$ 18892e14e .ro$ 28893e14e .ro$ 18893
6J
12:
/* Write a C++ program to sol3e the single source shortest path pro$lem" -/ote&
0se DiSkstraVs algorithm) */
include!iostream#
include!conio"h#
include!stdio"h#
using namespace std(
int shortest-int +int)(
int costK12LK12L+distK62L+i+S+n+k+m+SK62L+3+totcost+pathK62L+p(
main-)
%
int c(
cout !!4enter no o* 3ertices4(
cin ## n(
cout !!4enter no o* edges4(
cin ##m(
cout !!49nenter9n@DT@ Cost9n4(
*or-k.1(k!.m(k++)
%
cin ## i ## S ##c(
costKiLKSL.c(
)
*or-i.1(i!.n(i++)
*or-S.1(S!.n(S++)
i*-costKiLKSL..2)
costKiLKSL.81JJJ(
cout !!4enter initial 3erte'4(
cin ##3(
cout !! 3!!49n4(
shortest-3+n)(
)
int shortest-int 3+int n)
%
int min(
*or-i.1(i!.n(i++)
%
SKiL.2(
distKiL.costK3LKiL(
)
pathK++pL.3(
SK3L.1(
distK3L.2(
*or-i.6(i!.n51(i++)
%
k.51(
min.81JJJ(
82
*or-S.1(S!.n(S++)
%
i*-distKSL!min MM SKSL7.1)
%
min.distKSL(
k.S(
)
)
i*-costK3LKkL!.distKkL)
p.1(
pathK++pL.k(
*or-S.1(S!.p(S++)
cout!!pathKSL(
cout !!49n4(
//cout !!k(
SKkL.1(
*or-S.1(S!.n(S++)
i*-costKkLKSL7.81JJJ MM distKSL#.distKkL+costKkLKSL MM SKSL7.1)
distKSL.distKkL+costKkLKSL(
)
)
OUTPUT
enter no o. /ert!ce*&
enter no o. e14e*11
enter
ED5E Co*t
1 2 5%
1 3 45
1 4 1%
2 3 1%
2 4 15
3 5 3%
4 1 1%
4 5 15
5 2 2%
5 3 35
& 5 3
enter !n!t!+# /erte;1
1
14
145
1452
13
81
1F)
/* Write C++ programs *or sorting a gi3en list o* elements in ascending order
using Quick sort sorting methods */
<!nc#u1e=!o*tre+$.9
<!nc#u1e=con!o.9
!nt +>1%?@#@u@!@AB
/o!1 Cu!c2D!nt E@!nt@!nt:B
/o!1 $+!nD:
F
c#r*crD:B
cout ==Genter 1% e#e$ent*GB
.orD!H%B!=1%B!II:
c!n 99 +>!?B
#H%B
uH)B
Cu!c2D+@#@u:B
cout ==G*orte1 e#e$ent*GB
.orD!H%B!=1%B!II:
cout == +>!? == G GB
4etcD:B
J
/o!1 Cu!c2D!nt +>?@!nt #@!nt u:
F
!nt ,@te$,B
!.D#=u:
F
,H+>#?B
!H#B
AHuB
K!#eD!=A:
F
K!#eD+>!? =H , LL !=A :
!IIB
K!#eD+>A?9, LL !=HA :
A88B
!.D!=HA:
F
te$,H+>!?B
+>!?H+>A?B
+>A?Hte$,BJ
J
te$,H+>A?B
+>A?H+>#?B
+>#?Hte$,B
cout ==GMnGB
.orD!H%B!=1%B!II:
cout ==+>!?==G GB
Cu!c2D+@#@A81:B
86
Cu!c2D+@AI1@u:B JJ
OUTPUT
enter 1% e#e$ent*5 2 3 1& 25 1 2% ' ( &1 14
1 2 3 5 25 1& 2% ' ( &1
1 2 3 5 25 1& 2% ' ( &1
1 2 3 5 25 1& 2% ' ( &1
1 2 3 5 25 1& 2% ' ( &1
1 2 3 5 25 1& 2% ' ( &1
1 2 3 5 ( 1& 2% ' 25 &1
1 2 3 5 ' ( 2% 1& 25 &1
1 2 3 5 ' ( 1& 2% 25 &1
1 2 3 5 ' ( 1& 2% 25 &1 *orte1 e#e$ent*1 2 3 5 ' ( 1& 2% 25 &1
1B)
/* Write C++ programs *or sorting a gi3en list o* elements in ascending order
using Perge sort the *ollo,ing sorting methods */
include!iostream#
include!conio"h#
using namespace std(
3oid mergesort-int *+int+int)(
3oid merge-int *+int+int+int)(
int aK62L+i+n+$K62L(
main-)
%
cout !!49n enter no o* elements4(
cin ## n(
cout !!4enter the elements4(
*or-i.2(i!n(i++)
cin ## aKiL(
mergesort-a+2+n51)(
cout !!4 num$ers a*ter sort4(
*or-i.2(i!n(i++)
cout !! aKiL !! 4 4(
getch-)(
)
3oid mergesort-int aKL+int i+int S)
%
int mid(
i*-i!S)
%
mid.-i+S)/6(
mergesort-a+i+mid)(
mergesort-a+mid+1+S)(
merge-a+i+mid+S)(
)
88
)
3oid merge-int aKL+int lo,+int mid +int high)
%
int h+i+S+k(
h.lo,(
i.lo,(
S.mid+1(
,hile-h!.mid MM S!.high)
%
i*-aKhL!.aKSL)
$KiL.aKh++L(
else
$KiL.aKS++L(
i++(
)
i*- h # mid)
*or-k.S(k!.high(k++)
$Ki++L.aKkL(
else
*or-k.h(k!.mid(k++)
$Ki++L.aKkL(
cout !!49n4(
*or-k.lo,(k!.high(k++)
% aKkL.$KkL(
cout !! aKkL !!4 4(
)
)
OUTPUT
N enter no o. e#e$ent*( 12 5 &1 &% 5% 1 '% (1
enter te e#e$ent*
5 12
&% &1
5 12 &% &1
1 5%
'% (1
1 5% '% (1
1 5 12 5% &% &1 '% (1 nu$0er* +.ter *ort1 5 12 5% &% &1 '% (1
8?
1H)
/* Write a C++ program that uses dynamic programming algorithm to sol3e the
optimal $inary search tree pro$lem */
include!iostream#
include!conio"h#
include!stdio"h#
using namespace std(
de*ine PAA 12
int *ind-int i+int S)(
3oid print-int+int)(
int pKPAAL+EKPAAL+,K12LK12L+cK12LK12L+rK12LK12L+i+S+k+n+m(
char idntKCLK12L(
main-)
%
cout !! 4enter the no+ o* identi*iers4(
cin ##n(
cout !!4enter identi*iers4(
*or-i.1(i!.n(i++)
gets-idntKiL)(
cout !!4enter success propa$ility *or identi*iers4(
*or-i.1(i!.n(i++)
cin ##pKiL(
cout !! 4enter *ailure propa$ility *or identi*iers4(
*or-i.2(i!.n(i++)
cin ## EKiL(
*or-i.2(i!.n(i++)
%
,KiLKiL.EKiL(
cKiLKiL.rKiLKiL.2(
,KiLKi+1L.EKiL+EKi+1L+pKi+1L(
rKiLKi+1L.i+1(
cKiLKi+1L.EKiL+EKi+1L+pKi+1L(
)
,KnLKnL.EKnL(
rKnLKnL.cKnLKnL.2(
*or-m.6(m!.n(m++)
%
*or-i.2(i!.n5m(i++)
%
S.i+m(
,KiLKSL.,KiLKS51L+pKSL+EKSL(
k.*ind-i+S)(
rKiLKSL.k(
cKiLKSL.,KiLKSL+cKiLKk51L+cKkLKSL(
)
)
cout !!49n4(
print-2+n)( )
8F
int *ind-int i+int S)
%
int min.6222+m+l(
*or-m.i+1(m!.S(m++)
i*-cKiLKm51L+cKmLKSL!min)
%
min.cKiLKm51L+cKmLKSL(
l.m(
)
return l(
)
3oid print-int i+int S)
%
i*-i!S)
puts-idntKrKiLKSLL)(
else
return(
print-i+rKiLKSL51)(
print-rKiLKSL+S)(
)
OUTPUT
enter te no@ o. !1ent!.!er*4
enter !1ent!.!er*1o
!.
!nt
K!#e
enter *ucce** ,ro,+0!#!ty .or !1ent!.!er*3 3 1 1
enter .+!#ure ,ro,+0!#!ty .or !1ent!.!er*2 3 1 1 1

You might also like