23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

new node v to hold the element e, l<strong>in</strong>k v <strong>in</strong>to its place <strong>in</strong> the list, <strong>and</strong> then update the<br />

next <strong>and</strong> prev references of v's two new neighbors. This method is given <strong>in</strong> Code<br />

Fragment 6.7, <strong>and</strong> is illustrated (aga<strong>in</strong>) <strong>in</strong> Figure 6.6. Recall<strong>in</strong>g the use of sent<strong>in</strong>els<br />

(Section 3.3), note that this algorithm works even if p is the last real position.<br />

Code Fragment 6.7: Insert<strong>in</strong>g an element e after a<br />

position p <strong>in</strong> a l<strong>in</strong>ked list.<br />

Figure 6.6: Add<strong>in</strong>g anew node after the position for<br />

"JFK": (a) before the <strong>in</strong>sertion; (b) creat<strong>in</strong>g node v with<br />

element "BWI" <strong>and</strong> l<strong>in</strong>k<strong>in</strong>g it <strong>in</strong>; (c) after the <strong>in</strong>sertion.<br />

The algorithms for methods addBefore, addFirst, <strong>and</strong> addLast are similar<br />

to that for method addAfter. We leave their details as an exercise (R-6.5).<br />

335

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!