Download - Ds Full File
-
8/20/2019 Ds Full File
1/113
To implement traversal,insertion,deletion in a linear array.
ALGORITHM:
TRAVERSAL:
This algorithm traverses a linear array A with lower bound 0 and upper bound 9. t traverses Asear!hing the desired number "#$%.
Step &: Repeat 'or (0 to 9
Apply sear!h operation 'or A)*(("#$%.End o' loop.
Step +: E,it.
"SERT-":
n this A is a linear array with &0 elements and "#$% is any number and -S is the potion at
whi!h the "#$% is to be inserted.
Step &: Set (&0. )nitiali/e !ounter.*
Step +: Repeat steps and 1 while 2(-S.
Step : Set A)3&*(A)*. )$ove 4th element downwards.*
Step 1: Set (5&. )6e!rease !ounter.*
Step 7: Set A)-S*("#$%. )nsert element.*Step 8: Set &0 to &&. )Reset number o' elements.*
Step : E,it.
6ELET-":
n this A is a linear array with &0 elements and "#$% is any number and this deletes the "#$%number 'rom the array A.
-
8/20/2019 Ds Full File
2/113
Step &: Repeat 'or (0 to 9
Apply sear!h operation 'or A)*(("#$%.
End o' loop.
Step +: Repeat 'or (-S to
Set A)*(A)3&* )$ove th element upwards.*
End o' loop
Step : Set 9 to . )Reset number o' elements.*
Step 1: E,it.
FLOW CHART:
-
8/20/2019 Ds Full File
3/113
PROGRAM CO!:
;in!ludevoid main =>
?
!lrs!r=>@
int a)+0*numbpos,'lag(0@
!out
?!out
?pos(i@
!out
?!out
-
8/20/2019 Ds Full File
4/113
?!out
-
8/20/2019 Ds Full File
5/113
"CR!!# "HOT":
-
8/20/2019 Ds Full File
6/113
-
8/20/2019 Ds Full File
7/113
To implement "ta$%s &sin' arrays.
ALGORITHM:
#S -ERAT-":
Step &: ?Fhe!G 'or sta!G over'lowC
' T-S2(Si/e5&
-utput HSta!G -ver'lowH
Step +: ?n!rement the !ounter value by oneC
T-S(T-S3&
Step : ?er'orm nsertionC
a)T-S*(value
Step 1: E,it.
- -ERAT-":
Step &: ?Fhe!G whether the sta!G is emptyC
' T-S(0
-utput ISta!G #nder'lowH
Step +: ?op the T-S valueC
value(a)T-SC
T-S(T-S5&
Step : ?rint the top most value o' the sta!GC
Return =value>
Step 1: E,it.
-
8/20/2019 Ds Full File
8/113
PROGRAM CO!:
;in!lude;in!lude
?
!lrs!r=>@
int T-S(0ia)7*value!h@
!out
?i'=T-S21>?!out
?!out
-
8/20/2019 Ds Full File
9/113
i'=T-S((5&>
?!out
?e,it=0>@C
Cwhile=!h((& JJ !h((+>@
C
-
8/20/2019 Ds Full File
10/113
"CR!!# "HOT":
-
8/20/2019 Ds Full File
11/113
To per(orm linear or )inary sear$* as per t*e &sers $*oi$e.
ALGORITHM:
L"EAR SEARF:
Step &: Kor ea!h item in the list
Fhe!G i' value mat!hes any item in the array.
Step +: ' it mat!hes.
Return its inde,.
Step : ' it does not mat!h.
Fontinue sear!hing until the end o' the array.
Step 1: E,it.
%"AR SEARF:
Mhile =beg
Step &: mid(=beg3end>N+
Step +: i' ,((a)mid*
Return mid
Step : else i' ,2a)mid*
beg(mid3&
Step 1: else
end(mid5&
Step 7: Return , not 'ound in the array.
Step 8: E,it.
-
8/20/2019 Ds Full File
12/113
PROGRAM CO!:
;in!lude!lass Sear!h
?
publi!:
int a)&0*,y'lagiDtemp@
void getdata=>@
void linear=int ,>@
void sort=int a)*>@
void binary=int ,>@
C@
void Sear!h::getdata=>
?!out
?linear=,>@C
else i'=y((+>
?sort=a>@
binary=,>@C
else
?!out
-
8/20/2019 Ds Full File
13/113
void Sear!h::linear=int ,>
?'lag(0@
'or=int i(0@i
?i'=a)i*((,>
?!out
-
8/20/2019 Ds Full File
14/113
?mid(=beg3last>N+@
i'=,((a)mid*>
?'lag33@
!out
-
8/20/2019 Ds Full File
15/113
"CR!!# "HOT":
-
8/20/2019 Ds Full File
16/113
To per(orm sortin' on a linear array.
ALGORITHM:
%#%%LE S-RT:
Step &: Repeat Steps + and 'or (& to &0
Step +: Set 4(&
Step : Repeat while 4
-
8/20/2019 Ds Full File
17/113
PROGRAM CO!:
;in!ludevoid main=>
?!lrs!r=>@
int a)7*tempiD@
!out
-
8/20/2019 Ds Full File
18/113
"CR!!# "HOT":
To implement Linear and Cir$&lar +&e&e &sin' arrays.
-
8/20/2019 Ds Full File
19/113
ALGORITHM:
Linear +&e&e:
Insertin' an element to linear +&e&e
Step &: ' KR-"T (& and REAR(" or i' KR-"T(REAR3& then :
rint : -ver'low and return
Step +: K KR-"T ( "#LL then
Set KR-"T (& and REAR (&
Else ' REAR (" then
Set REAR ( &
Else
Set REAR (REAR 3&
Step : Set O#E#E)REAR*(TE$
Step 1: RET#R"
eletin' an element (rom a linear &e&e
Step &: ' KR-"T ("#LL then :
rint : #nder'low'low and return
Step +: Set TE$ ( O#E#E)KR-"T*
Step : K KR-"T ( REAR then
Set KR-"T (0 and REAR (0
Else ' KR-"T (" then
Set KR-"T ( &
Else
Set KR-"T (KR-"T 3&
Step 1: RET#R"
Cir$&lar +&e&e:
-
8/20/2019 Ds Full File
20/113
Insertin' an element to $ir$&lar +&e&e
Step &: nitiali/e KR-"T ( P &@ REAR ( &
Step +: REAR ( =REAR 3 &> Q SE
Step : ' =KR-"T is eual to REAR>
=a> 6isplay IOueue is 'ullH
=b> E,it
Step 1: Else
=a> nput the value to be inserted and assign to variable I6ATAH
Step 7: ' =KR-"T is eual to P &>
=a> KR-"T ( 0
=b> REAR ( 0
Step 8: O)REAR* ( 6ATA
Step : Repeat steps + to 7 i' we want to insert more elements
Step : E,it
eletin' an element (rom a $ir$&lar &e&e
Step &: ' =KR-"T is eual to P &>
=a> 6isplay IOueue is emptyH
=b> E,it
Step +: Else
=a> 6ATA ( O)KR-"T*
Step : ' =REAR is eual to KR-"T>
=a> KR-"T ( P&
=b> REAR ( P&
Step 1: Else
=a> KR-"T ( =KR-"T 3&> Q SE
Step 7: Repeat the steps & + and i' we want to delete more elements
Step 8: E,it
PROGRAM CO!:
-
8/20/2019 Ds Full File
21/113
Linear +&e&e:
;in!lude;in!lude
?i'=rear(($A5&>
?!out
-
8/20/2019 Ds Full File
22/113
else
?item(ueue)'ront*@
i'='ront((rear>
?'ront(5&@
rear(5&@
C
else
?'ront33@C
!out
-
8/20/2019 Ds Full File
23/113
!in22!h@
swit!h=!h>
?!ase &:insert=>@
breaG@
!ase +:del=>@
breaG@
!ase :display=>@
breaG@
!ase 1:e,it=0>@
de'ault:!out
-
8/20/2019 Ds Full File
24/113
-
8/20/2019 Ds Full File
25/113
Cir$&lar +&e&e:
;in!lude;in!lude
?i'=='ront((0 UU rear((SE5&> JJ ='ront((rear3&>>
?!out?rear(0@
'ront(0@
C
else i'=rear((SE5&>
?rear(0@C
else
?rear33@C
ueue)rear*(item@
!out
-
8/20/2019 Ds Full File
26/113
?!out
?'ront(0@C
else
?'ront33@C
!out
-
8/20/2019 Ds Full File
27/113
?!out
-
8/20/2019 Ds Full File
28/113
O-TP-T:
Cir$&lar +&e&e:
-
8/20/2019 Ds Full File
29/113
To per(orm vario&s operations on a sin'ly lin%ed list.
ALGORITHM:
"SERT-" -K A "-6E:
A> AT TE %E""" -K A L"WE6 LST
Step &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?Allo!ate 'ree spa!eC
"ew(new LinG
Step : ?Read value o' in'ormation part o' a new nodeC
n'o)"ew*( Value
Step 1: ?LinG address part o' the !urrently !reated node with the address o' startC "e,t)"e,t*(Start
Step 7: ?"ow assign address o' newly !reated node to the startC
Start("ew
Step 8: E,it.
%> AT TE E"6 -K A L"WE6 LST
Step &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?Allo!ate 'ree spa!eC
"ew(new LinG
Step : ?Read value o' in'ormation part o' a new nodeC
n'o)"ew*( Value
Step 1: ?$ove the pointer to the end o' the listCRepeat while "ode
-
8/20/2019 Ds Full File
30/113
F> AT A 6ESRE6 LAFEME" "-6E "#$%ER S W"-M"Step &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?nitiali/atonC
"odeXnumber(0
"ode(Start."e,t)oints to 'irst node o' the list*
revious(Address o' Start)Assign address o' start to previous*
Step : ?Read node number that we want to insertC
nput nsertXnode
Step 1: ?er'orm insertion operationCRepeat through Step 7 while "-6E
-
8/20/2019 Ds Full File
31/113
-utput I#"6ERKL-MH and e,it.
Else ?6elete 'irst nodeC
"e,t)revious*("e,t)"ode* ?$ove pointer to ne,t node in the listC
Kree the spa!e asso!iated with "ode
Step : E,it.
%> KR-$ TE E"6 -K A L"WE6 LSTStep &: ?nitiali/ationC
"ode(start."e,t ?oints to the 'irst node in the listC
revious(assign address o' start
"odeXnumber(0
Step +: ?Fhe!G list is empty or notC ' node("#LL
-utput I#"6ERKL-MH and e,it.
Step : ?S!an the list to !ount the number o' node in the listC
Repeat while node
-
8/20/2019 Ds Full File
32/113
PROGRAM CO!:
;in!lude;in!lude
-
8/20/2019 Ds Full File
33/113
?!out
-
8/20/2019 Ds Full File
34/113
?i(&@
!out
-
8/20/2019 Ds Full File
35/113
C
C
breaG@
!ase 1: i'=start(("#LL>
?!out
?!out
-
8/20/2019 Ds Full File
36/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
37/113
-
8/20/2019 Ds Full File
38/113
To Implement o&)ly Lin%ed List.
ALGORITHM:
I#"!RTI#G A #O!
Suppose START is the 'irst position in linGed list. Let 6ATA be the element to be
inserted in the new node. -S is the position where the "ew"ode is to be inserted. TE$
is a temporary pointer to hold the node address.
Step &: nput the -S
Step +: nitiali/e TE$ ( START@ i ( 0
Step : Repeat the step 1 i' =i less than -S> and =TE$ is not eual to "#LL>Step 1: TE$ ( TE$ ] R̂oint@ i ( i 3&
Step 7: ' =TE$ not eual to "#LL> and =i eual to -S>
=a> Freate a "ew "ode
=b> "ew"ode] 6̂ATA ( 6ATA
=c> "ew"ode] R̂oint ( TE$ ] R̂oint
=d > "ew"ode] L̂oint ( TE$
=e> =TE$ ] R̂oint> ] L̂oint ( "ew"ode
= f > TE$ ] R̂oint ( "ew "ode
Step 8: Else
=a> 6isplay Iosition "-T 'oundH
Step : E,it
!L!TI#G A #O!
Suppose START is the address o' the 'irst node in the linGed list. Let -S is the
position o' the node to be deleted. TE$ is the temporary pointer to hold the address o' the
node. A'ter deletion 6ATA will !ontain the in'ormation on the deleted node.
Step &: nput the -S
Step +: nitiali/e TE$ ( START@ i ( 0
Step : Repeat the step 1 i' =i less than -S> and =TE$ is not eual to "#LL>
Step 1: TE$ ( TE$ ] R̂oint@ i ( i 3&
-
8/20/2019 Ds Full File
39/113
Step 7: ' =TE$ not eual to "#LL> and =i eual to -S>
=a> Freate a "ew "ode
=b> "ew"ode] 6̂ATA ( 6ATA
=c> "ew"ode] R̂oint ( TE$ ] R̂oint
=d > "ew"ode] L̂oint ( TE$
=e> =TE$ ] R̂oint> ] L̂oint ( "ew"ode
= f > TE$ ] R̂oint ( "ew "ode
Step 8: Else
=a> 6isplay Iosition "-T 'oundH
Step : E,it
-
8/20/2019 Ds Full File
40/113
PROGRAM CO!:
;in!lude
?int in'o@
stru!t node Yprev@
stru!t node Yne,t@
CYstart@
void !reateXlist=int>@
void addXatXbeg=int>@
void addXa'ter=intint>@
void del=int>@
void display=>@
void main=>
?!lrs!r=>@
int !hnmposi@
start("#LL@
while=&>
?!out
-
8/20/2019 Ds Full File
41/113
swit!h=!h>
?!ase &: !out@
breaG@
!ase 7: display=>@
breaG@
!ase 8: e,it=0>@
breaG@
de'ault:!out
-
8/20/2019 Ds Full File
42/113
C
void !reateXlist=int num>
?stru!t node YYtemp@
temp(new node@
temp 52 in'o(num@
temp 52 ne,t("#LL@
i'=start(("#LL>
?temp52prev("#LL@
start52prev(temp@
start(temp@
C
else
?(start@
while=52ne,t[("#LL>
?( 52 ne,t@C
52ne,t(temp@ temp52prev(@
C
C
void addXatXbeg=int num>
?stru!t node Ytemp@
temp(new node@
temp 52 prev("#LL@
temp 52 in'o(num@
temp 52 ne,t(start@
start52 prev(temp@
start(temp@
C
-
8/20/2019 Ds Full File
43/113
void addXa'ter=int num int p>
?stru!t node Y Ytemp@
int i@
(start@
'or=i(0@i
? ( 52ne,t@
i'=(("#LL>
?!out
?temp(start@
start(start 52 ne,t@
start52prev("#LL@
delete temp@
'lag(&@
C
(start@
while=52ne,t52ne,t [( "#LL>
?i'=52ne,t52in'o (( num>
?temp(52ne,t@
-
8/20/2019 Ds Full File
44/113
52ne,t ( temp52ne,t@
temp52ne,t52prev(@
delete temp@
'lag(&@
C
( 52ne,t@
C
i'=52ne,t52in'o (( num>
?temp(52ne,t@
delete temp@
52ne,t ( "#LL@
'lag(&@
C
i'='lag((&>
?!out
?!out
-
8/20/2019 Ds Full File
45/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
46/113
-
8/20/2019 Ds Full File
47/113
To Implement Cir$&lar Lin%ed List.
ALGORITHM:
Al'orit*m (or t*e Creation o( t*e Cir$&lar list
FREATE = Y TE$EA6>
)This 'un!tion !reates the !ir!ular list and TE$EA6 is the pointer variable whi!h
points the 'irst element o' the list*
Step &:)Save the address o' the 'irst element*
SAVE ( TE$EA6
Step +: )Repeat thru step 7*
Repeat while Fhoi!e[ ( _n`Step : )Allo!ate the "ew node*
"EM "-6E=>
Step 1: )nitiali/e the 'ields o' new node*
"K- ="EM> (
L"W =SAVE> ( "EM
SAVE ( "EM
Step 7: )Mant to insert another node*
Read =Fhoi!e>
Step 8: )Set the L"W 'ield o' Last inserted element*
L"W =SAVE> ( TE$EA6
Step : )Kinished*
Return
Al'orit*m (or t*e insertion o( t*e node in t*e $ir$&lar list
"SERT = Y TE$EA6 WE>
)This Kun!tion inserts an element a'ter the node whi!h have the in'o 'ield eual to the
WE variable and TE$EA6 is the pointer whi!h points the 'irst element o' the list
and SAVE is the temp variable 'or the store address o' the 'irst element*
Step &: )Allo!ate the $emory 'or the "EM node*
"EM "-6E= >
-
8/20/2019 Ds Full File
48/113
Step +: )Set 'ields o' the "EM node*
"K- ="EM> (
L"W ="EM> ( "#LL
Step : )Save address o' the 'irst node*
KRST ( TE$EA6
Step 1: )nsertion as 'irst node and 'ind last element o' the list*
Repeat while L"W =TE$EA6>[ ( "#LL
Step 7: )nsert the node*
L"W =TE$EA6> ( "EM
L"W ="EM> ( KRST
KRST ( "EM
Return =KRST>
Step 8: )nsert in the list other than the 'irst node*
Repeat while "K- =L"W =TE$EA6>> ( WE
Step : )Set the linG 'or the "EM node*
L"W ="EM> ( L"W =TE$EA6>
L"W =TE$EA6> ( "EMStep : )Kinished*
Return =KRST>
Al'orit*m (or t*e eletion an element (rom t*e $ir$&lar list
6ELETE =YTE$EA6 WE>
)This Kun!tion deletes an element 'rom the !ir!ular list*
Step &: )Fhe!G 'or the empty list*
' TE$EA6 ( "#LL
Then write =IEmpty ListH>
Step +: )List !ontain Single node*
i' L"W =TE$EA6> ( TE$EA6
Return "#LL
Kree =TE$EA6>
Step : )Save the address o' the 'irst node*
-
8/20/2019 Ds Full File
49/113
KRST ( TE$EA6
Step 1: )6eletion o' the 'irst node*
Repeat while L"W =TE$EA6>[ ("#LL
Step 7: )6elete the node*
L"W =TE$EA6> ( L"W =KRST>
L"W =KRST> ( KRST
Return =KRST>
Step 8: )Kinding desire node*
Repeat while "K- =L"W =TE$EA6>> ( WE
Step : )6eletes the node*
TE$ ( L"W =TE$EA6>
L"W =TE$EA6> ( L"W =L"W =TE$EA6>>
Kree =TE$>
Step : )Kinished*
Return =KRST>
Al'orit*m (or display t*e element o( t*e $ir$&lar lin% list
6SLA =YTE$EA6>
Step &: )Fhe!G 'or the empty list*
' TE$EA6 ( "#LL
Then write =IEmpty listH>
Return
Step +: )rint the desire node*
Repeat while L"W =TE$EA6>[ ( TE$EA6
Mrite ="K- =TE$EA6>>
Step : )Kinished*
Return
-
8/20/2019 Ds Full File
50/113
PROGRAM CO!:
;in!lude
stru!t node YlinG@
C@
stru!t node Ylast@
typede' stru!t node Y"-6E@
publi!:
NNFonstru!tor is de'ined Fir!ularXLinGed=>
?last("#LL@C
void !reateXlist=int>@
void addatbeg=int>@
void adda'ter=intint>@
void del=>@
void display=>@
C@
NNA !ir!ular list !reated in this 'un!tion
void Fir!ularXLinGed::!reateXlist=int num>
?"-6E tmp@
NN"ew node is !reated
tmp ( ="-6E>new=stru!t node>@
-
8/20/2019 Ds Full File
51/113
tmp52in'o ( num@
i' =last (( "#LL>
?last ( tmp@
tmp52linG ( last@
C
else
?tmp52linG ( last52linG@ NYadded at the end o' listYN
last52linG ( tmp@
last ( tmp@
C
CNYEnd o' !reateXlist=>YN
NNThis 'un!tion will add new node at the beginning
void Fir!ularXLinGed::addatbeg=int num>
?"-6E tmp@
tmp ( ="-6E>new=stru!t node>@
tmp52in'o ( num@tmp52linG ( last52linG@
last52linG ( tmp@
CNYEnd o' addatbeg=>YN
NNKun!tion to add new node at any position o' the !ir!ular list
void Fir!ularXLinGed::adda'ter=int numint pos>
?"-6E tmp@
int i@
( last52linG@
NN'inding the position to insert a new node
'or=i(0@ i < pos5&@ i33>
? ( 52linG@
i' = (( last52linG>
?!out
-
8/20/2019 Ds Full File
52/113
return@
C
CNYEnd o' 'orYN
NN!reating the new node
tmp ( ="-6E>new=stru!t node>@
tmp52linG ( 52linG@
tmp52in'o ( num@
52linG ( tmp@
i'=((last> NYElement inserted at the endYN
?last(tmp@C
CNYEnd o' adda'ter=>YN
NNKun!tion to delete a node 'rom the !ir!ular linGed list
void Fir!ularXLinGed::del=>
?int num@
i'=last (( "#LL>
?!out@
return@
C
( last52linG@
i'=52in'o (( num>
-
8/20/2019 Ds Full File
53/113
?tmp ( @
last52linG ( 52linG@
NNdeleting the node
delete=tmp>@
return@
C
while=52linG [( last>
?i'=52linG52in'o (( num> NYElement deleted in betweenYN
?tmp ( 52linG@
52linG ( tmp52linG@
delete=tmp>@
!out
-
8/20/2019 Ds Full File
54/113
return@
C
( last52linG@
!out
?NN$enu options!out
?!out
-
8/20/2019 Ds Full File
55/113
!o.!reateXlist=m>@
C
breaG@
!ase +:!out@
breaG@
!ase 1:!o.del=>@
breaG@
!ase 7:!o.display=>@
breaG@!ase 8:e,it=0>@
de'ault:!out
-
8/20/2019 Ds Full File
56/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
57/113
-
8/20/2019 Ds Full File
58/113
To Implement "ta$%s &sin' lin%ed list.
ALGORITHM:
P-"H OP!RATIO#
Suppose T- is a pointer whi!h is pointing towards the topmost element o' the
sta!G. T- is "#LL when the sta!G is empty. 6ATA is the data item to be pushed.
Step &: nput the 6ATA to be pushed
Step +: Freat a "ew "ode
Step : "ew"ode] 6ATA ( 6ATA
Step 1: "ew"ode] "e,t ( T-
Step 7: T- ( "ew"ode
Step 8: E,it
POP OP!RATIO#
Suppose T- is a pointer whi!h is pointing towards the topmost element o' the
sta!G. T- is "#LL when the sta!G is empty. TE$ is pointer variable to hold any nodes
address. 6ATA is the in'ormation on the node whi!h is Dust deleted.
Step &: ' =T- is eual to "#LL>
=a> 6isplay IThe sta!G is emptyH
Step +: Else
=a> TE$ ( T-
=b> 6isplay IThe popped element T- ] 6ATAH
=c> T- ( T- ] "e,t
=d > TE$ ] "e,t ( "#LL
=e> Kree the TE$ node
Step : E,it
-
8/20/2019 Ds Full File
59/113
PROGRAM CO!:
;in!lude;in!lude@
int pop=>@
!lass SXLL
?private:
stru!t node
?int data@
node YlinG@
C@
nodeY top@
publi!:
SXLL=>
?top("#LL@C
void push=int n>
?node Ytmp@
tmp(new node@
i'=tmp(("#LL>
?!out
-
8/20/2019 Ds Full File
60/113
!out
-
8/20/2019 Ds Full File
61/113
while=top[("#LL>
?tmp(top@
top(top52linG@
delete tmp@
C
C
C@
void main=>
?!lrs!r=>@
SXLL s@
int !hnum@
do
?!out
-
8/20/2019 Ds Full File
62/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
63/113
To Implement +&e&es &sin' lin%ed list.
ALGORITHM:
P-"H OP!RATIO#
REAR is a pointer in ueue where the new elements are added. KR-"T is a pointer
whi!h is pointing to the ueue where the elements are popped. 6ATA is an element to be
pushed.
Step &: nput the 6ATA element to be pushed.
Step +: Freat a "ew "ode
Step : "ew"ode] 6ATA ( 6ATA
Step 1: "ew"ode] "e,t ( "#LL
Step 7: '=REAR not eual to "#LL>
(a) REAR ] ne,t ( "ew"ode@Step 8: REAR ("ew"ode@
Step : E,it
POP OP!RATIO#
REAR is a pointer in ueue where the new elements are added. KR-"T is a pointer
whi!h is pointing to the ueue where the elements are popped. 6ATA is an element popped
'rom the ueue.
Step &: ' =KR-"T is eual to "#LL>
=a> 6isplay IThe Oueue is emptyH
Step +: Else
=a> 6isplay IThe popped element is KR-"T ] 6ATAH
=b> '=KR-"T is not eual to REAR>
=i> KR-"T ( KR-"T ] "e,t
=c> Else
=d > KR-"T ( "#LL@
Step : E,it
-
8/20/2019 Ds Full File
64/113
PROGRAM CO!:
;in!lude;in!lude@
int del=>@
!lass OXLL
?private:
stru!t node
?int data@
node YlinG@
C@
node Y'rontYrear@
publi!:
OXLL=>?'ront("#LL@
rear("#LL@
C
void add =int n>
?node Ytmp@
tmp(new node@
i'=tmp(("#LL>
?!out@
-
8/20/2019 Ds Full File
65/113
!out@
!out
-
8/20/2019 Ds Full File
66/113
OXLL=>
?i'='ront(("#LL>
?return@C
node Ytmp@
while='ront[("#LL>
?tmp('ront@
'ront('ront52linG@
delete tmp@
C
C
C@
void main=>
?!lrs!r=>@
OXLL @
int !hnum@
do ?!out
-
8/20/2019 Ds Full File
67/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
68/113
To Implement inary "ear$* Tree.
ALGORITHM:
Insertion:
"EM"-6E is a pointer variable to hold the address o' the newly !reated node. 6ATA
is the in'ormation to be pushed.
Step &: nput the 6ATA to be pushed and R--T node o' the tree.
Step +: "EM"-6E ( Freate a "ew "ode.
Step : ' =R--T (( "#LL>
=a> R--T("EM "-6E
Step 1: Else ' =6ATA < R--T n'o>=a> R--T ( R--T L!hild
=b> oTo Step 1
Step 7: Else ' =6ATA 2 R--T n'o>
=a> R--T ( R--T R!hild
=b> oTo Step 1
Step 8: ' =6ATA < R--T n'o>
=a> R--T LFhild ( "EM"-6E
Step : Else ' =6ATA 2 R--T n'o>
=a> R--T RFhild ( "EM"-6E
Step : Else
=a> 6isplay =I6#LFATE "-6EH>
=b> ET
Step 9: "EM "-6E n'o ( 6ATA
Step &0: "EM "-6E LFhild ( "#LL
Step &&: "EM "-6E RFhild ( "#LL
Step &+: ET
"ear$*in':
Step &: nput the 6ATA to be sear!hed and assign the address o' the root node to R--T.
Step +: ' =6ATA (( R--T n'o>
=a> 6isplay IThe 6ATA e,ist in the treeH
-
8/20/2019 Ds Full File
69/113
=b> oTo Step 8
Step : ' =R--T (( "#LL>
=a> 6isplay IThe 6ATA does not e,istH
=b> oTo Step 8
Step 1: '=6ATA 2 R--Tn'o>
=a> R--T ( R--TRFhild
=b> oTo Step +
Step 7: '=6ATA < R--Tn'o>
=a> R--T ( R--TL!hild
=b> oTo Step +
Step 8: E,it
eletion:
"-6E is the !urrent position o' the tree whi!h is in under !onsideration. L-F is
the pla!e where node is to be repla!ed. 6ATA is the in'ormation o' node to be deleted.
Step &: Kind the lo!ation "-6E o' the 6ATA to be deleted.
Step +: ' ="-6E ( "#LL>
=a> 6isplay I6ATA is not in treeH=b> E,it
Step : '="-6E L!hild ( "#LL>
=a> L-F ( "-6E
=b> "-6E ( "-6E RFhild
Step 1: '="-6E RFhild( ("#LL>
=a> L-F ( "-6E
=b> "-6E ( "-6E LFhild
Step 7: '=="-6E L!hild not eual to "#LL> UU ="-6E R!hild not eual to "#LL>>
=a> L-F ( "-6E RFhild
Step 8: Mhile=L-F L!hild not eual to "#LL>
=a> L-F ( L-F L!hild
Step : L-F L!hild ( "-6E L!hild
Step : L-F RFhild( "-6E RFhild
Step 9: E,it
-
8/20/2019 Ds Full File
70/113
PROGRAM CO!:
;in!lude
!lass %ST
?
stru!t node
?int in'o@
stru!t node Yl!hild@
stru!t node Yr!hild@
C@
typede' stru!t node Y"-6E@
publi!:stru!t node Yroot@
%ST=>
?root("#LL@C
NNpubli! 'un!tions de!larations
void 'ind=int"-6E Y"-6E Y>@
void !aseXa="-6E"-6E>@
void !aseXb="-6E"-6E>@
void !aseX!="-6E"-6E>@
void insert=int>@
void del=int>@
void inorder="-6E>@
C@
-
8/20/2019 Ds Full File
71/113
NNKun!tion to 'ind the item 'orm the tree
void %ST::'ind=int item"-6E Ypar"-6E Ylo!>
?"-6E ptrptrsave@
i'=root(("#LL> NYtree emptyYN
?Ylo!("#LL@
Ypar("#LL@
return@
C
i'=item((root52in'o> NYitem is at rootYN
?Ylo!(root@
Ypar("#LL@
return@
C
NYnitiali/e ptr and ptrsaveYN
i'=item
-
8/20/2019 Ds Full File
72/113
Ylo!("#LL@ NNtem not 'ound
Ypar(ptrsave@
C
void %ST::!aseXa="-6E par"-6E lo!>
?i'=par(("#LL> NNtem to be deleted is root node
?root("#LL@C
else
?i'=lo!((par52l!hild>
?par52l!hild("#LL@C
else
?par52r!hild("#LL@C
C
C
void %ST::!aseXb="-6E par"-6E lo!>
?"-6E !hild@ NNnitiali/e !hild
i'=lo!52l!hild(("#LL> NNtem to be deleted is l!hild
?!hild(lo!52l!hild@C
else NNtem to be deleted is r!hild
?!hild(lo!52r!hild@C
i'=par(("#LL > NYtem to be deleted is root nodeYN
?root(!hild@C
else
?i'= lo!((par52l!hild> NYitem is l!hild o' its parentYN
?par52l!hild(!hild@C
else NYitem is r!hild o' its parentYN
?par52r!hild(!hild@C
C
CNYEnd o' !aseXb=>YN
-
8/20/2019 Ds Full File
73/113
void %ST::!aseX!="-6E par"-6E lo!>
?"-6E ptrptrsavesu!parsu!@
NYKind inorder su!!essor and its parentYN
ptrsave(lo!@
ptr(lo!52r!hild@
while=ptr52l!hild[("#LL>
?ptrsave(ptr@
ptr(ptr52l!hild@
C
su!(ptr@
parsu!(ptrsave@
i'=su!52l!hild(("#LL UU su!52r!hild(("#LL>
?!aseXa=parsu!su!>@C
else
?!aseXb=parsu!su!>@C
i'=par(("#LL> NYi' item to be deleted is root node YN?root(su!@C
else
i'=lo!((par52l!hild>
?par52l!hild(su!@C
else
?par52r!hild(su!@C
su!52l!hild(lo!52l!hild@
su!52r!hild(lo!52r!hild@
CNYEnd o' !aseX!=>YN
NNThis 'un!tion will insert an element to the tree
void %ST::insert=int item>
? "-6E tmpparentlo!ation@
'ind=itemUparentUlo!ation>@
-
8/20/2019 Ds Full File
74/113
i'=lo!ation[("#LL>
?!out
?root(tmp@C
else
?i'=itemYN
NNKun!tion to delete a node
void %ST::del=int item>
?"-6E parentlo!ation@
i'=root(("#LL>
?!out
-
8/20/2019 Ds Full File
75/113
return@
C
i'=lo!ation52l!hild(("#LL UU lo!ation52r!hild(("#LL>
?!aseXa=parentlo!ation>@C
i'=lo!ation52l!hild[("#LL UU lo!ation52r!hild(("#LL>
?!aseXb=parentlo!ation>@C
i'=lo!ation52l!hild(("#LL UU lo!ation52r!hild[("#LL>
?!aseXb=parentlo!ation>@C
i'=lo!ation52l!hild[("#LL UU lo!ation52r!hild[("#LL>
?!aseX!=parentlo!ation>@C
delete=lo!ation>@
CNYEnd o' del=>YN
NNKun!tion 'or norder traversal
void %ST::inorder="-6E ptr>
?i'=root(("#LL>?!out@
C
C
void main=>
?!lrs!r=>@
int !hoi!enum@
-
8/20/2019 Ds Full File
76/113
%ST bo@
while=&>
?NN$enu options
!outYN
-
8/20/2019 Ds Full File
77/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
78/113
To Implement Tree Traversal.
ALGORITHM:
PR!OR!R TRA/!R"AL
Step &: Visit the root node.
Step +: Traverse the le't sub tree in preorder.
Step : Traverse the right sub tree in preorder.
I#OR!R TRA/!R"AL
Step &: Traverse the le't sub tree in inorder.
Step +: Visit the root node.
Step : Traverse the right sub tree in inorder.
PO"TOR!R TRA/!R"AL
Step &: Traverse the le't sub tree in postorder.
Step +: Traverse the right sub tree in postorder.
Step : Visit the root node.
-
8/20/2019 Ds Full File
79/113
PROGRAM CO!:
;in!lude;de'ine ES &
;de'ine "- 0
!lass %ST
?private:
stru!t lea'
?int data@
lea' Yl@
lea' Yr@
C@
stru!t lea' Yp@
publi!:
%ST=>
?p("#LL@C
void 'indparent=int nint U'oundlea' YUparent>
?lea' Y@
'ound("-@
parent("#LL@
i'=p(("#LL>
?return@C
(p@
while=[("#LL>
?i'=52data((n>
?'ound(ES@
-
8/20/2019 Ds Full File
80/113
return@
C
i'=52data2n>
?parent(@
(52l@
C
else
?parent(@
(52r@
C
C
C
void add=int n>
?int 'ound@
lea' YtYparent@
'indparent=n'oundparent>@ i'='ound((ES>
?!out
-
8/20/2019 Ds Full File
81/113
void traverse=>
?int !@
!out
?!out
-
8/20/2019 Ds Full File
82/113
void post=lea' Y>
?i'=[("#LL>
?post=52l>@
post=52r>@
!out@
get!h=>@
C
-
8/20/2019 Ds Full File
83/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
84/113
To per(orm Insertion "ort on an array.
ALGORITHM:
Let A be a linear array o' n numbers A )&* A )+* A )* ...... A )n*......Swap be a temporaryvariable to inter!hange the two values. os is the !ontrol variable to hold the position o' ea!h pass.
Step &: nput an array A o' n numbers
Step +: nitiali/e i ( & and repeat through steps 1 by in!rementing i by one.=a> ' =i < ( n P &>=b> Swap ( A )*=c> os ( i P &
Step : Repeat the step i' =Swap < A)os* and =os 2( 0>>=a> A )os3&* ( A )os*=b> os ( os5&
Step 1: A )os 3&* ( Swap
Step 7: E,it.
-
8/20/2019 Ds Full File
85/113
PROGRAM CO!:
;in!lude
void main=>
?!lrs!r=>@
int a)+0*tntempD@
!out
-
8/20/2019 Ds Full File
86/113
!out
-
8/20/2019 Ds Full File
87/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
88/113
To per(orm &))le "ort on an array.
ALGORITHM:
Let A be a linear array o' n numbers. Swap is a temporary variable 'or swapping =or inter!hange>the position o' the numbers.
Step &: nput n numbers o' an array A
Step +: nitiali/e i ( 0 and repeat through step 1 i' =i < n>
Step : nitiali/e j ( 0 and repeat through step 1 i' = j < n P i P &>
Step 1: ' =A) j* 2 A) j 3 &*>=a> Swap ( A) j*
=b> A) j* ( A) j 3 &*=c> A) j 3 &* ( Swap
Step 7: 6isplay the sorted numbers o' array A
Step 8: E,it.
-
8/20/2019 Ds Full File
89/113
PROGRAM CO!:
;in!ludevoid main=>
?!lrs!r=>@
int a)+0*tntemp@
!out
?temp(a)D*@
a)D*(a)D3&*@
a)D3&*(temp@
C
C
!out
-
8/20/2019 Ds Full File
90/113
!out
-
8/20/2019 Ds Full File
91/113
To per(orm "ele$tion "ort on an array.
ALGORITHM:
Let A be a linear array o' n numbers A )&* A )+* A )* A )G* A )G3&* A )n*. Swap be atemporary variable 'or swapping =or inter!hanging> the position o' the numbers. Min is the variableto store smallest number and Loc is the lo!ation o' the smallest element.
Step &: nput n numbers o' an array A
Step +: nitiali/e i ( 0 and repeat through step7 i' =i < n P &>
=a> min ( a)i*
=b> lo! ( i
Step : nitiali/e j ( i 3 & and repeat through step 1 i' = j < n P &>
Step 1: ' =a) j* < min>
=a> min ( a) j*
=b> lo! ( j
Step 7: ' =lo! [ ( i>
=a> swap ( a)i*
=b> a)i* ( a)lo!*
=c> a)lo!* ( swap
Step 8: 6isplay Ithe sorted numbers o' array AH
Step : E,it.
-
8/20/2019 Ds Full File
92/113
PROGRAM CO!:
;in!ludevoid main=>
?!lrs!r=>@
int a)+0*tntempminXinde,@
!out
?minXinde,(D@C
C
temp(a)i*@
a)i*(a)minXinde,*@
a)minXinde,*(temp@
!out
-
8/20/2019 Ds Full File
93/113
!out
-
8/20/2019 Ds Full File
94/113
To per(orm +&i$% "ort on an array.
ALGORITHM:
(&n$tion quicksort =array> i( length=array> 2 & pivot :0 select any element of array le't :0 (irst inde1 o( array right :0 last inde1 o( array 2*ile le't right 2*ile array)le't* < pivot le't :( le't 3 & 2*ile array)right* 2 pivot right :( right 5 &
i( le't right s2ap array)le't* 2it* array)right* le't :( le't 3 & right :( right 5 & ui!Gsort=array (rom (irst inde1 to right> ui!Gsort=array (rom le't to last inde1>
-
8/20/2019 Ds Full File
95/113
PROGRAM CO!:
;in!ludeint partition=intY int int>@
void ui!G=intY int int>@
void ui!G=int arr)*int lowerint higher>
?int D@
i'=higher2lower>
?D(partition=arrlowerhigher>@
ui!G=arrlowerD5&>@
ui!G=arrD3&higher>@
C
C
int partition=int arr)*int lowerint higher>?int Drightle'ttemp@
right(lower3&@
le't(higher@
D(arr)lower*@
while=right
?le't55@C
i'=right
-
8/20/2019 Ds Full File
96/113
C
C
temp(arr)lower*@
arr)lower*(arr)le't*@
arr)le't*(temp@
return le't@
C
void main=>
?!lrs!r=>@
int na)+0*highlow@
!out
-
8/20/2019 Ds Full File
97/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
98/113
To per(orm "*ell "ort on an array.
ALGORITHM:
void shellXsort 3int Ya int n4
5 int h i D G@ (or 3h ( n@ h N( 6@4
5 (or 3i ( h@ i < n@ i334
5G ( a7i8@ (or 3 D ( i@ D 2( h UU G < a7 D 5 h8@ D 5( h4
5a7 D8 ( a7 D 5 h8@9 a7 D8 ( G@ 9 99
PROGRAM CO!:
;in!lude void main=>
?int inDgaptempa)0*@
!lrs!r=>@
!out
-
8/20/2019 Ds Full File
99/113
?'or=i(0@i
?temp(a)i*@
'or=D(i@D20 UU a)D5gap*2temp@D(D5gap>
?a)D*(a)D5gap*@C
a)D*(temp@
C
C
!out
-
8/20/2019 Ds Full File
100/113
To per(orm Mer'e "ort on an array.
ALGORITHM:
(&n$tion mergeXsort=m> i( length=m> & ret&rn m var list le't right result var integer middle ( length=m> N + (or ea$* , in m &p to middle add , to le't (or ea$* , in m a(ter or e&al middle add , to right le't ( mergeXsort=le't>
right ( mergeXsort=right> result ( merge=le't right> ret&rn result
(&n$tion merge=le'tright> var list result 2*ile length=le't> 2 0 or length=right> 2 0 i( length=le't> 2 0 and length=right> 2 0 i( 'irst=le't> 'irst=right> append 'irst=le't> to result le't ( rest=le't> else append 'irst=right> to result right ( rest=right> else i( length=le't> 2 0 append 'irst=le't> to result le't ( rest=le't> else i( length=right> 2 0 append 'irst=right> to result right ( rest=right> end 2*ile
ret&rn result
-
8/20/2019 Ds Full File
101/113
PROGRAM CO!:
;in!lude void $ergeSort=int arrays)* int temp)* int si/e>@
void mXsort=int arrays)* int temp)* int le't int right>@
void merge=int arrays)* int temp)* int le't int mid int right>@
int arrays)0*@
int temp)0*@
void $ergeSort=int arrays)* int temp)* int si/e>
?mXsort=arraystemp0si/e5&>@C
void mXsort=int arrays)* int temp)* int le't int right>
?int mid@ i'=right2le't>
?mid(=right3le't>N+@
mXsort=arraystemple'tmid>@
mXsort=arraystempmid3&right>@
merge=arraystemple'tmid3&right>@
C
C
void merge=int arrays)* int temp)* int le't int mid int right>
?int ile'tXendnumXeletempXpos@
le'tXend(mid5&@
tempXpos(le't@
numXele(right5le't3&@
while==le't
-
8/20/2019 Ds Full File
102/113
?i'=arrays)le't*
?temp)tempXpos*(arrays)le't*@
tempXpos(tempXpos3&@
le't(le't3&@
C
else
?temp)tempXpos*(arrays)mid*@
tempXpos(tempXpos3&@
mid(mid3&@
C
C
while=le't
-
8/20/2019 Ds Full File
103/113
!in22n@
!out
-
8/20/2019 Ds Full File
104/113
"t&dy o( Floyd Wars*alls Al'orit*m.
ALGORITHM:
A dire!ted graph with $ nodes is aintained in memory by the adDa!en!y matri, A. Thisalgorithm 'inds the =%oolean> path matri, o' the graph .
Step &: Repeat 'or 4(&+..$)nitialise *
' A)4* (0 then set p)4*:(0Else: Set )4* :(&
Step +: Repeat steps and 1 'or G(&+ .$)update *
Step :Repeat step 1 'or (&+..$:
Step 1:Repeat 'or 4(&+..$Set )4*:()4* V )W* f )W4*
Step 7:E,it
-
8/20/2019 Ds Full File
105/113
PROGRAM CO!:
;in!lude;de'ine in'inity 9999
;de'ine ma, &0
int minimum=int aint b>
?i'=a
?return a@C
else
?return b@C
C
void display=int matri,)ma,*)ma,*int n>
?int iD@ 'or=i(0@i
?!out
-
8/20/2019 Ds Full File
106/113
!out
-
8/20/2019 Ds Full File
107/113
O-TP-T "CR!!#:
-
8/20/2019 Ds Full File
108/113
To per(orm Heap "ort on an array.
ALGORITHM:
"SERT-" -K A "-6E:
6> AT TE %E""" -K A L"WE6 LST
Step &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?Allo!ate 'ree spa!eC
"ew(new LinG
Step : ?Read value o' in'ormation part o' a new nodeC
n'o)"ew*( Value
Step 1: ?LinG address part o' the !urrently !reated node with the address o' startC "e,t)"e,t*(Start
Step 7: ?"ow assign address o' newly !reated node to the startC
Start("ew
Step 8: E,it.
E> AT TE E"6 -K A L"WE6 LSTStep &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?Allo!ate 'ree spa!eC
"ew(new LinG
Step : ?Read value o' in'ormation part o' a new nodeC
n'o)"ew*( Value
Step 1: ?$ove the pointer to the end o' the listC
Repeat while "ode
-
8/20/2019 Ds Full File
109/113
K> AT A 6ESRE6 LAFEME" "-6E "#$%ER S W"-M"Step &: ?Fhe!G 'or 'ree spa!eC
' new ( "#LL output I-VERKL-MH and e,it.
Step +: ?nitiali/atonC
"odeXnumber(0
"ode(Start."e,t)oints to 'irst node o' the list*
revious(Address o' Start)Assign address o' start to previous*
Step : ?Read node number that we want to insertC
nput nsertXnode
Step 1: ?er'orm insertion operationC
Repeat through Step 7 while "-6E
-
8/20/2019 Ds Full File
110/113
Else ?6elete 'irst nodeC
"e,t)revious*("e,t)"ode* ?$ove pointer to ne,t node in the listC
Kree the spa!e asso!iated with "ode
Step : E,it.
6> KR-$ TE E"6 -K A L"WE6 LSTStep &: ?nitiali/ationC
"ode(start."e,t ?oints to the 'irst node in the listC
revious(assign address o' start
"odeXnumber(0
Step +: ?Fhe!G list is empty or notC ' node("#LL
-utput I#"6ERKL-MH and e,it.
Step : ?S!an the list to !ount the number o' node in the listC
Repeat while node
-
8/20/2019 Ds Full File
111/113
PROGRAM CO!:
;in!lude void main=>
?int b)0*noiD!ptemp@
!lrs!r=>@
!outN+@
i'=b)p*
?temp(b)p*@
b)p*(b)!*@
b)!*(temp@
C
!(p@
Cwhile=![(0>@
C
'or=D(no5&@D2(0@D55>
?temp(b)0*@
b)0*(b)D*@
b)D*(temp@
-
8/20/2019 Ds Full File
112/113
p(0@
do
?!(+Yp3&@
i'==b)!*UU!
?!33@C
i'=b)p*
-
8/20/2019 Ds Full File
113/113
O-TP-T "CR!!#: