7/23/2019 DataStructure Programs
1/53
1. DataStructure-Program that implements depth first
search algorithm
#include
#include #include
#defne TRUE 1
#defne FALSE0
#defne MAX 8
stuct node
!
int data"
stuct node ne$t "
% "
int &isited'MAX( "
&oid d)s * int+ stuct node , "
stuct node -etnode/ite * int , "
&oid del * stuct node , "
&oid ain* ,
!
stuct node a'MAX( "
stuct node &1+ &+ &2+ &3 "
int i "
clsc* , "
&1 4 -etnode/ite * , "
a'0( 4 &1 "&1 5> ne$t 4 & 4 -etnode/ite * 2 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 1 , "
a'1( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 3 , "
& 5> ne$t 4 &2 4 -etnode/ite * 7 , "
&2 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 1 , "
a'( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * , "
& 5> ne$t 4 &2 4 -etnode/ite * 9 , "
&2 5> ne$t 4 6ULL "
&1 4 -etnode/ite * , "
a'2( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * , "
http://www.technicalsymposium.com/Program_that_implements_depth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/Program_that_implements_depth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/Program_that_implements_depth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/Program_that_implements_depth_first_search_algorithm.html7/23/2019 DataStructure Programs
2/53
a'3( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 2 , "
a'7( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 2 , "
a'( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 3 , "
a'9( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 7 , "
& 5> ne$t 4 &2 4 -etnode/ite * , "
&2 5> ne$t 4 &3 4 -etnode/ite * 9 , "
&3 5> ne$t 4 6ULL "
d)s * 1+ a , "
)o * i 4 0 " i < MAX " i:: ,
del * a'i( , "
-etch* , "
%
&oid d)s * int &+ stuct node ; ,
!
stuct node "
&isited'& 5 1( 4 TRUE "
;int) * =d?t=+ & , " 4 * ; : & 5 1 , "
/hile * @4 6ULL ,
!
i) * &isited' 5> data 5 1( 44 FALSE ,
d)s * 5> data+ ; , "
else
4 5> ne$t "
%
%
stuct node -etnode/ite * int &al ,!
stuct node ne/node "
ne/node 4 * stuct node , alloc * sieo) * stuct node , , "
ne/node 5> data 4 &al "
etun ne/node "
%
&oid del * stuct node n ,
7/23/2019 DataStructure Programs
3/53
!
stuct node te; "
/hile * n @4 6ULL ,
!
te; 4 n 5> ne$t "
)ee * n , "n 4 te; "
%
%
2. DataStructure-breadth first search algorithm
#include
#include
#include
#defne TRUE 1#defne FALSE0
#defne MAX 8
stuct node
!
int data"
stuct node ne$t "
% "
int &isited'MAX( "
int '8( "
int )ont+ ea "
&oid B)s * int+ stuct node , "stuct node -etnode/ite * int , "
&oid addueue * int , "
int deleteueue* , "
int ise;tC* , "
&oid del * stuct node , "
&oid ain* ,
!
stuct node a'MAX( "
stuct node &1+ &+ &2+ &3 "
int i "clsc* , "
&1 4 -etnode/ite * , "
a'0( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 2 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 1 , "
a'1( 4 &1 "
http://www.technicalsymposium.com/breadth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/breadth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/breadth_first_search_algorithm.htmlhttp://www.technicalsymposium.com/breadth_first_search_algorithm.html7/23/2019 DataStructure Programs
4/53
&1 5> ne$t 4 & 4 -etnode/ite * 3 , "
& 5> ne$t 4 &2 4 -etnode/ite * 7 , "
&2 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 1 , "
a'( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * , "& 5> ne$t 4 &2 4 -etnode/ite * 9 , "
&2 5> ne$t 4 6ULL "
&1 4 -etnode/ite * , "
a'2( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * , "
a'3( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 2 , "
a'7( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 2 , "
a'( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 8 , "
& 5> ne$t 4 6ULL "
&1 4 -etnode/ite * 3 , "
a'9( 4 &1 "
&1 5> ne$t 4 & 4 -etnode/ite * 7 , "& 5> ne$t 4 &2 4 -etnode/ite * , "
&2 5> ne$t 4 &3 4 -etnode/ite * 9 , "
&3 5> ne$t 4 6ULL "
)ont 4 ea 4 51 "
B)s * 1+ a , "
)o * i 4 0 " i < MAX " i:: ,
del * a'i( , "
-etch* , "
%
&oid B)s * int &+ stuct node ; ,!
stuct node u "
&isited'& 5 1( 4 TRUE "
;int) * =d?t=+ & , "
addueue * & , "
/hile * ise;tC* , 44 FALSE ,
!
7/23/2019 DataStructure Programs
5/53
& 4 deleteueue* , "
u 4 * ; : & 5 1 , "
/hile * u @4 6ULL ,
!
i) * &isited ' u 5> data 5 1 ( 44 FALSE ,
!addueue * u 5> data , "
&isited ' u 5> data 5 1 ( 4 TRUE "
;int) * =d?t=+ u 5> data , "
%
u 4 u 5> ne$t "
%
%
%
stuct node -etnode/ite * int &al ,
!
stuct node ne/node "
ne/node 4 * stuct node , alloc * sieo) * stuct node , , "
ne/node 5> data 4 &al "
etun ne/node "
%
&oid addueue * int &ete$ ,
!
i) * ea 44 MAX 5 1 ,
!
;int) * =?nDueue &eo/.= , "
e$it* , "%
ea:: "
'ea( 4 &ete$ "
i) * )ont 44 51 ,
)ont 4 0 "
%
int deleteueue* ,
!
int data "
i) * )ont 44 51 ,!
;int) * =?nDueue Undeo/.= , "
e$it* , "
%
data 4 ')ont( "
i) * )ont 44 ea ,
)ont 4 ea 4 51 "
7/23/2019 DataStructure Programs
6/53
else
)ont:: "
etun data "
%
int ise;tC* ,
!i) * )ont 44 51 ,
etun TRUE "
etun FALSE "
%
&oid del * stuct node n ,
!
stuct node te; "
/hile * n @4 6ULL ,
!
te; 4 n 5> ne$t " )ee * n , "
n 4 te; "
%
%
3. DataStructure-minimum cost of a spanning tree
#include
#include
#include
stuct lled-e
!int &1+ & "
oat cost "
stuct lled-e ne$t "
% "
int stee'7( "
int count'7( "
int incost "
stuct lled-e Ginstee * stuct lled-e + int , "
int -et&al * int , "
&oid coBine * int+ int , "&oid del * stuct lled-e , "
&oid ain* ,
!
stuct lled-e te;+ oot "
int i "
clsc* , "
oot 4 * stuct lled-e , alloc * sieo) * stuct lled-e , , "
7/23/2019 DataStructure Programs
7/53
oot 5> &1 4 3 "
oot 5> & 4 2 "
oot 5> cost 4 1 "
te; 4 oot 5> ne$t 4 * stuct lled-e , alloc * sieo) * stuct lled-e , , "
te; 5> &1 4 3 "
te; 5> & 4 "te; 5> cost 4 "
te; 5> ne$t 4 * stuct lled-e , alloc * sieo) * stuct lled-e , , "
te; 4 te; 5> ne$t "
te; 5> &1 4 2 "
te; 5> & 4 "
te; 5> cost 4 2 "
te; 5> ne$t 4 * stuct lled-e , alloc * sieo) * stuct lled-e , , "
te; 4 te; 5> ne$t "
te; 5> &1 4 3 "
te; 5> & 4 1 "
te; 5> cost 4 3 "
te; 5> ne$t 4 6ULL "
oot 4 Ginstee * oot+ 7 , "
)o * i 4 1 " i
7/23/2019 DataStructure Programs
8/53
noo)ed-es:: "
incost :4 ; 5> cost "
i) * te; 44 6ULL ,
!
te; 4 ; "
4 te; "%
else
!
5> ne$t 4 ; "
4 5> ne$t "
%
5> ne$t 4 6ULL "
%
%
etun te; "
%
int -et&al * int i ,
!
int I+ G+ te; "
G 4 i "
/hile * stee'G( @4 G ,
G 4 stee'G( "
I 4 i "
/hile * I @4 G ,
!
te; 4 stee'I( "stee'I( 4 G "
I 4 te; "
%
etun G "
%
&oid coBine * int i+ int I ,
!
i) * count'i( < count'I( ,
stee'i( 4 I "
else!
stee'I( 4 i "
i) * count'i( 44 count'I( ,
count'I(:: "
%
%
&oid del * stuct lled-e oot ,
7/23/2019 DataStructure Programs
9/53
!
stuct lled-e te; "
/hile * oot @4 6ULL ,
!
te; 4 oot 5> ne$t "
)ee * oot , "oot 4 te; "
%
%
3. DataStructure-find the shortest path
#include
#include
#defne J6F KKKK
&oid ain* ,!
int a'3('3( "
int cost'3('3( 4 !
9+ 7+ 0+ 0+
9+ 0+ 0+ +
0+ 2+ 0+ 0+
3+ 0+ 1+ 0
% "
int i+ I+ G+ n 4 3 "
clsc* , "
)o * i 4 0 " i < n " i:: ,!
)o * I 4 0" I < n " I:: ,
!
i) * cost'i('I( 44 0 ,
a'i('I( 4 J6F "
else
a'i('I( 4 cost'i('I( "
%
%
;int) * =AdIacencC ati$ o) cost o) ed-es?n= , ")o * i 4 0 " i < n " i:: ,
!
)o * I 4 0" I < n " I:: ,
;int) * =d?t=+ a'i('I( , "
;int) * =?n= , "
%
)o * G 4 0 " G < n " G:: ,
7/23/2019 DataStructure Programs
10/53
!
)o * i 4 0 " i < n " i:: ,
!
)o * I 4 0 " I < n " I:: ,
!
i) * a'i('I( > a'i('G( : a'G('I( ,a'i('I( 4 a'i('G( : a'G('I("
%
%
%
;int) * =?nAdIacencC ati$ o) lo/est cost Bet/een the &etices?n= , "
)o * i 4 0 " i < n " i:: ,
!
)o * I 4 0" I < n " I:: ,
;int) * =d?t=+ a'i('I( , "
;int) * =?n= , "
%
-etch* , "
%
5. DataStructure-Bubble sort
#include
#include
&oid ain* ,
!
int a'7( 4 ! 7+ 19+ 21+ 12+ % "int i+ I+ te; "
clsc* , "
;int) * =uBBle sot.?n= , "
;int) * =?nAaC Be)oe sotin-?n=, "
)o * i 4 0 " i
7/23/2019 DataStructure Programs
11/53
%
;int) * =?n?nAaC a)te sotin-?n=, "
)o * i 4 0 " i a'I( ,
!
te; 4 a'i( "a'i( 4 a'I( "
a'I( 4 te; "
%
%
%
;int) * =?n?nAaC a)te sotin-?n=, "
)o * i 4 0 " i
7/23/2019 DataStructure Programs
12/53
!
int a'10( 4 ! 11+ + K+ 12+ 79+ 7+ 19+ 1+ K0+ 2 % "
int i "
&oid uicGsot * int + int+ int , "
clsc* , "
;int) * =DuicG sot.?n= , ";int) * =?nAaC Be)oe sotin-?n=, "
)o * i 4 0 " i
7/23/2019 DataStructure Programs
13/53
7/23/2019 DataStructure Programs
14/53
7/23/2019 DataStructure Programs
15/53
)o * i 4 1 " i < n " i:: ,
!
&al 4 $'i( "
s 4 i "
) 4 * s 5 1 , O "
/hile * s > 0 HH $')( < &al ,!
$'s( 4 $')( "
s 4 ) "
) 4 * s 5 1 , O "
%
$'s( 4 &al "
%
%
&oid hea;sot * int $' (+ int n ,
!
int i+ s+ )+ i&alue "
)o * i 4 n 5 1 " i > 0 " i55 ,
!
i&alue 4 $'i( "
$'i( 4 $'0( "
) 4 0 "
i) * i 44 1 ,
s 4 51 "
else
s 4 1 "
i) * i > HH $'( > $'1( ,s 4 "
/hile * s >4 0 HH i&alue < $'s( ,
!
$')( 4 $'s( "
) 4 s "
s 4 ) : 1 "
i) * s : 1
7/23/2019 DataStructure Programs
16/53
1'. DataStructure-(erge Sort
#include
#include
&oid ain* ,
!
int a'7( 4 ! 11+ + K+ 12+ 79 % "
int B'7( 4 ! 7+ 19+ 1+ K0+ 2 % "
int c'10( "
int i+ I+ G+ te; "
clsc* , "
;int) * =Me-e sot.?n= , "
;int) * =?nFist aaC?n= , "
)o * i 4 0 " i
7/23/2019 DataStructure Programs
17/53
else
c'i::( 4 B'G::( "
i) * I 44 7 PP G 44 7 ,
BeaG "
%
)o * " I
7/23/2019 DataStructure Programs
18/53
inset * HBt+ nu , "
%
;int) * =?nJn5ode Ta&esal = , "
inode * Bt , "
;int) * =?nQe5ode Ta&esal = , "
;eode * Bt , ";int) * =?nQost5ode Ta&esal = , "
;ostode * Bt , "
%
O insets a ne/ node in a BinaC seachtee O
&oid inset * stuct Bteenode s+ int nu ,
!
i) * s 44 6ULL ,
!
s 4 alloc * sieo) * stuct Bteenode , , "
* s , 5> le)tchild 4 6ULL "
* s , 5> data 4 nu "
* s , 5> i-htchild 4 6ULL "
etun "
%
else O seach the node to /hich ne/ node /ill Be attached O
!
O i) ne/ data is less+ ta&ese to le)t O
i) * nu < * s , 5> data ,
inset * H* * s , 5> le)tchild ,+ nu , "
else
O else ta&ese to i-ht Oinset * H* * s , 5> i-htchild ,+ nu , "
%
etun "
%
O ta&ese a BinaC seach tee in a LR *Le)t5ata5Ri-ht, )ashionO
&oid inode * stuct Bteenode s ,
!
i) * s @4 6ULL ,
!
inode * s 5> le)tchild , "O ;int the data o) the node /hose le)tchild is 6ULL o the ;ath has aleadC Been
ta&esed O
;int) * =?td=+ s 5> data , "
inode * s 5> i-htchild , "
%
else
etun "
http://www.technicalsymposium.com/Program_to_implement_a_binary_search_tree.htmlhttp://www.technicalsymposium.com/Program_to_implement_a_binary_search_tree.htmlhttp://www.technicalsymposium.com/Program_to_implement_a_binary_search_tree.htmlhttp://www.technicalsymposium.com/Program_to_implement_a_binary_search_tree.html7/23/2019 DataStructure Programs
19/53
%
O ta&ese a BinaC seach tee in a LR *ata5Le)t5i-ht, )ashion O
&oid ;eode * stuct Bteenode s ,
!
i) * s @4 6ULL ,
!O ;int the data o) a node O
;int) * =?td=+ s 5> data , "
O ta&ese till le)tchild is not 6ULL O
;eode * s 5> le)tchild , "
O ta&ese till i-htchild is not 6ULL O
;eode * s 5> i-htchild , "
%
else
etun "
%
O ta&ese a BinaC seach tee in LR *Le)t5Ri-ht5ata, )ashion O
&oid ;ostode * stuct Bteenode s ,
!
i) * s @4 6ULL ,
!
;ostode * s 5> le)tchild , "
;ostode * s 5> i-htchild , "
;int) * =?td=+ s 5> data , "
%
else
etun "%
12. DataStructure-Program to con)ert an *nfi+
e+pression to Prefi+ form
#include
#include
#include
#include
#defne MAX 70stuct inf$
!
cha ta-et'MAX( "
cha stacG'MAX( "
cha s+ t "
int to;+ l "
7/23/2019 DataStructure Programs
20/53
% "
&oid initinf$ * stuct inf$ , "
&oid sete$; * stuct inf$ + cha , "
&oid ;ush * stuct inf$ + cha , "
cha ;o;* stuct inf$ , "
&oid con&et * stuct inf$ , "int ;ioitC * cha c , "
&oid sho/ * stuct inf$ , "
&oid ain* ,
!
stuct inf$ "
cha e$;'MAX( "
clsc* , "
initinf$ * H , "
;int) * =?nEnte an e$;ession in inf$ )o = , "
-ets * e$; , "
sete$; * H+ e$; , "
con&et * H , "
;int) * =The Qef$ e$;ession is = , "
sho/ * , "
-etch* , "
%
O initialies eleents o) stuctue &aiaBle O
&oid initinf$ * stuct inf$ ; ,
!
; 5> to; 4 51 "
stc;C * ; 5> ta-et+ == , "stc;C * ; 5> stacG+ == , "
; 5> l 4 0 "
%
O e&eses the -i&en e$;ession O
&oid sete$; * stuct inf$ ;+ cha st ,
!
; 5> s 4 st "
ste& * ; 5> s , "
; 5> l 4 stlen * ; 5> s , "
* ; 5> ta-et : ; 5> l , 4 ?0 "; 5> t 4 ; 5> ta-et : * ; 5> l 5 1 , "
%
O adds o;eato to the stacG O
&oid ;ush * stuct inf$ ;+ cha c ,
!
i) * ; 5> to; 44 MAX 5 1 ,
;int) * =?nStacG is )ull.?n= , "
http://www.technicalsymposium.com/Program_to_convert_an_Infix_expression_to_Prefix_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_an_Infix_expression_to_Prefix_form.html7/23/2019 DataStructure Programs
21/53
else
!
; 5> to;:: "
; 5> stacG'; 5> to;( 4 c "
%
%O ;o;s an o;eato )o the stacG O
cha ;o; * stuct inf$ ; ,
!
i) * ; 5> to; 44 51 ,
!
;int) * =StacG is e;tC?n= , "
etun 51 "
%
else
!
cha ite 4 ; 5> stacG'; 5> to;( "
; 5> to;55 "
etun ite "
%
%
O con&etsthe inf$ e$;. to ;ef$ )o O
&oid con&et * stuct inf$ ; ,
!
cha o; "
/hile * * ; 5> s , ,
!i) * * ; 5> s , 44 PP * ; 5> s , 44 ?t ,
!
; 5> s:: "
continue "
%
i) * isdi-it * * ; 5> s , , PP isal;ha * * ; 5> s , , ,
!
/hile * isdi-it * * ; 5> s , , PP isal;ha * * ; 5> s , , ,
!
* ; 5> t , 4 * ; 5> s , "; 5> s:: "
; 5> t55 "
%
%
i) * * ; 5> s , 44 , ,
!
;ush * ;+ * ; 5> s , , "
http://www.technicalsymposium.com/Program_to_convert_an_Infix_expression_to_Prefix_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_an_Infix_expression_to_Prefix_form.html7/23/2019 DataStructure Programs
22/53
; 5> s:: "
%
i) * * ; 5> s , 44 PP * ; 5> s , 44 : PP * ; 5> s , 44 O PP * ; 5> s , 44
PP * ; 5> s , 44 5 PP * ; 5> s , 44 ,
!
i) * ; 5> to; @4 51 ,!
o; 4 ;o; * ; , "
/hile * ;ioitC * o; , > ;ioitC * * ; 5> s , , ,
!
* ; 5> t , 4 o; "
; 5> t55 "
o; 4 ;o; * ; , "
%
;ush * ;+ o; , "
;ush * ;+ * ; 5> s , , "
%
else
;ush * ;+ * ; 5> s , , "
; 5> s:: "
%
i) * * ; 5> s , 44 * ,
!
o; 4 ;o; * ; , "
/hile * o; @4 , ,
!
* ; 5> t , 4 o; "; 5> t55 "
o; 4 ;o; * ; , "
%
; 5> s:: "
%
%
/hile * ; 5> to; @4 51 ,
!
o; 4 ;o; * ; , "
* ; 5> t , 4 o; "; 5> t55 "
%
; 5> t:: "
%
O etuns the ;iotitC o) the o;eato O
int ;ioitC * cha c ,
!
7/23/2019 DataStructure Programs
23/53
i) * c 44 ,
etun 2 "
i) * c 44 PP c 44 O PP c 44 ,
etun "
else
!i) * c 44 : PP c 44 5 ,
etun 1 "
else
etun 0 "
%
%
O dis;laCs the ;ef$ )o o) -i&en e$;. O
&oid sho/ * stuct inf$ ; ,
!
/hile * * ;.t , ,
!
;int) * = c=+ * ;.t , , "
;.t:: "
%
%
13. DataStructure-Program to con)ert e+pression in
postfi+ form to prefi+ form
#include #include
#include
#defne MAX 70
stuct ;ostf$
!
cha stacG'MAX('MAX(+ ta-et'MAX( "
cha te;1'(+ te;'( "
cha st1'MAX(+ st'MAX(+ st2'MAX( "
int i+ to; "
% "
&oid init;ostf$ * stuct ;ostf$ , "&oid sete$; * stuct ;ostf$ + cha , "
&oid ;ush * stuct ;ostf$ + cha , "
&oid ;o;* stuct ;ostf$ + cha , "
&oid con&et * stuct ;ostf$ , "
&oid sho/ * stuct ;ostf$ , "
&oid ain* ,
http://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.html7/23/2019 DataStructure Programs
24/53
!
stuct ;ostf$ "
cha e$;'MAX( "
clsc* , "
init;ostf$ * H , "
;int) * =?nEnte an e$;ession in ;ostf$ )o = , "-ets * e$; , "
sete$; * H+ e$; , "
con&et * H , "
;int) * =?nThe Qef$ e$;ession is = , "
sho/ * , "
-etch* , "
%
O initialies the eleents o) the stuctue O
&oid init;ostf$ * stuct ;ostf$ ; ,
!
; 5> i 4 0 "
; 5> to; 4 51 "
stc;C * ; 5> ta-et+ == , "
%
O co;ies -i&en e$;. to ta-et stin- O
&oid sete$; * stuct ;ostf$ ;+ cha c ,
!
stc;C * ; 5> ta-et+ c , "
%
O adds an o;eato to the stacG O
&oid ;ush * stuct ;ostf$ ;+ cha st ,!
i) * ; 5> to; 44 MAX 5 1 ,
;int) * =?nStacG is )ull.= , "
else
!
; 5> to;:: "
stc;C * ; 5> stacG'; 5> to;(+ st , "
%
%
O ;o;s an eleent )o the stacG O&oid ;o; * stuct ;ostf$ ;+ cha a ,
!
i) * ; 5> to; 44 51 ,
;int) * =?nStacG is e;tC.= , "
else
!
stc;C * a+ ; 5> stacG'; 5> to;( , "
7/23/2019 DataStructure Programs
25/53
; 5> to;55 "
%
%
O con&ets-i&en e$;. to ;ef$ )o O
&oid con&et * stuct ;ostf$ ; ,
!/hile * ; 5> ta-et'; 5> i( @4 ?0 ,
!
O sGi; /hites;ace+ i) anC O
i) * ; 5> ta-et'; 5> i( 44 ,
; 5> i:: "
i)* ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 5 PP
; 5> ta-et'; 5> i( 44 : PP ; 5> ta-et'; 5> i( 44 O PP ; 5> ta-et'; 5> i( 44 ,
!
;o; * ;+ ; 5> st , "
;o; * ;+ ; 5> st2 , "
; 5> te;1'0( 4 ; 5> ta-et' ; 5> i( "
; 5> te;1'1( 4 ?0 "
stc;C * ; 5> st1+ ; 5> te;1 , "
stcat * ; 5> st1+ ; 5> st2 , "
stcat * ; 5> st1+ ; 5> st , "
;ush * ;+ ; 5> st1 , "
%
else
!
; 5> te;1'0( 4 ; 5> ta-et'; 5> i( "
; 5> te;1'1( 4 ?0 "stc;C * ; 5> te;+ ; 5> te;1 , "
;ush * ;+ ; 5> te; , "
%
; 5> i:: "
%
%
O dis;laCs the ;ef$ )o o) e$;. O
&oid sho/ * stuct ;ostf$ ; ,
!
cha te; 4 ;.stacG'0( "/hile * te; ,
!
;int) * =c =+ te; , "
te;:: "
%
%
http://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.html7/23/2019 DataStructure Programs
26/53
1,. DataStructure-Program to con)ert e+pression in
postfi+ form to prefi+ form
#include
#include #include
#defne MAX 70
stuct ;ostf$
!
cha stacG'MAX('MAX(+ ta-et'MAX( "
cha te;1'(+ te;'( "
cha st1'MAX(+ st'MAX(+ st2'MAX( "
int i+ to; "
% "
&oid init;ostf$ * stuct ;ostf$ , "
&oid sete$; * stuct ;ostf$ + cha , "
&oid ;ush * stuct ;ostf$ + cha , "
&oid ;o;* stuct ;ostf$ + cha , "
&oid con&et * stuct ;ostf$ , "
&oid sho/ * stuct ;ostf$ , "
&oid ain* ,
!
stuct ;ostf$ "
cha e$;'MAX( "
clsc* , "
init;ostf$ * H , ";int) * =?nEnte an e$;ession in ;ostf$ )o = , "
-ets * e$; , "
sete$; * H+ e$; , "
con&et * H , "
;int) * =?nThe Qef$ e$;ession is = , "
sho/ * , "
-etch* , "
%
O initialies the eleents o) the stuctue O
&oid init;ostf$ * stuct ;ostf$ ; ,
!
; 5> i 4 0 "
; 5> to; 4 51 "
stc;C * ; 5> ta-et+ == , "
%
O co;ies -i&en e$;. to ta-et stin- O
&oid sete$; * stuct ;ostf$ ;+ cha c ,
http://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.html7/23/2019 DataStructure Programs
27/53
!
stc;C * ; 5> ta-et+ c , "
%
O adds an o;eato to the stacG O
&oid ;ush * stuct ;ostf$ ;+ cha st ,
!i) * ; 5> to; 44 MAX 5 1 ,
;int) * =?nStacG is )ull.= , "
else
!
; 5> to;:: "
stc;C * ; 5> stacG'; 5> to;(+ st , "
%
%
O ;o;s an eleent )o the stacG O
&oid ;o; * stuct ;ostf$ ;+ cha a ,
!
i) * ; 5> to; 44 51 ,
;int) * =?nStacG is e;tC.= , "
else
!
stc;C * a+ ; 5> stacG'; 5> to;( , "
; 5> to;55 "
%
%
O con&ets-i&en e$;. to ;ef$ )o O
&oid con&et * stuct ;ostf$ ; ,!
/hile * ; 5> ta-et'; 5> i( @4 ?0 ,
!
O sGi; /hites;ace+ i) anC O
i) * ; 5> ta-et'; 5> i( 44 ,
; 5> i:: "
i)* ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 5 PP
; 5> ta-et'; 5> i( 44 : PP ; 5> ta-et'; 5> i( 44 O PP ; 5> ta-et'; 5> i( 44 ,
!
;o; * ;+ ; 5> st , ";o; * ;+ ; 5> st2 , "
; 5> te;1'0( 4 ; 5> ta-et' ; 5> i( "
; 5> te;1'1( 4 ?0 "
stc;C * ; 5> st1+ ; 5> te;1 , "
stcat * ; 5> st1+ ; 5> st2 , "
stcat * ; 5> st1+ ; 5> st , "
;ush * ;+ ; 5> st1 , "
http://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_expression_in_postfix_to_form.html7/23/2019 DataStructure Programs
28/53
%
else
!
; 5> te;1'0( 4 ; 5> ta-et'; 5> i( "
; 5> te;1'1( 4 ?0 "
stc;C * ; 5> te;+ ; 5> te;1 , ";ush * ;+ ; 5> te; , "
%
; 5> i:: "
%
%
O dis;laCs the ;ef$ )o o) e$;. O
&oid sho/ * stuct ;ostf$ ; ,
!
cha te; 4 ;.stacG'0( "
/hile * te; ,
!
;int) * =c =+ te; , "
te;:: "
%
%
15. DataStructure-Program to con)ert an e+pression in
postfi+ form to an infi+ form
#include #include
#include
#defne MAX 70
stuct ;ostf$
!
cha stacG'MAX('MAX(+ ta-et'MAX( "
cha te;1'(+ te;'( "
cha st1'MAX(+ st'MAX(+ st2'MAX( "
int i+ to; "
% "
&oid init;ostf$ * stuct ;ostf$ , "&oid sete$; * stuct ;ostf$ + cha , "
&oid ;ush * stuct ;ostf$ + cha , "
&oid ;o; * stuct ;ostf$ + cha , "
&oid con&et * stuct ;ostf$ , "
&oid sho/ * stuct ;ostf$ , "
&oid ain* ,
7/23/2019 DataStructure Programs
29/53
!
stuct ;ostf$ "
cha e$;'MAX( "
clsc* , "
init;ostf$ * H , "
;int) * =?nEnte an e$;ession in ;ostf$ )o = , "-ets * e$; , "
sete$; * H+ e$; , "
con&et * H , "
;int) * =?nThe inf$ e$;ession is = , "
sho/ * , "
-etch* , "
%
O initialies data eBe O
&oid init;ostf$ * stuct ;ostf$ ; ,
!
; 5> i 4 0 "
; 5> to; 4 51 "
stc;C * ; 5> ta-et+ == , "
%
O co;ies -i&en e$;ession to ta-et stin- O
&oid sete$; * stuct ;ostf$ ;+ cha c ,
!
stc;C * ; 5> ta-et+ c , "
%
O adds an e$;. to the stacG O
&oid ;ush * stuct ;ostf$ ;+ cha st ,!
i) * ; 5> to; 44 MAX 5 1 ,
;int) * =?nStacG is )ull.= , "
else
!
; 5> to;:: "
stc;C * ; 5> stacG'; 5> to;(+ st , "
%
%
O ;o;s an e$;. )o the stacG O&oid ;o;* stuct ;ostf$ ;+ cha a ,
!
i) * ; 5> to; 44 51 ,
;int) * =?nStacG is e;tC.= , "
else
!
stc;C * a+ ; 5> stacG'; 5> to;( , "
http://www.technicalsymposium.com/Program_to_convert_an_expression_in_postfix_form_to_an_infix_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_an_expression_in_postfix_form_to_an_infix_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_an_expression_in_postfix_form_to_an_infix_form.htmlhttp://www.technicalsymposium.com/Program_to_convert_an_expression_in_postfix_form_to_an_infix_form.html7/23/2019 DataStructure Programs
30/53
; 5> to;55 "
%
%
O con&ets -i&en e$;. to inf$ )o O
&oid con&et * stuct ;ostf$ ; ,
!/hile * ; 5> ta-et'; 5> i( ,
!
O sGi; /hites;ace+ i) anC O
i)* ; 5> ta-et'; 5> i( 44 ,
; 5> i:: "
i) * ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 PP ; 5> ta-et'; 5> i( 44 5
PP ; 5> ta-et'; 5> i( 44 : PP ; 5> ta-et'; 5> i( 44 O PP ; 5> ta-et'; 5> i( 44 ,
!
;o; * ;+ ; 5> st , "
;o; * ;+ ; 5> st2 , "
; 5> te;1'0( 4 ; 5> ta-et'; 5> i( "
; 5> te;1'1( 4 ?0 "
stc;C * ; 5> st1+ ; 5> st2 , "
stcat * ; 5> st1+ ; 5> te;1 , "
stcat * ; 5> st1+ ; 5> st , "
;ush * ;+ ; 5> st1 , "
%
else
!
; 5> te;1'0( 4 ; 5> ta-et'; 5> i( "
; 5> te;1'1( 4 ?0 "stc;C * ; 5> te;+ ; 5> te;1 , "
;ush * ;+ ; 5> te; , "
%
; 5> i:: "
%
%
O dis;laCs the e$;ession O
&oid sho/ * stuct ;ostf$ ; ,
!
cha t "t 4 ;.stacG'0( "
/hile * t ,
!
;int) * =c =+ t , "
t:: "
%
%
7/23/2019 DataStructure Programs
31/53
16. DataStructure-Program to e)aluate an epression
entered in postfi+ form
#include
#include #include
#include
#include
#defne MAX 70
stuct ;ostf$
!
int stacG'MAX( "
int to;+ nn "
cha s "
% "
&oid init;ostf$ * stuct ;ostf$ , "
&oid sete$; * stuct ;ostf$ + cha , "
&oid ;ush * stuct ;ostf$ + int , "
int ;o;* stuct ;ostf$ , "
&oid calculate * stuct ;ostf$ , "
&oid sho/ * stuct ;ostf$ , "
&oid ain* ,
!
stuct ;ostf$ "
cha e$;'MAX( "
clsc* , "init;ostf$ * H , "
;int) * =?nEnte ;ostf$ e$;ession to Be e&aluated = , "
-ets * e$; , "
sete$; * H+ e$; , "
calculate * H , "
sho/ * , "
-etch* , "
%
O initialies dataeBes O
&oid init;ostf$ * stuct ;ostf$ ; ,
!
; 5> to; 4 51 "
%
O sets s to ;oint to the -i&en e$;. O
&oid sete$; * stuct ;ostf$ ;+ cha st ,
!
; 5> s 4 st "
http://www.technicalsymposium.com/Program_to_evaluate_an_epression_entered_in_postfix_form.htmlhttp://www.technicalsymposium.com/Program_to_evaluate_an_epression_entered_in_postfix_form.htmlhttp://www.technicalsymposium.com/Program_to_evaluate_an_epression_entered_in_postfix_form.htmlhttp://www.technicalsymposium.com/Program_to_evaluate_an_epression_entered_in_postfix_form.html7/23/2019 DataStructure Programs
32/53
%
O adds di-it to the stacG O
&oid ;ush * stuct ;ostf$ ;+ int ite ,
!
i) * ; 5> to; 44 MAX 5 1 ,
;int) * =?nStacG is )ull.= , "else
!
; 5> to;:: "
; 5> stacG'; 5> to;( 4 ite "
%
%
O ;o;s di-it )o the stacG O
int ;o; * stuct ;ostf$ ; ,
!
int data "
i) * ; 5> to; 44 51 ,
!
;int) * =?nStacG is e;tC.= , "
etun 6ULL "
%
data 4 ; 5> stacG'; 5> to;( "
; 5> to;55 "
etun data "
%
O e&aluates the ;ostf$ e$;ession O
&oid calculate* stuct ;ostf$ ; ,!
int n1+ n+ n2 "
/hile * * ; 5> s , ,
!
O sGi; /hites;ace+ i) anC O
i) * * ; 5> s , 44 PP * ; 5> s , 44 ?t ,
!
; 5> s:: "
continue "
%O i) di-it is encounteed O
i) * isdi-it * * ; 5> s , , ,
!
; 5> nn 4 * ; 5> s , 5 0 "
;ush * ;+ ; 5> nn , "
%
else
7/23/2019 DataStructure Programs
33/53
!
O i) o;eato is encounteed O
n1 4 ;o; * ; , "
n 4 ;o; * ; , "
s/itch * * ; 5> s , ,
!case :
n2 4 n : n1 "
BeaG "
case 5
n2 4 n 5 n1 "
BeaG "
case O
n2 4 n O n1 "
BeaG "
case
n2 4 n n1 "
BeaG "
case
n2 4 n n1 "
BeaG "
case
n2 4 ;o/ * n + n1 , "
BeaG "
de)ault
;int) * =UnGno/n o;eato= , "
e$it * 1 , "%
;ush * ;+ n2 , "
%
; 5> s:: "
%
%
O dis;laCs the esult O
&oid sho/ * stuct ;ostf$ ; ,
!
;.nn 4 ;o; * H; , ";int) * =Result is d=+ ;.nn , "
%
17. DataStructure-Program to maintain a lin!ed list#include
#include
#include
7/23/2019 DataStructure Programs
34/53
/* structure containing a datapart and link part */
struct node{
intdata ;
struct node * link ;
} ;void append ( struct node **, int ) ;
void addatbeg ( struct node **, int ) ;
void addater ( struct node *, int, int ) ;void displa! ( struct node * ) ;
int count ( struct node * ) ;
void delete( struct node **, int ) ;void "ain( )
{
struct node *p ;
p $%&& ; /* e"pt! linked list */
print ( 'n$o. o ele"entsin the &inked &ist d', count ( p ) ) ;append ( p, + ) ;
append ( p, - ) ;append ( p, 0 ) ;
append ( p, ) ;
append ( p, +1 ) ;displa! ( p ) ;
addatbeg ( p, 222 ) ;
addatbeg ( p, 333 ) ;addatbeg ( p, 111 ) ;
displa! ( p ) ;
addater ( p, 1, ) ;addater ( p, , + ) ;addater ( p, 0, 22 ) ;
displa! ( p ) ;
print ( 'n$o. o ele"ents in the &inked &ist d', count ( p ) ) ;delete ( p, 22 ) ;
delete ( p, + ) ;
delete ( p, + ) ;displa! ( p ) ;
print ( 'n$o. o ele"ents in the &inked &ist d', count ( p ) ) ;
}
/* adds a node at the end o a linked list */void append ( struct node **4, int nu" )
{
struct node *te"p, *r ;i ( *4 $%&& ) /* i the list is e"pt!, create irst node */
{
te"p "alloc ( si5eo ( struct node ) ) ;te"p 6> data nu" ;
http://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_maintain_a_linked_list.html7/23/2019 DataStructure Programs
35/53
te"p 6> link $%&& ;
*4 te"p ;}
else
{
te"p *4 ;/* go to last node */
7hile ( te"p 6> link 8 $%&& )
te"p te"p 6> link ;/* add node at the end */
r "alloc ( si5eo ( struct node ) ) ;
r 6> data nu" ;r 6> link $%&& ;
te"p 6> link r ;
}
}
/* adds a ne7 node at the beginning o the linked list */void addatbeg ( struct node **4, int nu" )
{struct node *te"p ;
/* add ne7 node */
te"p "alloc ( si5eo ( struct node ) ) ;te"p 6> data nu" ;
te"p 6> link *4 ;
*4 te"p ;}
/* adds a ne7 node ater the speciied nu"ber o nodes */
void addater ( struct node *4, int loc, int nu" ){struct node *te"p, *r ;
int i ;
te"p 4 ;/* skip to desired portion */
or ( i ; i < loc ; i99 )
{te"p te"p 6> link ;
/* i end o linked list is encountered */
i ( te"p $%&& )
{print ( 'n:here are less than d ele"ents in list', loc ) ;
return ;
}}
/* insert ne7 node */
r "alloc ( si5eo ( struct node ) ) ;r 6> data nu" ;
7/23/2019 DataStructure Programs
36/53
r 6> link te"p 6> link ;
te"p 6> link r ;}
/* displa!s the contents o the linked list */
void displa! ( struct node *4 )
{print ( 'n' ) ;
/* traverse the entire linked list */
7hile ( 4 8 $%&& ){
print ( 'd ', 4 6> data ) ;
4 4 6> link ;}
}
/* counts the nu"ber o nodes present in the linked list */
int count ( struct node * 4 )
{int c ;
/* traverse the entire linked list */7hile ( 4 8 $%&& )
{
4 4 6> link ;c99 ;
}
return c ;}
/* deletes the speciied node ro" the linked list */
void delete ( struct node **4, int nu" ){struct node *old, *te"p ;
te"p *4 ;
7hile ( te"p 8 $%&& ){
i ( te"p 6> data nu" )
{/* i node to be deleted is the irst node in the linked list */
i ( te"p *4 )
*4 te"p 6> link ;
/* deletes the inter"ediate nodes in the linked list */else
old 6> link te"p 6> link ;
/* ree the "e"or! occupied b! the node */ree ( te"p ) ;
return ;
}/* traverse the linked list till the last node is reached */
7/23/2019 DataStructure Programs
37/53
else
{old te"p ; /* old points to the previous node */
te"p te"p 6> link ; /* go to the net node */
}
}print ( 'nle"ent d not ound', nu" ) ;
}
1". $ra)elling Salesman Problem using bac!trac!ing
O Ta&ellin- Salesan QoBle usin- BacGtacGin-O#include=stdio.h=int $'17(+used'17("int adI'17('17(4!0%"int ;ath'17('17(+/-ht'17("int c+in"
int ;athoG*int G+int n,!i)*used'$'G((,etun 0"i)*Gn51,etun*adI'$'G51(('$'G((,"elseetun*adI'$'G51(('$'G(( HH adI'$'G(('$'0((,"%&oid TSQ*int G+int n,!int i+su"
)o*$'G(41"$'G(
7/23/2019 DataStructure Programs
38/53
7/23/2019 DataStructure Programs
39/53
1%. // P0B/(
#include&oid ain*,!int i+G40+n+o/'17(+ddia-'0(+udia-'0("int Boad'17(++I"clsc*,";int)*=?n?t?t6 DUEE6S QRLEM?n?n=,";int)*=?n?tEnte the 6o. o) Dueens to Be ;laced =,"scan)*=d=+Hn,")o*i40"i
7/23/2019 DataStructure Programs
40/53
udia-'G:Boad'G((40"Boad'G(::"%%%%
2'. napsac! Problem sing Bac!trac!ing
O Yna;sacG QoBle Usin- acGtacGin-O#includeint $'1(+/'1(+;'1(++n++&al"&oid ain*,!int G+i"oat 40"&oid Gna;sacG*oat+int+oat,"
int s140"clsc*,";int)*=?n?n?t?tY6AQSAY QRLEM USJ6W AYTRAYJ6W?n?n=,";int)*=?n?tEnte the 6o. o) Jtes =,"scan)*=d=+Hn,";int)*=?n?tEnte the Zei-ht H Value )o the Jtes ?n=,")o*i40"i
7/23/2019 DataStructure Programs
41/53
;int)*=d =+;'I(,"%%;int)*=?n?n=,"%else i)*s:/'G(:/'G:1(
7/23/2019 DataStructure Programs
42/53
vF;
}ne7E7ghtCv+DCvDne7E7ghtCvDCv+D"in;
count99;
closedCcountDv;
print('nGcan H d d666666666d 7ght d n',count,v+9+,v9+,"in);getch();
}
}void "ain()
{
int i,F,ed,su";clrscr();
print('nnt=IA?@G J&KIA:L? :K BA$M G=J$$A$ :Inn');
print('ntnter the $o. o $odes H ');
scan('d',n);
or(i;i
7/23/2019 DataStructure Programs
43/53
or(i;i
7/23/2019 DataStructure Programs
44/53
)o*i40"i
7/23/2019 DataStructure Programs
45/53
!&'i(.distance4dist"&'i(.;ecd4cuent"%%%
sall4J6F")o*i40"i
7/23/2019 DataStructure Programs
46/53
!;int)*=?n?tEnte the Zei-ht =,"scan)*=d=+Hed-e,"i)*ed-e>0,&'i(./ei-ht'I(4ed-e"%
%%\ush*stdin,";int)*=?n?n?tEnte the Souce =,"scan)*=c=+Hsouce,"\ush*stdin,";int)*=?n?n?tEnte the estination =,"scan)*=c=+Hdest,"i)*souce44dest,!;int)*=?n?tSURE H ESTJ6ATJ6 A6T E SAME.=,"e$it*0,"
%)o*i40"i
7/23/2019 DataStructure Programs
47/53
int issalle * cha + cha , "int is-eate * cha + cha , "cha -etsuB * cha + int+ int , "cha le)tsuB * cha + int n , "cha i-htsuB * cha + int n , "&oid u;;e * cha , "
&oid lo/e * cha , "&oid e&ese * cha , "int e;lace * cha + cha+ cha , "int setat * cha + cha+ int , "&oid ain* ,!cha s1' ( 4 =Nello= "cha s' ( 4 =Nello Zold= "cha s2' ( 4 =Fou hunded thitC t/o= "cha ch+ s "int i "clsc* , "
;int) * =?nStin- s1 s=+ s1 , "O checG )o the fst occuence o) a chaacte O;int) * =?nEnte chaacte to seach = , "scan) * =c=+ Hch , "i 4 seach * s1+ ch , "i) * i @4 51 ,;int) * =The fst occuence o) chaacte c is )ound at inde$ no. d?n=+ ch+ i , "else;int) * =haacte c is not ;esent in the list.?n=+ ch , ";int) * =?nStin- s s=+ s , "O co;aes t/o stin-s s1 and s Oi 4 iseuals * s1+ s , "i) * i 44 1 ,;int) * =?nStin-s s1 and s ae identical= , "else;int) * =?nStin-s s1 and s ae not identical=, "i 4 issalle * s1+ s , "i) * i 44 1 ,;int) * =?nStin- s1 is salle than stin- s= , "else;int) * =?nStin- s1 is not salle than stin- s= , "i 4 is-eate * s1+ s , "i) * i 44 1 ,;int) * =?nStin- s1 is -eate than stin- s?n= , "else;int) * =?nStin- s1 is not -eate than stin- s?n= , "O e$tact chaactes at -i&en ;osition O;int) * =?nStin- s2 s=+ s2 , "s 4 -etsuB * s2+ 7+ 9 , ";int) * =?nSuB stin- s=+ s , ")ee * s , "O e$tact le)tost n chaactes O
7/23/2019 DataStructure Programs
48/53
s 4 le)tsuB * s2+ 3 , ";int) * =?nLe)t suB stin- s=+ s , ")ee * s , "O e$tact i-htost n chaactes Os 4 i-htsuB * s2+ 2 , ";int) * =?nRi-ht suB stin- s=+ s , "
)ee * s , "O con&et stin- to u;;ecase Ou;;e * s2 , ";int) * =?nStin- in u;;e case s=+ s2 , "O con&et stin- to lo/ecase Olo/e * s2 , ";int) * =?nStin- in lo/e case s=+ s2 , "O e&ese the -i&en stin- Oe&ese * s2 , ";int) * =?nRe&esed stin- s=+ s2 , "O e;lace fst occuence o) one cha /ith ne/ one Oe;lace * s1+ N + M , "
;int) * =?nStin- s1 s=+ s1 , "O sets a cha at a -i&en ;osition Oi 4 setat * s1+ M+ 2 , "i) * i ,;int) * =?nStin- s1 s=+ s1 , "else;int) * =?nJn&alid ;osition.= , "-etch* , "%O checG )o the fst occuence o) a chaacte Oint seach * cha st+ cha ch ,!int i 4 0 "/hile * st ,!i) * st 44 ch ,etun i "st:: "i:: "%etun 51 "%O checGs /hethe t/o stin-s ae eual Oint iseuals * cha s+ cha t ,!/hile * s PP t ,!i) * s @4 t ,etun 0 "s:: "t:: "%
7/23/2019 DataStructure Programs
49/53
etun 1 "%O checGs /hethe fst stin- is less than second Oint issalle * cha s+ cha t ,!/hile * t ,
!i) * s @4 t ,!i) * s < t ,etun 1 "elseetun 0 "%t:: "s:: "%etun 0 "
%O checGs /hethe fst stin- is -eate than second Oint is-eate * cha s+ cha t ,!/hile * s ,!i) * s @4 t ,!i) * s > t ,etun 1 "elseetun 0 "%s:: "t:: "%etun 0 "%O e$tacts the chaacte at -i&en ;osition Ocha -etsuB * cha st+ int s;os+ int n ,!cha s 4 st : s;os "cha t 4 * cha , alloc * n : 1 , "int i 4 0 "/hile * i < n ,!t'i( 4 s "s:: "i:: "%t'i( 4 ?0 "etun t "
7/23/2019 DataStructure Programs
50/53
7/23/2019 DataStructure Programs
51/53
%O e&eses a stin- O&oid e&ese * cha st ,!int l 4 stlen * st , "cha ch+ t 4 * st : l 5 1 , "
int i 4 0 "/hile * i < l O ,!ch 4 st "st 4 t "t 4 ch "st:: "t55 "i:: "%%O e;laces the fst occuence o) cha /ith ne/ cha O
int e;lace * cha st+ cha oldch+ cha ne/ch ,!/hile * st ,!i) * st 44 oldch ,!st 4 ne/ch "etun 1 "%st:: "%etun 0 "%O sets a cha at a -i&en ;osition Oint setat * cha st+ cha ch+ int i ,!i) * i < 0 PP stlen * st , < i ,etun 0 " * st : i , 4 ch "etun 1 "%
25. DataStructure-Program to re)erse a lin!ed list
#include #include #include O stuctue containin- a data;at and linG ;at Ostuct node!int data "
http://www.technicalsymposium.com/Program_to_reverse_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_reverse_a_linked_list.html7/23/2019 DataStructure Programs
52/53
stuct node linG "% "&oid addatBe- * stuct node + int , "&oid e&ese * stuct node , "&oid dis;laC * stuct node , "int count * stuct node , "
&oid ain* ,!stuct node ; "; 4 6ULL " O e;tC linGed list OaddatBe- * H;+ 9 , "addatBe- * H;+ 32 , "addatBe- * H;+ 19 , "addatBe- * H;+ 2 , "addatBe- * H;+ 2 , "addatBe- * H;+ 7 , "clsc* , "dis;laC * ; , "
;int) * =?n6o. o) eleents in the linGed list 4 d=+ count * ; , , "e&ese * H; , "dis;laC * ; , ";int) * =?n6o. o) eleents in the linGed list 4 d=+ count * ; , , "%O addsa ne/ node at the Be-innin- o) the linGed list O&oid addatBe- * stuct node + int nu ,!stuct node te; "O add ne/ node Ote; 4 alloc * sieo) * stuct node , , "te; 5> data 4 nu "te; 5> linG 4 " 4 te; "%&oid e&ese * stuct node $ ,!stuct node + + s " 4 $ " 4 6ULL "O ta&ese the entie linGed list O/hile * @4 6ULL ,!s 4 " 4 " 4 5> linG " 5> linG 4 s "%$ 4 "%O dis;laCs the contents o) the linGed list O&oid dis;laC * stuct node ,
http://www.technicalsymposium.com/Program_to_reverse_a_linked_list.htmlhttp://www.technicalsymposium.com/Program_to_reverse_a_linked_list.html7/23/2019 DataStructure Programs
53/53
!;int) * =?n= , "O ta&ese the entie linGed list O/hile * @4 6ULL ,!;int) * =d =+ 5> data , "
4 5> linG "%%O counts the nuBe o) nodes ;esent in the linGed list Oint count * stuct node ,!int c 4 0 "O ta&ese the entie linGed list O/hile * @4 6ULL ,! 4 5> linG "c:: "
%etun c "%
Top Related