P. 1
JAVA chapter 2

JAVA chapter 2

|Views: 2|Likes:
Published by amolas22
Java programming
Java programming

More info:

Published by: amolas22 on Feb 27, 2014
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





llCUR£ S.8 DeleLing a specified link.

AI each cycle Ihrough Ihe Wh1Je loop, jusI beíore curren1 is seI Io curren1.nex1,
prev1ous is seI Io curren1. This keeps iI poinIing aI Ihe link preceding curren1.
To deleIe Ihe currenI link once iI´s íound, Ihe nex1 íield oí Ihe previous link is seI Io
Ihe nexI link. A special case arises ií Ihe currenI link is Ihe íirsI link because Ihe íirsI
link is poinIed Io by Ihe L1nkL1s1´s f1rs1 íield and noI by anoIher link. In Ihis case
Ihe link is deleIed by changing f1rs1 Io poinI Io f1rs1.nex1, as we saw in Ihe
J1nkL1s1.¡ava program wiIh Ihe deJe1eF1rs1{) meIhod. Here´s Ihe code IhaI covers
Ihese Iwo possibiliIies:
11 found 11
1f{curren1 == f1rs1) 11 1f f1rs1 J1nk,
f1rs1 = f1rs1.nex1¦ 11 change f1rs1
eJse 11 o1herW1se,
prev1ous.nex1 = curren1.nex1¦ 11 bypass J1nk
Other Methods
We´ve seen meIhods Io inserI and deleIe iIems aI Ihe sIarI oí a lisI, and Io íind a
speciíied iIem and deleIe a speciíied iIem. You can imagine oIher useíul lisI meIhods.
Eor example, an 1nser1Af1er{) meIhod could íind a link wiIh a speciíied key value
and inserI a new link íollowing iI. We´ll see such a meIhod when we Ialk abouI lisI
iIeraIors aI Ihe end oí Ihis chapIer.
linding and DeleLing Specified Links 1º7
a) Before deletion
b) After deletion
First Next Next Next
Previous Current
Previous Current
   
  
Next Next Next
17 44 98 73
17 44 73
DoubIe-£nded Lists
A double-ended lisI is similar Io an ordinary linked lisI, buI iI has one addiIional
íeaIure: a reíerence Io Ihe lasI link as well as Io Ihe íirsI. Eigure S.º shows such a lisI.
CHAPT£R 5 Linked LisLs 1º8
next next next next
llCUR£ S.9 A double-ended lisL.
The reíerence Io Ihe lasI link permiIs you Io inserI a new link direcIly aI Ihe end oí
Ihe lisI as well as aI Ihe beginning. Oí course, you can inserI a new link aI Ihe end oí
an ordinary single-ended lisI by iIeraIing Ihrough Ihe enIire lisI unIil you reach Ihe
end, buI Ihis approach is ineííicienI.
Access Io Ihe end oí Ihe lisI as well as Ihe beginning makes Ihe double-ended lisI
suiIable íor cerIain siIuaIions IhaI a single-ended lisI can´I handle eííicienIly. One
such siIuaIion is implemenIing a gueue, we´ll see how Ihis Iechnigue works in Ihe
nexI secIion.
LisIing S.3 conIains Ihe f1rs1Las1L1s1.¡ava program, which demonsIraIes a double-
ended lisI. (IncidenIally, don´I coníuse Ihe double-ended lisI wiIh Ihe doubly linked
lisI, which we´ll explore laIer in Ihis chapIer.)
LlSTlNC S.3 1he f1rs1Las1L1s1.¡ava Program
11 f1rs1Las1L1s1.¡ava
11 denons1ra1es J1s1 W11h f1rs1 and Jas1 references
11 1o run 1h1s progran: 0>¡ava F1rs1Las1App
cJass L1nk
pubJ1c Jong d0a1a¦ 11 da1a 11en
pubJ1c L1nk nex1¦ 11 nex1 J1nk 1n J1s1
11 -------------------------------------------------------------
pubJ1c L1nk{Jong d) 11 cons1ruc1or
{ d0a1a = d¦ }
11 -------------------------------------------------------------
pubJ1c vo1d d1spJayL1nk{) 11 d1spJay 1h1s J1nk
{ Sys1en.ou1.pr1n1{d0a1a + " ")¦ }
11 -------------------------------------------------------------
} 11 end cJass L1nk
cJass F1rs1Las1L1s1
pr1va1e L1nk f1rs1¦ 11 ref 1o f1rs1 J1nk
pr1va1e L1nk Jas1¦ 11 ref 1o Jas1 J1nk
11 -------------------------------------------------------------
pubJ1c F1rs1Las1L1s1{) 11 cons1ruc1or
f1rs1 = nuJJ¦ 11 no J1nks on J1s1 ye1
Jas1 = nuJJ¦
11 -------------------------------------------------------------
pubJ1c booJean 1sEnp1y{) 11 1rue 1f no J1nks
{ re1urn f1rs1==nuJJ¦ }
11 -------------------------------------------------------------
pubJ1c vo1d 1nser1F1rs1{Jong dd) 11 1nser1 a1 fron1 of J1s1
L1nk neWL1nk = neW L1nk{dd)¦ 11 nake neW J1nk
1f{ 1sEnp1y{) ) 11 1f enp1y J1s1,
Jas1 = neWL1nk¦ 11 neWL1nk <-- Jas1
neWL1nk.nex1 = f1rs1¦ 11 neWL1nk --> oJd f1rs1
f1rs1 = neWL1nk¦ 11 f1rs1 --> neWL1nk
11 -------------------------------------------------------------
pubJ1c vo1d 1nser1Las1{Jong dd) 11 1nser1 a1 end of J1s1
L1nk neWL1nk = neW L1nk{dd)¦ 11 nake neW J1nk
1f{ 1sEnp1y{) ) 11 1f enp1y J1s1,
f1rs1 = neWL1nk¦ 11 f1rs1 --> neWL1nk
Jas1.nex1 = neWL1nk¦ 11 oJd Jas1 --> neWL1nk
Jas1 = neWL1nk¦ 11 neWL1nk <-- Jas1
11 -------------------------------------------------------------
pubJ1c Jong deJe1eF1rs1{) 11 deJe1e f1rs1 J1nk
{ 11 {assunes non-enp1y J1s1)
Jong 1enp = f1rs1.d0a1a¦
1f{f1rs1.nex1 == nuJJ) 11 1f onJy one 11en
Double-Ended LisLs 1ºº
LlSTlNC S.3 ConLinued
Jas1 = nuJJ¦ 11 nuJJ <-- Jas1
f1rs1 = f1rs1.nex1¦ 11 f1rs1 --> oJd nex1
re1urn 1enp¦
11 -------------------------------------------------------------
pubJ1c vo1d d1spJayL1s1{)
Sys1en.ou1.pr1n1{"L1s1 {f1rs1-->Jas1): ")¦
L1nk curren1 = f1rs1¦ 11 s1ar1 a1 beg1nn1ng
Wh1Je{curren1 != nuJJ) 11 un11J end of J1s1,
curren1.d1spJayL1nk{)¦ 11 pr1n1 da1a
curren1 = curren1.nex1¦ 11 nove 1o nex1 J1nk
11 -------------------------------------------------------------
} 11 end cJass F1rs1Las1L1s1
cJass F1rs1Las1App
pubJ1c s1a11c vo1d na1n{S1r1ng¡¡ args)
{ 11 nake a neW J1s1
F1rs1Las1L1s1 1heL1s1 = neW F1rs1Las1L1s1{)¦
1heL1s1.1nser1F1rs1{22)¦ 11 1nser1 a1 fron1
1heL1s1.1nser1Las1{11)¦ 11 1nser1 a1 rear
1heL1s1.d1spJayL1s1{)¦ 11 d1spJay 1he J1s1
1heL1s1.deJe1eF1rs1{)¦ 11 deJe1e f1rs1 1Wo 11ens
1heL1s1.d1spJayL1s1{)¦ 11 d1spJay aga1n
} 11 end na1n{)
} 11 end cJass F1rs1Las1App
CHAPT£R 5 Linked LisLs 200
LlSTlNC S.3 ConLinued
Eor simpliciIy, in Ihis program we´ve reduced Ihe number oí daIa iIems in each link
írom Iwo Io one. This makes iI easier Io display Ihe link conIenIs. (Remember IhaI in
a serious program Ihere would be many more daIa iIems, or a reíerence Io anoIher
objecI conIaining many daIa iIems.)
This program inserIs Ihree iIems aI Ihe íronI oí Ihe lisI, inserIs Ihree more aI Ihe
end, and displays Ihe resulIing lisI. II Ihen deleIes Ihe íirsI Iwo iIems and displays
Ihe lisI again. Here´s Ihe ouIpuI:
L1s1 {f1rs1-->Jas1): 66 44 22 11 33 55
L1s1 {f1rs1-->Jas1): 22 11 33 55
NoIice how repeaIed inserIions aI Ihe íronI oí Ihe lisI reverse Ihe order oí Ihe iIems,
while repeaIed inserIions aI Ihe end preserve Ihe order.
The double-ended lisI class is called Ihe F1rs1Las1L1s1. As discussed, iI has Iwo daIa
iIems, f1rs1 and Jas1, which poinI Io Ihe íirsI iIem and Ihe lasI iIem in Ihe lisI. Ií
Ihere is only one iIem in Ihe lisI, boIh f1rs1 and Jas1 poinI Io iI, and ií Ihere are no
iIems, Ihey are boIh nuJJ.
The class has a new meIhod, 1nser1Las1{), IhaI inserIs a new iIem aI Ihe end oí Ihe
lisI. This process involves modiíying Jas1.nex1 Io poinI Io Ihe new link and Ihen
changing Jas1 Io poinI Io Ihe new link, as shown in Eigure S.10.
Double-Ended LisLs 201
next next next next
a) Before insertion
next next next next
b) After insertion
llCUR£ S.10 lnserLion aL Lhe end of a lisL.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->