Dsa Lab Manual Krishna Gopal Lodhi
Dsa Lab Manual Krishna Gopal Lodhi
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
ORI
ENTALI
NSTI
TUTEOFSCI
ENCE&T
ECHNOL
OGY,BHOPAL
DEPARTMENTOFARTI
FICI
ALI
NTEL
LIGENCE&MACHI
NEL
EARNI
NG
L
ABMANUAL
Na
me:-DATAST
RUCTURE
Co
urs
eCo
de:-AL
-303
Co
urs
e:-B.
Tec
h
Se
ssi
on:-J
uly
-De
c,2023
Pr
eparedBy - Su
bmitt
edto:-
Ro
llNo-0 105AL221117
0105AL221104 Prof.San
deepPi
pl
ot
ia
Name-Moh amma
Krishna dshab
gopal an.
lodhi CS
E-AI
ML,OI
ST
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
S
YLL
ABUS
ORI
ENTAL
INS
TITUTEOFSCI
ENCE&TECHNOL
OGY,
BHOPALDEPARTMENTOF
I
NFORMATI
ONTECHNOL
OGY
L
IST
OFEXPERI
MENT
S
S.
No Na
meo
fEx
per
ime
nt Co
urs
e
Ou
tco
me
1 Wr
it
eap
rog
ramt
oin
ser
tane
leme
nti
nar
ray
. C
O2
2 Wr
it
eap
rog
ramt
ode
let
eane
leme
nti
nar
ray
. C
O2
3 Wr
it
eap
rog
ramt
ose
arc
hane
leme
nti
nth
ear
rayu
sin
gLi
nea
rSe
arc
h C
O2
4 Wr
it
eap
rog
ramt
ose
arc
hane
leme
nti
nth
ear
rayu
sin
gBi
nar
ySe
arc
h. C
O2
5 Wri
teaprogr
amtoperf
ormth
ef ol
l
owingoper
ati
oninMa
tri
x:
1.
Addi
ti
on2.
Subt
rac
ti
on3.
Mul
tip
li
cati
on4.
Tra
nspos
e
C
O2
6 Wri
teaprogr
amtoperf
ormthefol
l
owi
ngope
rat
ionons
tri
ngsus
ings
tri
ng
fu
nct
ion
s:1.Addi
ti
on2.Cop
ying3.Re
ver
se4.Leng
tho
fS t
ri
ng C
O2
7 Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngBu
bbl
eSo
rt C
O5
8 Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngS
ele
cti
onS
ort
. C
O5
9 Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngI
nse
rti
onS
ort C
O5
1
0 Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngQu
ickS
ort
. C
O5
1
1. Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngMe
rgeS
ort
. C
O5
Wr
iteaprog
ramtha
tusesst
ackoper
ati
onst
oco
nve
rtag
ive
nin
fix
1
2.
Ex
pres
sio
nintoi
tspos
tfi
xequiv
ale
nt. C
O3
1
3. Wr
it
eap
rog
ramt
ome
rget
wos
ort
eda
rra
yin
too
nes
ort
eda
rra
y. C
O2
1
4. Wr
it
eap
rog
ramt
oimp
leme
nts
tac
kus
inga
rra
yan
dli
nke
dli
st
. C
O3
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
1
5. Wr
it
eap
rog
ramt
oimp
leme
ntq
ueu
eus
inga
rra
yan
dli
nke
dli
st C
O3
1
6. Wri
teapro
gra
mtoi
nse
rta
nel
eme
nti
nth
ebe
gin
nin
gan
den
dofs
ing
ly
l
in
kedli
st
.
C
O4
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-01
M:
AI Wr
it
eap
rog
ramt
oin
ser
tane
leme
nti
nar
ray
.
ALGORI
THM:LetLAbeaLi
nearArr
ay(
unor
der
ed)wi
thNelement
sandKisapos
it
ivei
nte
ger
su
chthatK<
=N.Fo
ll
owi
ngi
sthealg
ori
thmwher
eITEMisi
nse
rte
di nt
oth
eKthp
osit
iono
fLA.
1
.St
art
2
.Se
tJ=N
3
.Se
tN=N+
1
4
.Re
pea
tst
eps
5an
d6wh
il
eJ>
=K
5
.Se
tLA[
J+1]
=LA
[J]
6
.Se
tJ=J
-1
7
.Se
tLA[
K]=I
TEM
8
.St
op.
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontodi
spl
aytheelement
sofana
rra
y
v
oiddis
play
Arr
ay(i
ntarr
[]
,ints
ize){
f
or(i
nti=0;i<s i
ze;++
i){
cou
t<<arr
[i
]<<"";
}
co
ut<<endl
;
}
/
/Fu n
cti
ont oins
ertanel
ementatas p
eci
fie
dp osi
ti
oni
nthear
ray
v
oidinse
rtEl
e ment
(i
ntarr
[]
,int
&s i
ze,i
ntposi
ti
on,in
tit
em){
//Checkifposi
ti
onisval
i
d
i
f(posi
tion<1||posi
ti
on>size+1){
cout<<"In
vali
dposi
ti
onforins
ert
ion
."<<endl
;
ret
u r
n;
}
/
/Shif
tel
ement
stomak
es pac
efort
hen
ewe
leme
nt
f
or(i
ntj=si
ze;j>
=pos
iti
on;--j
){
a
rr[
j]=ar
r[
j-1];
}
/
/Inse
rtthen
ewel
ementa
tth
esp
eci
fi
edp
osi
ti
on
a
rr[
posi
ti
on-1]=i
tem;
/
/Inc
reme
ntt
hes
izeo
fth
ear
ray
+
+si
ze;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;//Ma
ximu
msi
zeo
fth
ear
ray
i
ntl
inear
Arr
ay[
MAX_SIZE]
;
i
nts
ize,pos
it
ion
,ite
m;
/
/Inputthesi
zeofthear
ray
c
out<<"Ent
erthes
izeoft
hea r
ray
:";
c
in>>si
z e
;
/
/Inputarr
ayele
me nt
s
c
out<<"En
ter"<
<size<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<s i
ze;++i
){
c
in>>li
near
Array
[i]
;
}
/
/In
putp
osi
ti
ona
ndi
temt
oin
ser
t
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
c
out<<"
Entert
hep
osit
iont
oins
ert(
1to"<
<si
ze+1<
<")
:";
c
in>>posi
ti
on;
c
out<<"
Entert
hei
temtoin
ser
t:"
;
c
in>>it
em;
/
/Inser
tth
ee l
emen
tatthespec
if
iedposi
ti
on
i
nse
rtEl
ement
(l
in
ear
Arr
ay,si
ze,po
sit
ion
,it
em);
//Displ
ayt h
eu pdat
eda r
ray
cout<<"Update
da r
raya f
teri
nser
ti
on:"
;
dis
p l
ayArr
a y
(l
inear
Arra
y,size
);
r
etu
rn0;
}
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
t e
rthesi
zeo ft
hearray
:5
En
t e
r5e l
ementsf
orthearr
ay :
12345
En
t e
rtheposi
ti
ontoin
sert(1to6)
:3
En
t e
rtheit
emt oi
nse
rt:10
Update
darrayaft
eri
nsert
ion:1210345
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-02
AI
M:Wr
it
eap
rog
ramt
ode
let
eane
leme
nti
nar
ray
.
ALGORITHM:Cons
ide
rLAisalin
earar
raywit
hNe l
ement
sandKisapo
siti
veint
egersu
ch
t
h
th
atK<=N.Fol
lo
wingist
heal
gor
it
hmtodel
eteane
lemen
tavai
l
abl
eatt
heK posi
ti
onofLA.
1.S
tar
t
2.S
etJ
=K
3.Re
pea
tst
eps
4an
d5wh
il
eJ<
N
4.S
etL
A[J
]=L
A[J
+1]
5.S
etJ
=J+
1
6.S
etN=N-1
7.S
top
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontodi
spl
aytheelement
sofana
rra
y
v
oiddis
play
Arr
ay(i
ntarr
[]
,ints
ize){
f
or(i
nti=0;i<s i
ze;++
i){
cou
t<<arr
[i
]<<"";
}
co
ut<<endl
;
}
/
/Fu n
cti
ont odel
etea ne l
ementataspeci
fiedpos
it
ioni
nth
ear
ray
v
oiddele
te El
ement(
inta r
r[
],in
t&siz
e,intposi
ti
on){
//Checkifposi
ti
oni sval
id
i
f(posi
tion<1||positi
on>s i
ze){
cout<<"In
val
idp osi
tio
nf o
rdelet
ion.
"<<endl
;
ret
u r
n ;
}
/
/Shif
tel
ement
stofi
l
ltheg
aplef
tbythede
let
ede
leme
nt
f
or(i
ntj=pos
it
ion-1;j<s
ize-1;+
+j){
a
rr[
j]=ar
r[
j+1];
}
//Decr
eme
ntt
hes
izeo
fth
ear
ray
--si
ze;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;/
/Ma
ximu
msi
zeo
fth
ear
ray
i
ntl
inear
Arr
ay[
MAX_SI
ZE];
i
nts
ize,pos
it
ion
;
/
/Inputthesi
zeofthear
ray
c
out<<"Ent
erthes
izeoft
hea r
ray
:";
c
in>>si
z e
;
/
/Inputarr
ayele
me nt
s
c
out<<"En
ter"<
<size<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<s i
ze;++i
){
c
in>>li
near
Array
[i]
;
}
/
/Inputposi
ti
ontodel
ete
c
out<<"En
terthepo
sit
iontode
let
e(1t
o"<
<si
ze<
<")
:";
c
in>>posi
ti
on;
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
//Del
eteth
ee l
emen
tatthespec
if
iedposi
ti
on
del
ete
Element
(l
in
ear
Arr
ay,si
ze,pos
it
ion
);
//Displ
ayt h
eu pdat
eda r
ray
cout<<"Update
da r
raya f
terdel
eti
on:"
;
dis
p l
ayArr
a y
(l
inear
Arra
y,size)
;
r
etu
rn0;
}
1
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
t e
rthesi
zeo ft
hearr
ay:5
En
t e
r5e l
ementsf
orth
ea rr
ay:
12345
En
t e
rtheposi
ti
ontodel
ete(1to5)
:3
Update
darra
ya ft
erdel
eti
on:1245
1
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-03
AI
M:Wr
it
eap
rog
ramt
ose
arc
hane
leme
nti
nth
ear
rayu
sin
gLi
nea
rSe
arc
h.
ALGORITHM:Linearse
archi
sav e
rybas
icands
impl
esear
cha
lgo
rit
hm.InLin
earsearc
h,we
se
a r
cha nele
me n
to rval
uei
nag i
vena
rraybyt
rav
ers
ingt
hear
rayfro
mt h
es t
art
ing
,ti
llt
he
desi
rede l
ementorval
ueisf
oun
d.
I
tcompar
esthee
lementt
obes e
arch
e dwit
hal
lth
ee l
eme
n t
sp r
esenti
nthearr
aya n
dwh e
n
t
hee l
emen
tismat
che
ds uc
ces
sfu
ll
y,itre
tur
nst
heindexoftheele
me n
tinth
ea rr
ay,el
seit
r
etu
rN-1.
L
inearS
ear
chi
sap
pli
edo
nun
sor
tedo
run
orde
redl
i
sts
,wh
ent
her
ear
efe
were
leme
ntsi
na
l
i
st.
Asi
mpl
eap
pro
achi
stodol
i
nea
rse
arc
h,i
.
e
1.Sta
rtf
romt
hel
eftmo
ste
leme
nto
far
r[
]an
don
ebyo
nec
omp
arexwi
the
ache
leme
nto
f
arr
[]
2.
Ifxma
tch
eswi
tha
nel
eme
nt,r
etu
rnt
hei
nde
x.
3.I
fxdo
esn
’tma
tchwi
tha
nyo
fel
eme
nts
,re
tur
n-1
.
1
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontoperf
ormlinearsear
chinanarr
ay
i
ntl
ine
arSearc
h(i
ntarr[
],intsi
ze,i
ntkey
){
f
or(i
nti=0;i<s i
z e
;+ +i
){
i
f(arr
[i
]==k ey){
ret
urni;/
/Re t
u r
nt h
eindexifel
ementi
sfo
und
}
}
r
etur
n-1;//Retur
n-1i fel
ementisno
tfound
}
i
ntmai
n(){
c
ons
tintMAX_S
IZE=100;//Ma
ximu
msi
zeo
fth
ear
ray
i
ntl
inear
Arr
ay[
MAX_
SIZE]
;
i
nts
ize,key
;
/
/Inputthesi
zeofthear
ray
c
out<<"Ent
erthes
izeoft
hea r
ray
:";
c
in>>si
z e
;
/
/Inputarr
ayele
me nt
s
c
out<<"En
ter"<
<size<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<s i
ze;++i
){
c
in>>li
near
Array
[i]
;
}
/
/Inputtheel
eme
nttos
ear
ch
c
out<<"En
tert
heele
men
ttose
arc
h:"
;
c
in>>key;
/
/Perf
orml
i
nears
ear
ch
i
nti
ndex=l
ine
arS
ear
ch(
li
nea
rAr
ray
,si
ze,k
ey)
;
//Di
splayth
er e
sult
i
f(in
de x!=-1){
cout<<"
Elementfo
u n
da ti
nde
x:"<<i
ndex<<endl
;
}el
se{
cout<<"
Elementnotfou
ndinthea
rra
y."<
<e n
dl;
}
r
etu
rn0;
}
1
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
tert
hesi
zeofth
earra
y:5
En
ter5el
emen
tsfo
rth
ea rr
ay:
12345
En
tert
heel
ementt
osear
ch:3
El
emen
tfo
undatin
dex:2
1
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-04
AI
M:Wr
it
eap
rog
ramt
ose
arc
hane
leme
nti
nth
ear
rayu
sin
gBi
nar
ySe
arc
h.
ALGORI
THM:Bin
aryS
ear
chi
sus
edwi
ths
ort
eda
rra
yorl
i
st.I
nbi
nar
yse
arc
h,wef
ol
lo
wth
e
fo
ll
owi
ngst
eps
:
1
.Wes
tar
tbyc
omp
ari
ngt
hee
leme
ntt
obes
ear
che
dwi
tht
hee
leme
nti
nth
emi
ddl
eoft
he
l
i
st/
arr
ay.
2
.Ifweg
etama
tch
,we
ret
urnt
hei
nde
xoft
hemi
ddl
eel
eme
nt.
3
.Ifwedon
otg
etama
tch
,wec
hec
kwh
eth
ert
hee
leme
ntt
obes
ear
che
disl
esso
rgr
eat
ert
han
i
nva
luet
hant
hemi
ddl
eel
eme
nt.
4
.Ift
hee
leme
nt/
numbe
rtobes
ear
che
disg
rea
teri
nva
luet
hant
hemi
ddl
enu
mbe
r,t
henwe
p
ickt
hee
leme
ntso
nth
eri
ghts
ideo
fth
emi
ddl
eel
eme
nt(
ast
hel
i
st/
arr
ayi
sso
rte
d,h
enc
eon
t
her
igh
t,wewi
l
lha
vea
llt
hen
umbe
rsg
rea
tert
hant
hemi
ddl
enu
mbe
r),a
nds
tar
tag
ainf
rom
t
hes
tep1.
5
.If
the
ele
men
t/n
umbe
rto
bes
ear
che
dis
les
ser
inv
alu
eth
ant
hemi
ddl
enu
mbe
r,t
hen we p
ick t
he
e
leme
ntso
nth
ele
fts
ideo
fth
emi
ddl
eel
eme
nt,a
nds
tar
tag
ainf
romt
hes
tep1.
Bi
nar
yS ea
rchi
sus
efu
lwh
ent
her
ear
ela
rgen
umbe
rofe
leme
ntsi
nana
rra
yan
dth
ey
ar
esort
ed.
1
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
a
)It
era
tiv
eAp
pro
ach
:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Func
tio
nt operf
ormbina
ryse
archinaso
rte
dar
ray
i
ntbi
naryS
ea r
ch(
intar
r[
],i
nts
ize,i
ntke
y){
i
ntl
eft=0;
i
ntri
ght=s i
ze-1;
wh
il
e(l
eft<
=righ
t){
i
ntmi
d=l e
ft+(r
igh
t-l
eft
)/2;
/
/Checkift
hekeyispre
sentatt
hemiddl
e
i
f(a
rr[
mid]==key){
r
etu
rnmid;/
/Re t
urnthei
ndexife
leme
nti
sfo
und
}
/
/Ifkeyisgr
eat
er,i
gno
ret
hel
efth
alf
e
lsei
f(ar
r[mi
d]<key
){
l
eft=mid+1;
}
/
/Ifkeyiss
mal
l
er,i
gno
ret
her
igh
tha
lf
e
lse{
ri
ght=mi
d-1;
}
}
r
etu
rn-1;/
/Re
tur
n-1i
fel
eme
nti
sno
tfo
und
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;//Ma
ximu
msi
zeo
fth
ear
ray
i
nts
ortedAr
ray[
MAX_
SIZE];
i
nts
ize,key;
/
/Inputthesi
zeofthear
ray
c
out<<"Ent
erthes
izeoft
hesor
teda
rra
y:"
;
c
in>>si
z e
;
/
/Inputsor
tedarr
aye l
emen
ts
c
out<<"En
ter"<<si
ze<<"so
rte
del
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<s i
ze;++i
){
c
in>>sort
edArr
ay[i
];
}
1
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
/
/Inputtheel
eme
nttos
ear
ch
c
out<<"En
tert
heele
men
ttose
arc
h:"
;
c
in>>key;
/
/Perf
ormbi
narys
ear
ch
i
nti
ndex=bin
aryS
ear
ch(
sor
tedAr
ray
,si
ze,k
ey)
;
//Di
splayth
er e
sult
i
f(in
de x!=-1){
cout<<"
Elementfo
u n
da ti
nde
x:"<<i
ndex<<endl
;
}el
se{
cout<<"
Elementnotfou
ndinthea
rra
y."<
<e n
dl;
}
r
etu
rn0;
}
1
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
ert
hesiz
eo fthesor
teda r
ray
:7
Ent
er7so
rtedelement
sforthearr
ay:
10203040506070
Ent
ert
heeleme
n ttosear
ch:40
El
emen
tfoun
da tinde
x:3
1
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
b)Rec
urs
iveAp
pro
ach
:
#i
ncl
ude<
ios
trea
m>
u
sin
gna
mes
pac
est
d;
/
/Recur
sivefun
c t
iont
op erf
ormbina
rysear
ch
i
ntbi
narySea
rchRecur
siv
e(in
ta r
r[
],i
ntl
eft
,intr
igh
t,i
ntk
ey){
i
f(l
eft<=ri
gh t
){
i
ntmid=l eft+(r
ight-left
)/2;
/
/Checkift
hekeyispres
entatt
hemiddl
e
i
f(ar
r[
mid]==key){
r
etur
nmid;/
/Returnthei
ndexifel
emen
tisf
oun
d
}
//Ifk
eyisgreater
,sear
chintheri
ghtha
lf
els
eif(ar
r[
mid]<key){
ret
urnbin
arySearc
hRecu
rsi
ve(
arr
,mid+1,r
igh
t,k
ey)
;
}
//Ifk
eyissmal
l
er,s
earchint
helef
th a
lf
els
e{
ret
urnbi
nar
ySear
chRecu
rsi
ve(
arr
,lef
t,mi
d-1,k
ey)
;
}
}
r
etu
rn-1;/
/Ret
urn-1i
fel
emen
tisn
otf
oun
d
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;//Ma
ximu
msi
zeo
fth
ear
ray
i
nts
ortedAr
ray[
MAX_SI
ZE];
i
nts
ize,key;
/
/Inputthesi
zeofthear
ray
c
out<<"Ent
erthes
izeoft
hesor
teda
rra
y:"
;
c
in>>si
z e
;
/
/Inputsor
tedarr
ayelemen
ts
c
out<<"En
ter"<<si
ze<<"so
rtedel
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<s i
ze;++i
){
c
in>>sort
edArr
ay[i
];
}
/
/Inputtheel
emen
tt os
earc
h
c
out<<"En
tert
heelementt
osea
rch
:";
c
in>>key;
1
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
/
/Perf
ormbi
narysea
rchusi
ngrecur
siv
ea p
proa
ch
i
nti
ndex=bina
rySear
chRec
urs
ive(
sor
tedAr
ray
,0,si
ze-1,k
ey)
;
//Disp
layth
er es
ult
i
f(index!=-1){
cout<<"
Elementf
ou n
da ti
ndex
:"<<i
ndex<<en
dl;
}els
e{
cout<<"
Elementnotfou
ndinthea
rra
y."<
<endl
;
}
r
etu
rn0;
}
2
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
ert
hesiz
eo fthesor
teda r
ray
:7
Ent
er7so
rtedelement
sforthearr
ay:
10203040506070
Ent
ert
heeleme
n ttosear
ch:40
El
emen
tfoun
da tinde
x:3
2
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-05
AI
M:Wr
it
eap
rog
ramt
ope
rfo
rmt
hef
ol
lo
win
gop
era
tio
ninMa
tri
x:
1.Addi
ti
on2.
Subt
rac
ti
on3
.Mu
lt
ipl
i
cat
ion4.
Tra
nsp
ose
ALGORITHM:Theel
ement
sofama tr
ixorvect
orarenumbersfr
oman umbersyst
em,suc
ha s
therealnumber
s,th
ec ompl
exnumbers
,o rin
teg
e r
smo dul
oap r
ime
.T hen u
mbersyst
em
defi
nesh o
wt oaddandmu l
ti
pl
yn umber
s.Wec ane x
tendthesedefi
nit
ion
st oenc
ompass
addi
tio
na ndmul
ti
pl
ic
ati
onofmatr
ic
es.
Wedefi
nema
tri
xa ddi
ti
ona
sfol
lo
ws.I
fA=( aij
)an
dB=(
bij
)ar
emnma
tri
ces
,th
ent
hei
r
mat
ri
xsumC=(ci
j)=A+Bisth
emnma tr
ixde
fin
edby
c
ij
=a i
j+bi
j
f
ori=1,2,
...,mandj=1,2,...,n.Tha
tis,ma
tri
xaddi
ti
oni
spe
rfo
rme
dco
mpo
nen
twi
se.A
z
eromatr
ixisth
eiden
tit
yformatr
ixaddi
tio
n:
A+
0=A
=0+A.
Ifisan u
mbe
ra n
dA=( ai
j)isama t
ri
x,thenA=(ai
j)i
sthescal
armu
lt
ipl
eofAobt
ain
edby
mult
ipl
yi
ngeac
ho fi
tsel
eme n
tsby.Asas p
eci
alc
ase,wedefi
netheneg
ati
veo
fama t
ri
xA=
(ai
j)t
obe-1A=-A,s oth
a tthei
jt
he n
tryof-Ais-aij
.Th
us,
A+(
-A)
=0
=(
-A)+A.
Gi
venthi
sde
fin
iti
on,weca
nde
fin
ema
tri
xsu
btr
act
iona
sth
eaddi
ti
ono
fth
ene
gat
iveo
fa
mat
ri
x:A-B=A+( -B)
.
Wede fin
ema t
ri
xmu lt
ipl
i
cati
ona sfol
lo
ws.Wes t
artwit
ht woma t
ri
cesAa ndBt h
atare
co
mp at
ibl
einthesens
et ha
tthen u
mbero fcol
umnsofAe qual
sthenumberofr
owsofB.(
In
gene
ral,anexpre
ssio
nc ont
aini
ngama t
rixp r
oductABi salway
sa ss
umedtoimpl
yt ha
t
matr
ic
esAa ndBa recompat
ibl
e.)I
fA=( ai
j)i
sa nmnma tr
ixandB=( bjk
)isannpmatr
ix,
t
henthe i
rmat
ri
xp r
oductC=ABi sthempma tr
ixC=( c
ik)
,where
2
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Func
tio
nt odis
pla
yama tr
ix
v
oiddi
spl
ayMatri
x(i
nt**matr
ix,i
ntr
ows
,in
tco
ls){
f
or(i
nti=0;i<r ows;++i
){
f
or(i
ntj=0;j<c ol
s;++
j){
cou
t<<ma t
ri
x[i]
[j
]<<"";
}
c
out<<endl
;
}
}
/
/Funct
iontoper
formmatri
xaddit
ion
v
oidaddMatr
ic
es(i
nt*
*A,int
**B,i
nt**re
sul
t,i
ntr
ows
,in
tco
ls){
f
or(in
ti=0;i<r ows
;++i
){
fo
r(in
tj=0;j<c ol
s;++j
){
res
ult
[i
][
j]=A[i
][
j]+B[i
][
j]
;
}
}
}
/
/Funct
iontoper
formma tri
xsubtr
act
ion
v
oidsu
btrac
tMatr
ice
s(in
t**A,int
**B,in
t**r
esu
lt
,in
tro
ws,i
ntc
ols
){
f
or(i
nti=0;i<r ows;++i
){
f
or(in
tj=0;j<c ol
s;++j
){
res
ult
[i
][
j]=A[i][
j]-B[i]
[j
];
}
}
}
/
/Func
tio
nt operfo
rmma tri
xmu lti
pl
ic
ati
on
v
oidmul
ti
plyMatr
ices
(int
**A,int**B,i
nt*
*res
ult
,in
tro
wsA,i
ntc
ols
A,i
ntc
ols
B){
f
or(i
nti=0;i<r o ws
A;++i){
f
or(i
ntj=0;j<c ol
sB;++j){
res
ult
[i]
[j
]=0;
fo
r( i
ntk=0;k<c ol
sA;+ +
k){
resul
t[
i]
[j]+=A[i
][k]*B[k]
[j
];
}
}
}
}
/
/Funct
iontope
rfor
mma tri
xtrans
pos
e
v
oidtr
anspo
seMatr
ix(
int
**matr
ix,i
nt*
*re
sul
t,i
ntr
ows
,in
tco
ls){
f
or(i
nti=0;i<r o
ws;++i
){
f
or(in
tj=0;j<c ol
s;++j
){
2
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
r
esu
lt
[j
][
i]=ma
tri
x[i
][
j]
;
}
}
}
/
/Funct
iontodeall
oca
teme mo
ryf o
ramat
rix
v
oiddeal
loc
ateMatr
ix(
int
**matr
ix,i
ntr
ows
){
fo
r(in
ti=0;i<r ows;++
i){
del
ete[
]ma t
ri
x[i
];
}
del
ete
[]matri
x;
}
i
ntmai
n(){
i
ntr
ows,co
ls;
/
/Inputdimens
ion
so ft
hemat
ri
ces
c
out<<"Ente
rthenumbero
fro
wsformat
ri
ces
:";
c
in>>rows;
c
out<<"Ente
rthenumbero
fco
lumn
sformat
ri
ces
:";
c
in>>cols
;
/
/Inputma
tri
cesAa ndB
i
nt*
*A=n ewint
*[r
ows];
i
nt*
*B=n ewint
*[r
ows];
f
or(i
nti=0;i<rows;++i
){
A[
i]=newint
[c
ols
];
B[
i]=newint
[c
ols]
;
}
c
out<<"
Ent
erel
eme n
tsf o
rma t
ri
xA:
"<<e
ndl
;
f
or(i
nti=0;i<rows;++i
){
f
or(i
ntj=0;j<co l
s;++j
){
ci
n>>A[
i]
[j
];
}
}
c
out<<"
Ent
erel
eme n
tsf o
rma t
ri
xB:
"<<e
ndl
;
f
or(i
nti=0;i<rows;++i
){
f
or(i
ntj=0;j<co l
s;++j
){
ci
n>>B[
i]
[j
];
}
}
/
/All
ocat
eme mor
yforres
u l
tmatr
ice
s
i
nt*
*sum=n e
wint*
[r
ows]
;
i
nt*
*dif
ference=newint
*[r
ows];
i
nt*
*product=newint
*[
rows]
;
i
nt*
*tra
n s
poseA=newint
* [
col
s]
;
i
nt*
*tra
n s
poseB=newint*[
col
s]
;
2
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
f
or(i
nti=0;i<r ows;++i
){
su
m[i]=newint[c
ols
];
di
ffe
rence
[i]=newi nt
[c
ols
];
pr
oduct
[i
]=n ewint[
col
s];
}
f
or(i
nti=0;i<co
ls;++
i){
t
rans
pos
eA[
i]=ne wi
nt[
rows
];
t
rans
pos
eB[
i]=ne wi
nt[
rows
];
}
/
/Perf
ormma t
rixoper
ati
ons
a
ddMatr
ice
s(A,B,su
m,rows,co
ls)
;
s
ubt
ract
Matri
ces(
A,B,di
ffer
ence,r
ows
,co
ls)
;
/
/Tran
spos
ema t
ric
esAandB
t
ran
spos
eMatr
ix(
A,tr
ans
pos
eA,r
ows
,co
ls)
;
t
ran
spos
eMatr
ix(
B,tr
ans
pos
eB,r
ows
,co
ls)
;
//Displ
aytheres
ult
s
cout<<"Ma
tri
xA:"<<en
dl;
dis
p l
ayMat
rix
(A,ro
ws,col
s)
;
co
ut<<"Ma
tri
xB:"<
<e n
dl;
di
spl
ayMat
rix
(B,r
ows,col
s)
;
co
ut<<"Ma
tri
xSum(A+B):
"<<en
dl;
di
spl
ayMat
rix
(su
m,r
ows,c
ols
);
co
ut<<"Ma
tri
xDif
fer
enc
e(A-B):
"<<en
dl;
di
spl
ayMat
rix
(di
ff
ere
nce
,ro
ws,c
ols
);
co
ut<<"T
ransp
oseofMatr
ixA:
"<<e
ndl;
di
spl
ayMatr
ix(
tr
ans
pose
A,col
s,r
ows)
;
co
ut<<"T
ransp
oseofMatr
ixB:
"<<e
ndl;
di
spl
ayMatr
ix(
tr
ans
pose
B,col
s,r
ows)
;
//Deal
l
ocat
eme mory
deal
l
ocat
eMatr
ix(A,rows)
;
deal
l
ocat
eMatr
ix(B,rows);
deal
l
ocat
eMatr
ix(sum,rows);
deal
l
ocat
eMatr
ix(dif
ference,r
ows);
deal
l
ocat
eMatr
ix(product
,rows)
;
deal
l
ocat
eMatr
ix(tr
an s
poseA,col
s)
;
deal
l
ocat
eMatr
ix(tr
an s
poseB,col
s)
;
r
etu
rn0;
}
2
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUTPUT
:
En
tert
henumbe
rofr
owsfo
rmatr
ic
es:2
En
tert
henumbe
rofc
olu
mnsfo
rmatr
ice
s:2
En
tere
leme
ntsf
orma
tri
xA:
12
34
En
tere
leme
ntsf
orma
tri
xB:
56
78
Ma
tri
xA:
12
34
Ma
tri
xB:
56
78
Mat
ri
xSu
m(A+B)
:
68
1012
Mat
ri
xDi
ff
ere
nce(
A-B)
:
-4-4
-4-4
Tr
ans
pos
eofMa
tri
xA:
13
24
Tr
ans
pos
eofMa
tri
xB:
57
68
2
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-06
AI
M:Wr
it
eap
rog
ramt
ope
rfo
rmt
hef
ol
lo
win
gop
era
tio
nons
tri
ngsu
sin
gst
ri
ngf
unc
ti
ons
:
1.Addi
ti
on2.
Cop
yin
g3.
Rev
ers
e4.
Len
gtho
fSt
ri
ng
AL
GORI
THM:
Tocomparetwos t
ri
ng s
,con
cat
enat
estr
ing
s,co
pyonestri
ngtoanot
her&perfo
rmvariou
sstr
ing
manip
ula
tiono p
erati
ons.Wecanper
for
msuchoper
ati
onsusi
ngth
ep re
-defi
nedfunct
ion
sof
“s
tri
ng.
h”h eaderfi
le.Inor
dertous
ethes
estri
ngfun
cti
onsyoumusti
ncl
udestr
ing
.hfil
ein
yourCprogram.
2
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Functi
ontocalc
ula
tet
hel e
ng t
hofas
tri
ng
i
ntstr
ingL
ength(
cons
tchar
*str){
i
ntlengt
h=0;
whi
le(st
r[l
engt
h]!='
\0'){
++l
ength;
}
re
turnlen
gth;
}
/
/Fu n
cti
ontoco p
yo nestri
ngtoano
the
r
v
oidstr
ingCopy(
constch
ar*sourc
e,c
har
*de
sti
nat
ion
){
in
ti=0;
whil
e(sour
ce[i
]!='\0'){
desti
nati
on[i
]=s ou
rce[i
];
++i;
}
dest
inat
ion[
i]='\0';
}
/
/Funct
iont
oconc
ate
natet
wostr
ing
s
v
oidst
ri
ngCon
cat
enat
e(c
ons
tch
ar*str
1,c
ons
tch
ar*s
tr2,c
har
*re
sul
t){
i
nti=0,j=0;
//Copystr
1tores
ult
whil
e(st
r1[i
]!='
\0'){
resul
t[
j++
]=str
1[i+
+];
}
i=0;
//Copystr
2tores
ult
whil
e(st
r2[i
]!='
\0'){
resul
t[
j++
]=str
2[i+
+];
}
r
esu
lt
[j
]='
\0'
;
}
/
/Funct
iontor
evers
eas tri
ng
v
oidst
ri
ngReve
rse(
char*st
r){
i
ntl
ength=str
ingLengt
h(s
tr)
;
f
or(i
nti=0,j=l en
g t
h-1;i<j;+
+i,--j
){
c
hartemp=s t
r[
i];
s
tr[
i]=st
r[j
];
2
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
s
tr[
j]=t
emp
;
}
}
i
ntmai
n(){
c
harstr
1[100]
,st
r2[
100]
,re
sul
t[
200]
;
/
/1.S t
rin
gAdditi
on
c
out<<"Ent
erthefi
rs
tstrin
g:";
c
in.
getl
ine(
st
r1,si
zeo
f(s
tr1))
;
c
out<<"Ent
erthesec
onds t
ri
ng:"
;
c
in.
getl
i
n e
(st
r2,si
zeo
f(s
tr2)
);
s
tri
ngCo
nca
ten
ate
(st
r1,s
tr2,r
esu
lt
);
c
out<
<"S
tri
ngAddi
ti
on(
Con
cat
ena
tio
n):"<
<re
sul
t<<e
ndl
;
/
/2.S t
ri
ngCopyi
ng
s
tri
ngCopy(
st
r1,r
esu
lt
);
c
out<<"St
ri
ngCopy(s
tr1c
opi
edt
ore
sul
t)
:"<
<re
sul
t<<e
ndl
;
/
/3.S t
ri
ngRever
se
s
tri
ngRever
se(
res
ult
);
c
out<<"St
ri
ngRevers
e:"<
<re
sul
t<<e
ndl
;
/
/4.Leng
thofS tr
ing
i
ntl
engt
h=s tr
ingLengt
h(re
s u
lt
);
c
out<
<"Leng
tho fStr
ing:"<<len
gth<
<en
dl;
r
etu
rn0;
}
2
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erth
efir
ststr
ing:Hel
lo
Ent
erth
esecondstr
ing:Worl
d
St
ri
ngAddi
ti
on(Concatenat
ion
):Hel
lo
Wo r
ld
St
ri
ngCopy(s
tr1copiedtoresu
lt
):Hel
lo
St
ri
ngRever
se:ol
le
H
Le
ngthofSt
ri
ng:5
3
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-07
AI
M:Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngBu
bbl
eSo
rt.
AL
GORI
THM:
Fo
ll
owi
ngaretheste
p sin
volve
dinbubblesort(
forsorti
ngag iv
enarr
ayina scen
dingorde
r):
1.S t
art
ingwit
ht hefi
rstel
ement(
index=0) ,c
omp arethecur
ren
te l
eme n
twiththenext
el
ementofthearray
.
2.Ifthecurr
ente l
ementisgr
eaterth
ant h
en extelementoft
hea r
ray,swapthem.
3.Ifthecurr
ente l
ementisl
essth
a nthenextel
e ment,movet
othen e
x tel
ement.Rep
eat
St
ep1.
3
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
IMPLEMENTATI
ON:
#i
ncl
ude<i
ost
rea
m>
u
sin
gna
mes
pac
est
d;
/
/Fu nct
iont ope r
formBu bbleS ort
v
oidbu bble Sort
(intarr[
],intn){
f
o r(inti=0;i<n-1;+ +i){
f
o r(intj=0;j<n-i-1;+ +j
){
/
/Co mp ar
ea djacente lementsandswapi
fth e
ya r
eint
hewr
ongo
rde
r
i
f( a r
r[
j]>a rr[
j+1] ){
//S wapa rr
[j
]a nda r
r[j+1]
inttemp=a rr[
j];
a r
r[
j]=a rr[
j+1] ;
a r
r[
j+1]=t emp ;
}
}
}
}
/
/Fu nct
iont odispl
a ythee l
eme n
tso fanarr
ay
v
oiddisp l
ay Array(i
nta r
r[]
,intn ){
f
o r(inti=0;i<n ;++i){
c
o ut< <arr[i
]<<"";
}
cout<<e n dl
;
}
i
ntma i
n (
){
constintMAX_ SIZE=100;/ /Ma xi
mu msi
zeofthea r
ray
i
n tarr[MAX_ SIZE];
i
n tn;
//Inp utth esizeo fthea r
ra y
cout<<" En t
erth enumbe ro feleme nt
sint
h ear
ray:";
ci
n> >n ;
//Inputarr
aye l
ement
s
cout<<"Ent
er"<<n<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
for(i
nti=0;i<n ;++i
){
c
in>>a r
r[
i]
;
}
//PerformBubbl
eSort
bubbl
e Sor
t(
arr,n
);
//Displ
aythesort
edarr
ay
cout<<"S
orte
dAr r
ayinAsc
endi
ngOr
der
:";
dis
p l
ayArr
ay(ar
r,n)
;
r
etu
rn0;
}
3
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erthenumbero
fe l
ement
sinthea
rra
y:6
Ent
er6e l
ement
sforthear
ray:
4218537128
Sor
tedArr
ayinAsc
e n
dingOrde
r:5812183742
3
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-08
AI
M:Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngS
ele
cti
onS
ort
.
ALGORITHM:Sel
ect
ionsor
tisasi
mples
orti
ngal
gor
ith
m.Thisso
rti
ngal
gor
ith
misanin-pl
ace
co
mp ar
ison
-basedal
gori
thminwhi
chth
el i
sti
sdiv
idedint
otwopart
s,t
hesort
edpar
tatthe
l
eftenda ndtheunsor
tedparta
ttherig
htend.Ini
ti
al
ly
,thesort
edpartisemp
tyandthe
unso
rte
dp arti
stheent
irel
ist
.
T
hes mal
le
ste l
ementi
ss el
ect
edfr
omt heunso
rtedarr
aya n
dswappe
dwi t
ht hele
ftmos
t
e
lement
,andthatel
ementbeco
mesapartoft
hesorte
darra
y.Thi
spr
ocessc
onti
nuesmovi
ng
u
nsor
tedarra
ybo u
ndar
ybyo neel
emen
tt ot
herigh
t.
T
hisa
lgo
rit
hmi
sno
tsu
ita
blef
orl
arg
eda
tas
etsa
sit
sav
era
gea
ndwo
rstc
asec
omp
lex
iti
es
2
a
reo
fΟ(
n )
,wh
ereni
sth
enu
mbe
rofi
tem.
S
tep
1–S
etMI
Ntol
oca
tio
n0
S
tep
2−S
ear
cht
hemi
nimu
mel
eme
nti
nth
eli
stS
tep3
−S
wapwi
thv
alu
eatl
oca
tio
nMI
N
S
tep
4−I
ncr
eme
ntMI
Nto
poi
ntt
one
xte
leme
ntS
tep5−
Re
pea
tun
till
i
sti
sso
rte
d
3
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontoperfor
mS el
e ct
ionSor
t
v
oidse
lect
ionS
o r
t(
intarr
[],intn){
f
or(i
nti=0;i<n-1;+ +
i){
/
/As s
umet hecurr
en tinde
xisth
emi
nimu
m
i
ntminIndex=i ;
/
/Findth
ei n
dexofthemini
mume
leme
nti
nth
ere
mai
nin
gun
sor
teda
rra
y
f
or(i
ntj=i+1;j<n ;++
j){
i
f(ar
r[
j]<arr
[mi
nIndex
]){
mi
nIndex=j;
}
}
/
/Swa pthef oundmin
imumeleme
ntwi
tht
hef
ir
ste
leme
nt
i
f(minIndex!=i){
//Swa parr[
i]andarr
[mi
nIn
dex]
i
n ttemp=a rr[
i]
;
arr[
i]=ar r
[minI
ndex]
;
arr[
minIndex]=temp;
}
}
}
/
/Funct
iontodi
spl
aytheele
ment
sofa
nar
ray
v
oiddis
play
Arr
ay(i
ntar
r[]
,in
tn){
f
or(i
nti=0;i<n ;+
+i){
cou
t<<arr
[i
]<<""
;
}
co
ut<<endl
;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;/
/Ma
ximu
msi
zeo
fth
ear
ray
i
ntar
r[MAX_
SIZE];
i
ntn;
/
/Inputthesi
zeofth
earr
ay
c
out<<"En
terthen
umbero
fel
eme
ntsi
nth
ear
ray
:";
c
in>>n;
/
/Inputarr
aye l
eme
n t
s
c
out<<"En
ter"<<n<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<n ;++i
){
c
in>>arr[
i]
;
3
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
/
/Perf
ormSel
ecti
onSo
rt
s
ele
cti
onS
ort
(ar
r,n)
;
//Displ
aythesort
edarr
ay
cout<<"S
orte
dAr r
ayinAsc
endi
ngOr
der
:";
dis
p l
ayArr
ay(ar
r,n)
;
r
etu
rn0;
}
3
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erthenumbero
fe l
ement
sinthea
rra
y:6
Ent
er6e l
ement
sforthear
ray:
4218537128
Sor
tedArr
ayinAsc
e n
dingOrde
r:5812183742
3
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-09
AI
M:Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngI
nse
rti
onS
ort
ALGORITHM:Thi
si sanin-plac
ec ompar
is
on-basedsor
ti
nga l
gor
it
hm.He re
,as ub-l
isti
s
maint
ainedwh
ichisal
wayss o
rte
d.Fo re
xample,t
helowe
rp ar
tofana r
rayismai
ntai
nedto
bes or
ted.Ane l
emen
twh ichist obei ns
ertedinthi
ss or
tedsub-l
ist
,h ast
of in
di t
s
ap
p r
opriat
epl
aceandthe
ni thastobeinser
tedthe
re.Henc
ethename,ins
erti
ons
ort.
T
hea rr
ayi sse
arc
hedsequ
enti
all
ya nduns
ort
edit
emsar
emo vedandinse
rtedi
ntoth
e
s
ort
eds u
b-li
st(
inthesa
mea r
ray)
.T h
isal
gor
it
hmisno
tsui
tabl
eforl
arg
eda t
aset
sa si
ts
2
a
verag
ea ndwor
stcas
ecomp
lex
ityareofΟ(
n),whe
renist
hen u
mbero
fitems
.
S
tep1−I
fi
tisthef
irs
te l
eme
nt,i
tis
alr
eady
sor
ted.
ret
urn
1;
S
tep2−Pickn
exte
lemen
t
S
tep
3−Co
mpa
rewi
tha
lle
leme
ntsi
nth
eso
rte
dsu
b-l
i
st
S
tep
4−S
hif
tallt
heel
ement
sint
hes
ort
eds
ub-l
i
stt
hati
sgr
eat
ert
hant
he
val
uetobesor
ted
S
tep
5−I
nse
rtt
hev
alu
e
S
tep
6−Re
pea
tun
til
l
ist
is
sor
ted
3
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontoper
for
mI ns
e r
ti
onSor
t
v
oidin
sert
ionS
ort
(i
ntarr
[],i
ntn){
f
or(i
nti=1;i<n ;++
i){
i
ntkey=a r
r[i
];
i
ntj=i-1;
//Moveele
men
tsgreat
ertha
nke
ytoo
nep
osi
ti
ona
hea
doft
hei
rcu
rre
ntp
osi
ti
on
whi
le(j>=0&&arr[
j]>ke
y){
arr
[j+1]=arr
[j
];
--j;
}
/
/Inse
rtt
hekeyi
nit
sco
rre
ctp
osi
ti
on
a
rr[
j+1]=ke
y;
}
}
/
/Funct
iontodi
spl
aytheele
ment
sofa
nar
ray
v
oiddis
play
Arr
ay(i
ntar
r[]
,in
tn){
f
or(i
nti=0;i<n ;+
+i){
cou
t<<arr
[i
]<<""
;
}
co
ut<<endl
;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;/
/Ma
ximu
msi
zeo
fth
ear
ray
i
ntar
r[MAX_
SIZE];
i
ntn;
/
/Inputthesi
zeofth
earr
ay
c
out<<"En
terthen
umbero
fel
eme
ntsi
nth
ear
ray
:";
c
in>>n;
/
/Inputarr
aye l
eme
n t
s
c
out<<"En
ter"<<n<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<n ;++i
){
c
in>>arr[
i]
;
}
/
/Perf
ormIns
erti
onSor
t
i
nse
rti
onS
ort
(ar
r,n);
3
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
//Displ
aythesort
edarr
ay
cout<<"S
orte
dAr r
ayinAsc
endi
ngOr
der
:";
dis
p l
ayArr
ay(ar
r,n)
;
r
etu
rn0;
}
4
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erthenumbero
fe l
ement
sinthea
rra
y:6
Ent
er6e l
ement
sforthear
ray:
4218537128
Sor
tedArr
ayinAsc
e n
dingOrde
r:5812183742
4
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-10
AI
M:Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngQu
ickS
ort
.
AL
GORI
THM:Qui
cks
orti
sah
igh
lye
ffi
ci
ents
ort
inga
lgo
rit
hma
ndi
sba
sedo
npa
rti
ti
oni
ngo
far
rayo
f
dat
ai n
tosmall
erarr
ays.Alar
gea r
rayi
spar
ti
ti
one
din
tot
woarr
ayson
eo fwh
ichh
oldsva
lue
ss ma
ll
er
th
ant hespe
c i
fi
edv al
ue,saypiv
ot,ba
sedonwhi
cht
hepar
ti
ti
onismadeandanoth
erarra
yh ol
ds
val
uesgre
atertha
nthep i
votval
ue.
Qui
cksor
tp a
rti
tio
nsanarrayandth
encall
sit
sel
frecu
rsi
vel
ytwic
etosor
tthet
wores
ult
ings
ubarra
ys.
Thi
salgo
rit
hmi squi
teef
fi
cien
tforl
arge
-size
dda t
asetsasit
saver
ageandwor
stc
asecompl
exi
tyare
ofΟ(n
2),wherenisthenumberofi
te .
ms
St
ep1−Choos
ethehigh es
ti
ndexvaluehaspiv
ot
St
ep2−Taket
wo v
ari
a blest
op o
intlef
ta ndrightofth
elistexc
ludin
gpi
votS
tep
3−leftpoi
ntstothel owindex
St
ep4−ri
ghtpoi
ntst
oth ehig
h
St
ep5−wh i
levaluea tlef
tislessthanpivotmoverig
ht
St
ep6−whil
eval
ueatr igh
tisgreaterthanp i
votmovelef
t
St
ep7−if
boths
tep5an dst
ep6doesnotmatch
swapleftandrig
htSt e
p8−if
l
eft≥rig
ht,thepointwh er
et heyme tisnewp i
vot
4
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontopart
it
iont h
ea rr
ayandret
urntheindexoft
hepiv
otele
men
t
i
ntpar
ti
ti
o n
(in
tarr[
],i
n tl
ow,inthi
gh){
i
ntpi
vot=a rr
[hi
gh];//Chooset
hehigh
estinde
xv al
ueast
hep i
vot
i
nti=low-1;/ /Initi
ali
zethein
dexofthesma l
l
erel
emen
t
f
or(i
ntj=low;j<h ig
h;++j
){
/
/Ifthecurr
ente l
ementi
ssma
ll
ert
hano
req
ualt
oth
epi
vot
i
f(arr
[j
]<=pivot){
//Swaparr[
i]andarr
[j
]
++i
;
swap(
arr
[i
],arr[
j]
);
}
}
/
/Swaparr
[i+1]andarr
[hi
gh](
piv
ot)
s
wap(
arr
[i+1],a
rr[
hig
h])
;
r
etu
rni+1;
}
/
/Func
tio
nt operf
ormQuickSor
t
v
oidqu
ickSo
rt(i
ntarr
[]
,in
tlow,i
nthig
h){
i
f(l
ow<h i
gh){
/
/Partit
iont
hea r
ray,ar
r[
p]isnowatt
herig
htp
lac
e
i
ntparti
ti
onI
ndex=p art
it
ion
(ar
r,l
ow,h
igh
);
/
/Recu
rsi
vel
ysortel
ementsbef
oreandaf
tert
hep
art
it
ioni
nde
x
q
uic
kSor
t(
arr
,lo
w,part
iti
onIn
dex-1 )
;
q
uic
kSor
t(
arr
,part
it
ion
Index+1,hig
h);
}
}
/
/Funct
iontodi
spl
aytheele
ment
sofa
nar
ray
v
oiddis
play
Arr
ay(i
ntar
r[]
,in
tn){
f
or(i
nti=0;i<n ;+
+i){
cou
t<<arr
[i
]<<""
;
}
co
ut<<endl
;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;/
/Ma
ximu
msi
zeo
fth
ear
ray
i
ntar
r[MAX_
SIZE];
i
ntn;
4
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
/
/Inputthesi
zeofth
earr
ay
c
out<<"En
terthen
umbero
fel
eme
ntsi
nth
ear
ray
:";
c
in>>n;
/
/Inputarr
aye l
eme
n t
s
c
out<<"En
ter"<<n<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<n ;++i
){
c
in>>arr[
i]
;
}
/
/Perf
ormQuic
kSo
rt
q
uic
kSor
t(
arr
,0,n-1)
;
//Displ
aythesort
edarr
ay
cout<<"S
orte
dAr r
ayinAsc
endi
ngOr
der
:";
dis
p l
ayArr
ay(ar
r,n)
;
r
etu
rn0;
}
4
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erthenumbero
fe l
ement
sinthea
rra
y:6
Ent
er6e l
ement
sforthear
ray:
4218537128
Sor
tedArr
ayinAsc
e n
dingOrde
r:5812183742
4
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-11
AI
M:Wr
it
epr
ogr
amf
ori
mpl
eme
nti
ngMe
rgeS
ort
.
AL
GORITHM:Mer
geso
rtisasor
ti
ngtec
hni
quebas
edondiv
ideandcon
que
rtech
niq
ue.Wi
th
wor
st-c
aset
imec
omp
lexi
tybe
ingΟ(nl
ogn)
,iti
soneo
fth
emo str
esp
ect
edal
gor
it
hms.
Mer
gesor
tfi
rs
tdi
vi
dest
hea
rra
yin
toe
qua
lha
lve
san
dth
enc
ombi
nest
hemi
nas
ort
ed
ma
nner
.
Mer
ges or
tk e
epsondiv
idi
ngtheli
stint
oe q
ualhal
vesunti
litcann
omo r
ebedivi
ded.By
def
ini
ti
on,ifitiso
nlyoneel
ementi
nt heli
st
,itissor
ted.The
n,merg
esortc
ombi
nesthe
smal
l
ersort
edlis
tsk
eepi
ngthene
wlists
ort
edtoo.
St
ep1−ifiti
sonl
yoneel
ementint
helis
titi
sa l
rea
dys ort
ed,r
etu
rn.
St
ep2−divi
deth
elis
tre
curs
ive
lyin
totwohal
vesuntili
tcannomorebedi
vi
ded.S
tep
3−me rgethes
mall
erl
i
stsi
ntonewli
stinso
rte
do rder.
4
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
/
/Funct
iontomergetwosubar
ray
sofar
r[
]
/
/Fir
stsubarr
ayisarr
[l
..
m]
/
/Secon
ds ubar
rayisarr
[m+1.
.
r]
v
oidmerge(
intar
r[
],in
tl,in
tm,intr
){
i
ntn1=m-l+1;
i
ntn2=r-m;
/
/Crea
tetempor
aryarr
ays
i
ntl
eft
[n1]
,ri
ght
[n2]
;
/
/Copydatatote
mp orar
yarr
aysl
eft
[]a
ndr
igh
t[]
f
or(i
nti=0;i<n1;i +
+)
l
eft
[i
]=a r
r[
l+i ]
;
f
or(i
ntj=0;j<n 2;j+
+)
r
ight
[j
]=a r
r[
m+1+j ];
/
/Merg
eth etempor
a r
yarra
ysbacki
n t
oa r
r[
l.
.
r]
i
nti=0;//Ini
tia
lindexoffi
rs
tsuba
r r
ay
i
ntj=0;//Ini
tia
lindexofsec
ondsubarr
ay
i
ntk=l;/
/I n
iti
alin
de xo
fme r
gedsubarr
ay
whi
le(i<n1&&j<n 2){
i
f(lef
t[
i]<
=right
[j]
){
arr[
k]=l
eft[
i]
;
i++;
}el
se{
arr[
k]=ri
ght[
j]
;
j++;
}
k++
;
}
//Copyth
er e
maini
nge
leme
ntso
fle
ft[
],i
fth
erea
rea
ny
whil
e(i<n1){
arr[
k]=lef
t[
i]
;
i++;
k++;
}
//Copyth
er e
maini
nge
leme
ntso
fri
ght
[]
,ift
her
ear
ean
y
whil
e(j<n2){
arr[
k]=ri
ght[
j]
;
j++;
k++;
4
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
}
}
/
/Functi
ontoperfo
rmMe r
geSort
v
oidmergeSo
rt(
intarr
[],i
ntl,i
ntr){
i
f(l<r){
//Sameas(l+r
)/2,butav
oidsov
erf
lo
wfo
rla
rgela
ndr
intm=l+( r-l)/2;
//S
ortf
ir
standseco
ndh a
lve
s
mer
geSo
rt(
arr
,l,m)
;
mer
geSo
rt(
arr
,m+1,r )
;
//Merg
et hesor
tedh
alv
es
merg
e(arr
,l,m,r
);
}
}
/
/Funct
iontodi
spl
aytheele
ment
sofa
nar
ray
v
oiddis
play
Arr
ay(i
ntar
r[]
,in
tn){
f
or(i
nti=0;i<n ;+
+i){
cou
t<<arr
[i
]<<""
;
}
co
ut<<endl
;
}
i
ntmai
n(){
c
ons
tintMAX_SIZE=100;/
/Ma
ximu
msi
zeo
fth
ear
ray
i
ntar
r[MAX_
SIZE];
i
ntn;
/
/Inputthesi
zeofth
earr
ay
c
out<<"En
terthen
umbero
fel
eme
ntsi
nth
ear
ray
:";
c
in>>n;
/
/Inputarr
aye l
eme
n t
s
c
out<<"En
ter"<<n<<"el
eme
ntsf
ort
hea
rra
y:"<
<en
dl;
f
or(i
nti=0;i<n ;++i
){
c
in>>arr[
i]
;
}
//Per
for
mMe rgeSo
rt
merg
eSort
(ar
r,0,n-1)
;
//Displ
aythesort
edarr
ay
cout<<"S
orte
dAr r
ayinAsc
endi
ngOr
der
:";
dis
p l
ayArr
ay(ar
r,n)
;
r
etu
rn0;
}
4
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Ent
erthenumbero
fe l
ement
sinthea
rra
y:6
Ent
er6e l
ement
sforthear
ray:
4218537128
Sor
tedArr
ayinAsc
e n
dingOrde
r:5812183742
4
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-12
AI
M:Wri
teapr
ogramt
hatu
sess
tac
kop
era
tio
nst
oco
nve
rtag
ive
nin
fixe
xpr
ess
ioni
ntoi
ts
po
stf
ixe
qui
val
ent
ALGORITHM:Le
t,Xisanari
thmet
ice
xpr
ess
ionwr
it
teni
nin
fixn
ota
tio
n.T
hisa
lgo
rit
hmf
inds
th
ee qu
ival
entpos
tfi
xexp
ressi
onY.
Pu
sh “(
“ont
oStack,anda dd“)”t
othee ndo fX.
Sc
an Xfr
omlef
ttori
ghtan
drepeatSt
ep3to6foreachel
ement
ofXunt
il
th
eStac
kisempt
y.
1.I fanop er
a n
dise ncount
ered,addi ttoY.
2.I faleftparent
he s
isisencounte
red,p us
hitont
oS ta
ck.
3.I fanop er
a t
orisencount
ered,then:
4.Re peatedl
yp opfromS t
acka ndaddt oYe ac
hope r
ator(
onthetopofSt
ack
)whi
chhas
thesamep rece
de nc
ea sorh i
gherp re
cedencet
ha noper
ato
r.
5.Addo pe rat
ortoS t
ack.
6.[ EndofI f
]
7.I farightparenthes
isisencounter
ed, t
hen:
8.Re peatedl
yp opfromS t
acka ndaddt oYe ac
hope r
ator(
onthetopofSt
ack
)un
tila
lef
tparenthesi
sisencounte
red.
9.Re movet helef
tPa r
enthes
is.
10.[EndofI f
]
11.[EndofI f
]
12.END.
5
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<
ios
tre
am>
u
sin
gna
mes
pac
est
d;
//Funct
iontoc
heckifth
echa
rac
teri
sano per
ato
r
booli
sOpera
tor
(ch
arch){
ret
urn(ch=='+'|
|ch=='
-'|
|ch=='*
'||c
h==' /
')
;
}
/
/Funct
iontog etth
ep re
cedenc
eo fanope
rat
or
i
ntget
Precedence(
charop){
i
f(op==' +
'||op=='-')
r
eturn1;
i
f(op==' *
'||op=='/'
)
r
eturn2;
r
etu
rn0;/ /Forotherchar
act
ersorin
val
ido
pera
tor
s
}
/
/Funct
iontoconverti
nfi
xexpr
essi
ontopo
stf
ixexp
res
sio
n
v
oidinf
ixT
oPostfi
x(c
onstc
har
*infi
x,ch
ar*p
ost
fix
){
ch
arstac
k[100];
i
nttop=-1 ;
i
ntpost
fix
Index=0;
a
utopus
h=[ &](
cha
rc){
s
tac
k[+
+to
p ]=c;
}
;
a
utopop=[&](
)->char{
i
f(t
op==-1)
r
etu
rn'\0';
r
etu
rnsta
ck[
top--]
;
}
;
a
utois
Stac
kEmp
ty=[&]
()->bo
ol{
r
etur
ntop==-1;
}
;
f
or(i
nti=0;i nfi
x[i]!
='\0' ;++i
){
ch
arc=i nfi
x [
i]
;
i
f(is
aln
um( c
)){
//Operand,addt op o
s t
fi
xe x
pres
sio
n
post
fi
x [
postf
ixIndex++]=c;
}el
seif(c==' (
'){
//Leftparenthesi
s,pushontost
ack
push
(c);
}el
seif(c==' )
'){
//Ri
gh tparenth
e s
is,popanda p
pendop
era
tor
sun
til'
(
'ise
nco
unt
ere
d
5
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
whil
e(!i
sSt
a c
kEmpty(
)&&stack
[to
p]!='(
'
){
postf
ix[
postf
ixI
ndex+
+]=p o
p();
}
//Removethe'('f
romthest
ack
i
f(!i
sStac
kEmp t
y()
)
pop()
;
}el
seif(i
sOperat
or(c
)){
//Operat
or,popandappen
do per
ator
swithhi
gheroreq
ualpr
ecedenc
e
whil
e(!i
sSt
a c
kEmpty(
)&&getPrec
edence
(st
ack
[to
p])>
=getPr
eceden
ce(c
)){
postf
ix[
postf
ixI
ndex+
+]=p o
p();
}
//Pushthecurr
entoper
ato
ro n
tothesta
ck
push
(c);
}
}
//Popandappendremai
nin
gopera
tor
sfr
omt
hes
tac
k
whil
e(!
isS
tack
Empty(
)){
post
fi
x[po
stf
ixI
ndex+
+]=p o
p()
;
}
p
ost
fi
x[p
ost
fi
xIn
dex
]='
\0'
;//Nu
ll
-te
rmi
nat
eth
epo
stf
ixe
xpr
ess
ion
}
i
ntmai
n (
){
c
onstintMAX_S
IZE=100;/
/Ma
ximu
msi
zeo
fth
ein
fixa
ndp
ost
fi
xex
pre
ssi
ons
c
harinfi
x[MAX_
SIZE];
c
harpostf
ix[
MAX_SIZE]
;
/
/Inputinf
ixexpr
ess
ion
c
out<<"Ent
eraninf
ixexpr
ess
ion
:";
c
in.
getl
i
n e
(in
fix
,MAX_SI
ZE);
/
/Conver
ttopost
fix
i
nfi
xTo
Postf
ix(
inf
ix,p
ost
fi
x);
/
/Dis
pla
ythep o
stf
ixexpre
ssi
on
c
out<
<"Pos
tfi
xexpres
sio
n:"<<po
stf
ix<
<en
dl;
r
etu
rn0;
}
5
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
terani
nfi
xe x
pre
ssi
on:(
a+b)
*c-(
d/e
)
Po
stf
ixex
pres
sio
n:ab+
c*de
/-
5
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-13
AI
M:Wr
it
eap
rog
ramt
ome
rget
wos
ort
eda
rra
yin
too
nes
ort
eda
rra
y.
ALGORITHM:Arr
aysar
eassu
medt obesort
e dinascen
dingor
der.Youe n
tert
woshor
tsort
ed
arr
aysa n
dcombinet
hemtogetalarg
erarray.Ifthearr
aysa
ren otsor
tedth
enyoucansort
th
emf ir
standthenus
etheme r
gefunct
ion,anoth
erme t
hodi
st ome r
getheman
dt hensort
th
ea rr
ay.Sor
ti
ngtwosma
ll
erarr
ayswil
ltak
el es
stimeascompar
edt osor
ti
ngabi
ga r
ray.
As
sume,thatbo t
ha rraysa r
es or
tedi na scendi
n gordera n
dwewa ntre s
ult
inga r
rayt o
mai
ntai
nt hes a
meo rder.Algo
rithmt ome r
g etwoa rr
aysA[0.
.m-1]a ndB[0.
.n-1]in
toa narray
C[
0..
m+n-1]i sasfol
lowi
ng :
1.I ntr
o duceread-indi
cesi ,
jtotravers
ea rraysAa ndB,acc
o r
dingl
y.Int
roducewrite
-
indexkt ostorepo s
it
iono fthefirs
tf r
eec el
lintheresul
tin
ga rr
ay.Byde f
aulti=j=k
=0 .
2.Ate achstep:ifbothindicesareinr ange(i<ma ndj<n ),choos
eminimumo f(A[i
],
B[j]
)a ndwriteittoC[k].Otherwis
eg otos t
ep4.
3.I ncreaseka ndindexo fthea rray
,a l
gori
thml o
catedmini
ma lval
ueat,byo ne
.Re pea
t
step2.
4.Co pyt herestv a
luesfr
o mt hea r
ray,whichindexisst
il
linra n
ge,totheresul
tin
ga rr
ay.
5
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
/
/Funct
iontomer
getwosor
tedarr
aysi
ntoo
nes o
r t
edar
ray
v
oidmergeS
ort
edAr
rays
(i
ntA[]
,in
tm,in
tB[]
,in
tn ,in
tC[
]){
i
nti=0,j=0,k=0;
//Compareel
ement
sfr
omAa
ndBa
ndi
nse
rti
ntoC
whil
e(i<m&&j<n ){
if(A[
i]<B[
j]
){
C[
k++]=A[
i++
];
}else{
C[
k++]=B[
j++
];
}
}
//Copyther
emai
nin
gel
eme
ntsf
romA,i
fan
y
whil
e(i<m){
C[k++
]=A[i
++]
;
}
//Copyther
emai
nin
gel
eme
ntsf
romB,i
fan
y
whil
e(j<n){
C[k++
]=B[j
++]
;
}
}
/
/Funct
iontodi
spl
aye l
ementso
fa nar
ray
v
oiddis
play
Arr
ay(i
ntarr
[]
,intsi
ze){
f
or(i
nti=0;i<s i
ze;++
i){
cou
t<<arr
[i
]<<"";
}
co
ut<<endl
;
}
i
ntmai
n (
){
c
onstintMAX_SI
ZE=100;
i
ntA[MAX_SIZE]
,B[
MAX_
SIZE]
,C[
MAX_
SIZE*2]
;//As
sumi
ngma
ximu
msi
zef
ort
her
esu
lt
inga
rra
y
i
ntm,n
;
/
/Inputsi
zeandele
me nt
sforthefi
rstar
ray
c
out<<"En
terth
esizeofthefi
rsta
r r
ay(A)
:";
c
in>>m;
c
out<<"En
ter"<<m<<"el
ementsforthefi
rs
tarr
ay(
A)i
nas
cen
din
gor
der
:"<
<en
dl;
f
or(i
nti=0;i<m;+ +i
){
c
in>>A[i
];
}
5
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
/
/Inputsi
zea n
de l
ement
sfort
hesec
ondarr
ay
c
out<<"En
terthesi
zeofthese
con
da r
ray(
B):"
;
c
in>>n;
c
out<<"En
ter"<<n<<"el
ement
sfort
hesec
ondarr
ay(
B)i
nas
cen
din
gor
der
:"<
<en
dl;
f
or(i
nti=0;i<n ;++i
){
c
in>>B[i]
;
}
//Merg
ethetwosort
edarra
y s
merg
eSort
edAr
ray
s(A,m,B,n,C);
//Displ
aythemer
gedsor
tedar
ray
cout<<"Mer
gedSo
rte
dArray(
C):"
;
dis
p l
ayArr
ay(
C,m+n);
r
etu
rn0;
}
5
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
tert
hesi
zeofth
efir
starra
y( A):5
En
ter5el
ement
sfo
rthefi
rstarr
a y(A)ina
sce
ndingor
der:
13579
En
tert
hesi
zeofth
esecondarra
y( B):4
En
ter4el
ement
sfo
rthesecondarray(B)i
nasce
ndin
go r
der:
2468
Mer
gedSo
rte
dArray(
C):123456789
5
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-14
AI
M:Wr
it
eap
rog
ramt
oimp
leme
nts
tac
kus
inga
rra
yan
dli
nke
dli
st
.
AL
GORI
THM:
)I
a mp
lemen
tat
iono
fst
acku
sin
gAr
ray
:
As
tac
kca
nbei
mpl
eme
nte
dus
inga
rra
yasf
ol
lo
ws.
.
.
Be
for
eimp
leme
nti
nga
ctu
alo
per
ati
ons
,fi
rs
tfo
ll
owt
hebe
lows
tep
stoc
rea
tea
nemp
tys
tac
k.S
tep
1-Ch
eckwh
eth
ers
tac
kisEMPT
Y.(
top==-1)
S
tep
2-I
fiti
sEMPT
Y,t
hendi
spl
ay"S
tac
kisEMPT
Y!!
!"
andt
ermi
nat
eth
efu
nct
ion
.
St
ep3-I fi
tisNOTEMPT
Y,thende
fin
eav
ari
abl
e'i
'an
din
iti
al
iz
ewi
tht
op.Di
spl
ays
tac
k[i
]va
luea
nd
dec
reme
ntival
uebyone(
i--)
.
S
tep
3-Re
pea
tabo
ves
tepu
nti
lIv
alu
ebe
come
s'0
'.
)I
b mp
lemen
tat
iono
fst
acku
sin
gLi
nkedL
ist
:
T
oimp
leme
nta
sta
cku
sin
gal
i
nke
dli
st
,we
nee
dto
set
the
fol
l
owi
ngt
hin
gsbe
for
eimp
leme
nti
nga
ctu
alo
per
ati
ons
.
S
tep1-I
ncl
udeal
lth
eh ea
derf
il
eswh
icha
reu
sedi
nth
epr
ogr
am.An
dde
cla
rea
llt
he
u
serdef
ine
dfun
cti
ons.
S
tep
2- De
fin
ea'
Node
'st
ruc
tur
ewi
tht
wome
mbe
rsda
taa
ndn
ext
.
S
tep3-De
fin
eaNo
dep
oin
ter'
t
op'
ands
eti
ttoNUL
L.
St
ep4-Impl
ementt
hemainme
tho
dbydi
spl
ayi
ngMe
nuwi
thl
i
sto
fop
era
tio
nsa
nd
makes
uit
abl
efu
ncti
onc
all
sint
hemai
nmet
hod.
5
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
a
)Us
ingAr
ray
:
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
c
ons
tin
tMAX_
SIZE=100;/
/Ma
ximu
msi
zeo
fth
est
ack
c
las
sStac
kArr
ay{
p
riv
ate
:
i
ntarr
[MAX_S
IZE]
;
i
nttop
;
p
ubl
ic:
S
tack
Arr
ay(
){
t
op=-1;//I
nit
ial
i
zet
opt
o-1t
oin
dic
atea
nemp
tys
tac
k
}
v
oidpush(i
ntval
ue){
i
f(top==MAX_ SI
ZE-1){
cout<<"
Stac
kOv e
rfl
ow!Can
notpu
shelemen
t"<<va
lue<
<en
dl;
ret
u r
n;
}
ar
r[+
+top]=val
u e
;
co
ut<<"Pus
hed"< <val
ue<<"t
othest
ack
."<<endl
;
}
v
oidpop(
){
i
f(to
p==-1){
cou
t< <"
Stac
kUnde
rfl
ow!Can
notpopf
romanempt
ysta
ck.
"<<e
ndl
;
ret
urn;
}
co
ut<<"Poppe
d"<<ar
r[t
op--]<
<"fr
omthest
ack
."<
<endl
;
}
v
oiddis
play
(){
i
f(to
p==-1){
cou
t<<"St
ackise
mp t
y."<
<en
dl;
ret
urn;
}
co
ut<<"St
ackele
ment
s:";
f
or(i
nti=t op
;i>=0;--i){
cou
t<<arr[
i]<
<"";
}
co
ut<<endl
;
}
}
;
i
ntma
in(
){
5
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
S
tac
kAr
rays
tac
k;
s
tac
k.p
ush
(10)
;
s
tac
k.p
ush
(20)
;
s
tac
k.p
ush
(30)
;
s
tac
k.di
spl
ay(
);
s
tac
k.po
p()
;
s
tac
k.di
spl
ay(
);
r
etu
rn0;
}
6
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
Pu
shed10tothest
ack
.
Pu
shed20tothest
ack
.
Pu
shed30tothest
ack
.
St
ackel
eme
n t
s:302010
Po
pped30fro
mt hest
ack
.
St
ackel
eme
n t
s:2010
6
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
b)Us
ingL
ink
edL
ist
:
#i
ncl
ude<
ios
trea
m>
u
sin
gna
mes
pac
est
d;
c
las
sNo
de{
p
ubl
i
c:
i
ntda
ta;
No
de*n
ext
;
No
de(
intv
alu
e){
da
ta=v
alu
e;
n
ext=n
ull
ptr
;
}
}
;
c
las
sSt
ack
Lin
kedL
ist{
p
riv
ate:
No
de*t
op;
p
ubl
i
c:
St
ack
Lin
kedL
ist
(){
t
op=n
ull
ptr
;//I
nit
ial
i
zet
opt
onu
ll
ptrt
oin
dic
atea
nemp
tys
tac
k
}
v
oidp
ush
(in
tva
lue){
No
de*n
ewNo
de=n
ewNo
de(
val
ue)
;
n
ewNo
de->
nex
t=t
op;
t
op=n
ewNo
de;
c
out<
<"Pu
shed"<
<va
lue<
<"t
oth
est
ack
."<
<en
dl;
}
v
oidp
op(
){
i
f(t
op==n
ull
ptr
){
c
out<
<"St
ackUn
der
fl
ow!Ca
nno
tpo
pfr
oma
nemp
tys
tac
k."<
<en
dl;
r
etu
rn;
}
No
de*t
emp=t
op;
t
op=t
op->
nex
t;
6
2
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
c
out<
<"Po
pped"<
<temp
->da
ta<
<"f
romt
hes
tac
k."<
<en
dl;
del
etet
emp
;
}
v
oiddi
spl
ay(
){
i
f(t
op==n
ull
ptr
){
c
out<
<"St
acki
semp
ty.
"<<en
dl;
r
etu
rn;
}
c
out<
<"St
ackel
emen
ts:"
;
No
de*c
urr
ent=t
op;
wh
il
e(c
urr
ent!
=nu
ll
ptr
){
c
out<
<cu
rren
t->
dat
a<<""
;
c
urr
ent=c
urr
ent
->n
ext
;
}
c
out<
<en
dl;
}
}
;
i
ntma
in(
){
St
ack
Lin
kedL
ists
tac
k;
s
tac
k.p
ush
(10)
;
s
tac
k.p
ush
(20)
;
s
tac
k.p
ush
(30)
;
s
tac
k.di
spl
ay(
);
s
tac
k.p
op(
);
s
tac
k.di
spl
ay(
);
r
etu
rn0;
}
6
3
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
Pu
shed10tothest
ack
.
Pu
shed20tothest
ack
.
Pu
shed30tothest
ack
.
St
ackel
eme
n t
s:302010
Po
pped30fro
mt hest
ack
.
St
ackel
eme
n t
s:2010
6
4
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-15
AI
M:Wr
it
eap
rog
ramt
oimp
leme
ntq
ueu
eus
inga
rra
yan
dli
nke
dli
st
.
AL
GORI
THM:
a
)Qu
eueOp
era
tio
nsu
sin
gAr
ray
Queuedatastru
c t
ureu s
inga r
rayc anbei mp l
eme nt
eda sfol
l
ows.
..
Befor
ewei mplementa c
tualoperations,fi
rstfol
lo
wt hebel
o wstepstocrea
tea ne mptyq u
eue.
Ste
p1-I ncl
udea l
ltheh eaderfileswh i
cha reusedinthep r
ograma nddefineac onsta
nt'SIZE'wi
th
spec
ifi
cval
ue .
Ste
p2-De cl
area l
ltheuserde f
ine df u
n c
tionswhichareusedinq ueueimpl
eme nt
a t
ion.Step
3-Cr eat
eao nedi mensi
on alar
ra ywitha bovede f
inedSIZE(intqueue[SIZE])
Ste
p4–De fi
net wointegervari
a bl
e s'
f r
ont'and'rea
r'andini
tial
izebothwith'
-1'.
(in
tf r
ont=-1,r
ear
=-1)
Ste
p5-Thenimpl
e mentmainmethodby di
s p
layingmenuof
oper
a t
ions
li
standmakesui
tablefunct
ioncall
sto
perf
ormoperati
ons el
ectedbyt heu sero nq ueue
.
b)Qu
eueOp
era
tio
nsu
sin
gLi
nkedL
ist
Toimpl
ementqueueusi
ngli
nkedlis
t,weneedtosetthefol
lowi
ngthin
gsbe f
oreimpl
eme nt
ingac
tua
l
oper
ati
ons
.
St
ep1–I ncl
udeallt
heheaderfil
eswhic
hareus e
dinth epr
o g
ram.Anddecl
area l
lth
eu ser
def
inedfu
ncti
ons.
St
ep2-De fi
nea' No
de'st
ruct
urewitht
wome mbersdataandn ex
t.
St
ep3–De f
inetwoNodep o
int
ers'f
ront
'and'r
ear'andsetbothtoNULL.
St
ep4-Impl
ementthemainmethodbydispl
ayi
ngMe nuofli
stofoperat
ion
sa ndmakes u
itabl
e
fu
ncti
oncal
l
sint h
ema i
nme th
odtop er
for
mu sersel
ect
edo p
e r
ati
on.
6
5
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
a
)Qu
eueOp
era
tio
nsu
sin
gAr
ray
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
c
ons
tin
tSI
ZE=5;/
/Ma
ximu
msi
zeo
fth
equ
eue
c
las
sQueueArr
ay{
p
riv
ate
:
i
ntqueu
e [
SIZE]
;
i
ntfr
ont
,rear;
p
ubli
c:
Queu
eArr
ay(
){
fr
ont=re
ar=-1;/
/In
iti
al
iz
efr
onta
ndr
eart
o-1t
oin
dic
atea
nemp
tyq
ueu
e
}
v
oidenque ue(in
tval
ue){
i
f((r
ea r+1)%S IZE==f r
ont
){
cout< <"Queu
eOve r
fl
ow!Cannoten
queueele
me nt"<<val
ue<
<en
dl;
ret
u rn;
}
i
f(is
Emp ty
()){
fr
on t=r ear=0;//Ifqueueisempty
,setfr
onta n
dr e
arto0
}el
se{
rear=( rear+1)%S I
ZE;//Moverearc
irc
ular
ly
}
queu
e[re ar
]=v al
ue;
co
ut<<" Enqueued"<<val
ue<<"tothequeue
."<<endl
;
}
v
oiddeque ue(){
i
f(is
Emp ty
()){
cout< <"QueueUnde
rfl
ow!Cannotdequeuefro
ma ne mpt
yq u
eue.
"<<e
ndl
;
ret
u rn;
}
co
ut<<" Deq u
eued"<<queue[
fr
ont]<<"fro
mt hequeue.
"<<endl
;
i
f(fr
on t==r ear){
fr
on t=r ear=-1;//Ifonl
yo neelement,r
esetfr
ontandreart
o-1
}el
se{
fr
on t=( fron
t+1)%S IZE;//Movef r
ontci
rcul
arl
y
}
}
bo
olisEmp
ty(){
re
turnfr
ont==-1&&r
ear==-1;
}
6
6
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
v
oiddi s
play(){
if(i
sEmp ty(
)){
cou
t< <"Queueisempt
y."<
<endl
;
ret
urn;
}
cout<<"Qu eu
ee l
ements
:";
inti=f ront;
do{
cou
t< <queue[i
]<<""
;
i=(i+1)%S I
ZE;
}wh i
le(i!=( r
ear+1)%S IZE)
;
cout<<en dl
;
}
}
;
i
ntmai
n(){
Qu
eueAr
rayq
ueu
e;
q
ueu
e.e
nqu
eue
(10)
;
q
ueu
e.e
nqu
eue
(20)
;
q
ueu
e.e
nqu
eue
(30)
;
q
ueu
e.di
spl
ay(
);
q
ueu
e.de
queue(
);
q
ueu
e.di
spl
ay(
);
r
etu
rn0;
}
6
7
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:-
En
queu
ed10t otheque
ue.
En
queu
ed20t otheque
ue.
En
queu
ed30t otheque
ue.
Qu
eueeleme
nts:102030
Deq
ueue
d10f romtheque
ue.
Qu
eueeleme
nts:2030
6
8
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
b)Us
ingL
ink
edL
ist
:
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
c
las
sNode{
p
ubli
c:
i
ntdata
;
Node
*n ex
t;
Node
(intv
alu
e){
dat
a=v al
ue;
next=nul
lpt
r;
}
}
;
c
las
sQueue
Link
edL
ist{
p
riva
te:
Node
*fro
nt;
Node
*rear
;
p
ubli
c:
Queu
eLin
kedLi
st
(){
fr
ont=rea
r=n ul
l
ptr
;//I
nit
ial
i
zef
ron
tan
dre
art
onu
ll
pt
rtoi
ndi
cat
eane
mpt
yqu
eue
}
v
oidenque ue(
intvalu
e){
Node*n ewNode=n ewNode(
val
ue)
;
i
f(is
Emp t
y()
){
fr
ont=r e
a r=n ewNode;/
/Ifqu
euei
semp t
y,setf
ron
tan
dre
art
oth
ene
wno
de
}el
se{
rear->next=n ewNode;
rear=n ewNo de;
}
co
ut<<" Enqueue
d"< <valu
e<<"toth
equeu
e."<<e
ndl;
}
v
oidde que ue(){
if(i
sEmp ty
()){
c
o ut< <"QueueUnderf
lo
w!Cann
otdequeuefr
omane mptyqueu
e."<
<en
dl;
r
e t
u rn;
}
Node *te mp=f ront;
cout<<" Deq u
eued"< <t
emp->
dat
a< <"fr
omthequeue
."<<endl
;
fro
n t=f ron t
->nex
t;
deletete mp ;
if(f
ron t==n ul
l
ptr
){
r
e ar=n ul
lpt
r;//Ifnoel
emen
tslef
t,setr
earton
ull
ptr
}
}
6
9
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
bo
olisEmp
ty(){
re
turnfr
ont==n
ull
pt
r;
}
v
oiddispla
y (){
i
f(isEmp ty(
)){
cout< <"Queueisemp t
y."<
<en
dl;
return;
}
co
ut< <"Qu eu
ee le
me nt
s:";
Node*c urrent=front
;
whil
e( c
u rren
t!=n ul
lp
tr){
cout< <curre
nt->dat
a<<"";
current=c urrent
->next
;
}
co
ut< <en dl
;
}
}
;
i
ntmai
n(){
Qu
eueLi
nke
dLi
stq
ueu
e;
q
ueu
e.e
nqu
eue
(10)
;
q
ueu
e.e
nqu
eue
(20)
;
q
ueu
e.e
nqu
eue
(30)
;
q
ueu
e.di
spl
ay(
);
q
ueu
e.de
queue(
);
q
ueu
e.di
spl
ay(
);
r
etu
rn0;
}
7
0
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
En
queu
ed10t otheque
ue.
En
queu
ed20t otheque
ue.
En
queu
ed30t otheque
ue.
Qu
eueeleme
nts:102030
Deq
ueue
d10f romtheque
ue.
Qu
eueeleme
nts:2030
7
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
EXPERI
MENTNO:
-16
AI
M:Wr
it
eap
rog
ramt
oin
ser
tane
leme
nti
nth
ebe
gin
nin
gan
den
dofs
ing
lyl
i
nke
dli
st
.
AL
GORI
THM:
In
ser
ti
ngatBeg
inni
ngoft
heli
st
Wecanus
ethefol
l
owin
gst
epstoin
ser
tan
ewn
odea
tbe
gin
nin
goft
hes
ing
lel
i
nke
dli
st
..
.
S
tep1-Creat
ean ewNo dewithgi
venva l
ue.
S
tep2–Chec
kwh e
therli
stisEmpty(he
a d==NUL
L)
S
tep3-I fi
tisEmptythen,setne
wNo de→ n ext=NULLa
ndhead=newNode
.
S
tep4-I
fiti
sNotEmp t
yt h
e n
,setnewNo de→ next=he
adandh
ead=n
ewNode.
In
ser
ti
ngatEndoft
heli
st
Wecanus
ethefo
ll
owi
ngste
pst
oin
ser
tan
ewn
odea
ten
doft
hes
ing
lel
i
nke
dli
st
..
.
S
tep1-Cr ea t
ean e
wNo dewithgiv
enval
ueandnewNo de→ nextasNULL.
S
tep2-Ch eckwh ethe
rlis
tisEmpty(h
ead==NUL L
).
S
tep3–I fitisEmp t
yt he
n,sethe
a d=newNode.
S
tep4-IfitisNo tEmptythen
,defin
eanodepoin
terte
mpa ndin
iti
al
izewi
thhea
d.
S
tep5-Keepmo v
ingthetemptoitsnex
tnodeunt
ili
treache
stothel
astno
deinthel
i
st(
unt
ilt
emp
→nextise qualtoNULL).
S
tep6-Settemp→n ext
=n ewNode.
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
I
MPL
EMENT
ATI
ON:
a
)In
ser
ti
nga
tth
ebeg
inn
ingo
fth
eli
st
:
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
/
/No destr
uct
uref
oras
ing
lyl
i
nke
dli
st
c
las
sNo de{
p
ubli
c:
i
ntda t
a;
Node*next
;
Node
(intv
alu
e){
dat
a=v al
ue;
next=nul
lpt
r;
}
}
;
/
/L i
nkedL
istcl
asst
ope
rfo
rmo
per
ati
onso
nth
eli
nke
dli
st
c
las
sL i
nkedLi
st{
p
riva
te:
Node*head;
p
ubl
ic:
L
inkedLi
st
(){
head=n u
ll
pt
r;/
/In
iti
al
iz
ehe
adt
onu
ll
pt
rtoi
ndi
cat
eane
mpt
yli
st
}
/
/Funct
iontoi
ns er
tan ewno deatthebe
gin
nin
goft
hel
i
st
v
oidins
ert
AtBegin
ning(
intv
a l
u e
){
Node*ne
wNo de=n ewNo de(va
lue
);
i
f(head==nu l
lpt
r){
head=newNo de;
}el
se{
newNode
-> nex
t=h ead;
head=newNo de;
}
}
/
/Funct
iontoi
nser
tanewn odeattheen
doft
hel
i
st
v
oidins
ert
AtEn
d(i
ntval
ue){
Node*ne
wNode=n ewNode(v
alu
e);
i
f(head==nul
lpt
r){
head=newNode;
}el
se{
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
Node*temp=h ead;
whil
e(t
emp ->n
ext!=nul
lp
tr){
temp=t emp->ne
xt;
}
te
mp ->
next=n ewNode
;
}
}
/
/Functi
ont odis
playt h
ee l
eme nt
soft
hel
i
nke
dli
st
v
oiddispl
ay(){
i
f(head==n ul
l
ptr){
cout<<"Li
stise mpty.
"<<endl
;
retur
n;
}
co
ut<<"LinkedListelements
:";
Node*temp=h ead;
whil
e(temp!=n ul
lptr
){
cout<<temp->data<<"";
temp=t emp->n ext
;
}
co
ut<<e ndl
;
}
}
;
i
ntmain
(){
Li
nkedL
istl
i
st;
/
/Inser
tatthebegi
nni
ng
l
i
st.
in
sert
AtBe
g i
nni
ng(
30)
;
l
i
st.
in
sert
AtBe
g i
nni
ng(
20)
;
l
i
st.
in
sert
AtBe
g i
nni
ng(
10)
;
l
i
st.
dis
pla
y()
;
/
/Inser
tattheend
l
i
st.
in
sert
AtEn
d(40)
;
l
i
st.
in
sert
AtEn
d(50)
;
l
i
st.
dis
pla
y()
;
r
etu
rn0;
}
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
L
ink
edL
iste
leme
nts
:102030
L
ink
edL
iste
leme
nts
:1020304050
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
b)I
nser
ti
nga
tth
een
doft
hel
i
st:
#i
ncl
ude<i
ost
ream>
us
ingname
spacest
d;
/
/No destr
uct
uref
oras
ing
lyl
i
nke
dli
st
c
las
sNo de{
p
ubli
c:
i
ntda t
a;
Node*next
;
Node
(intv
alu
e){
dat
a=v al
ue;
next=nul
lpt
r;
}
}
;
/
/L i
nkedL
istcl
asst
ope
rfo
rmo
per
ati
onso
nth
eli
nke
dli
st
c
las
sL i
nkedLi
st{
p
riva
te:
Node*head;
p
ubl
ic:
L
inkedLi
st
(){
head=n u
ll
pt
r;/
/In
iti
al
iz
ehe
adt
onu
ll
pt
rtoi
ndi
cat
eane
mpt
yli
st
}
/
/Funct
iontoi
ns er
tan ewno deatthebe
gin
nin
goft
hel
i
st
v
oidins
ert
AtBegin
ning(
intv
a l
u e
){
Node*ne
wNo de=n ewNo de(va
lue
);
i
f(head==nu l
lpt
r){
head=newNo de;
}el
se{
newNode
-> nex
t=h ead;
head=newNo de;
}
}
/
/Funct
iontoinser
tane wnodeattheen
doft
hel
i
st
v
oidins
ertAt
End(i
ntval
ue){
Node*newNode=n ewNode(val
ue)
;
i
f(head==n ul
lpt
r){
head=n ewNode;
}el
se{
Node*temp=h ead;
whil
e(temp->n
ext!=nul
lpt
r){
t
emp=t emp->
n e
xt;
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
}
t
emp
->n
ext=n
ewNo
de;
}
}
/
/Functi
ont odis
playt h
ee l
eme nt
soft
hel
i
nke
dli
st
v
oiddispl
ay(){
i
f(head==n ul
l
ptr){
cout<<"Li
stise mpty.
"<<endl
;
retur
n;
}
co
ut<<"LinkedListelements
:";
Node*temp=h ead;
whil
e(temp!=n ul
lptr
){
cout<<temp->data<<"";
temp=t emp->n ext
;
}
co
ut<<e ndl
;
}
}
;
i
ntmain
(){
Li
nkedL
istl
i
st;
/
/Inser
tatthebegi
nni
ng
l
i
st.
in
sert
AtBe
g i
nni
ng(
30)
;
l
i
st.
in
sert
AtBe
g i
nni
ng(
20)
;
l
i
st.
in
sert
AtBe
g i
nni
ng(
10)
;
l
i
st.
dis
pla
y()
;
/
/Inser
tattheend
l
i
st.
in
sert
AtEn
d(40)
;
l
i
st.
in
sert
AtEn
d(50)
;
l
i
st.
dis
pla
y()
;
r
etu
rn0;
1
L
abo
rat
oryMa
nua
l
Da
taSt
ruc
tur
e(AL
-30
3) J
ul-Dec
202
3
OUT
PUT
:
L
ink
edL
iste
leme
nts
:102030
L
ink
edL
iste
leme
nts
:1020304050
*
***
***
*END*
***
***
*
1
L
abo
rat
oryMa
nua
l