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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 3.17: Add<strong>in</strong>g a new node after the node<br />

stor<strong>in</strong>g JFK: (a) creat<strong>in</strong>g a new node with element BWI<br />

<strong>and</strong> l<strong>in</strong>k<strong>in</strong>g it <strong>in</strong>; (b) after the <strong>in</strong>sertion.<br />

3.3.2 Removal <strong>in</strong> the Middle of a Doubly L<strong>in</strong>ked List<br />

Likewise, it is easy to remove a node v <strong>in</strong> the middle of a doubly l<strong>in</strong>ked list. We<br />

access the nodes u <strong>and</strong> w on either side of v us<strong>in</strong>g v's getPrev <strong>and</strong> getNext<br />

methods (these nodes must exist, s<strong>in</strong>ce we are us<strong>in</strong>g sent<strong>in</strong>els). To remove node v,<br />

we simply have u <strong>and</strong> w po<strong>in</strong>t to each other <strong>in</strong>stead of to v. We refer to this<br />

operation as the l<strong>in</strong>k<strong>in</strong>g out of v. We also null out v's prev <strong>and</strong> next po<strong>in</strong>ters so<br />

as not to reta<strong>in</strong> old references <strong>in</strong>to the list. This algorithm is given <strong>in</strong> Code<br />

Fragment 3.21 <strong>and</strong> is illustrated <strong>in</strong> Figure 3.18.<br />

Code Fragment 3.21: Remov<strong>in</strong>g a node v <strong>in</strong> a<br />

doubly l<strong>in</strong>ked list. This method works even if v is the<br />

first, last, or only nonsent<strong>in</strong>el node.<br />

175

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

Saved successfully!

Ooh no, something went wrong!