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.

Next, consider the remove(p) method, which removes the element e stored at<br />

position p. Similar to the discussion <strong>in</strong> Section 3.3.2, to perform this operation, we<br />

l<strong>in</strong>k the two neighbors of p to refer to one another as new neighbors—l<strong>in</strong>k<strong>in</strong>g out p.<br />

Note that after p is l<strong>in</strong>ked out, no nodes will be po<strong>in</strong>t<strong>in</strong>g to p; hence, the garbage<br />

collector can reclaim the space for p. This algorithm is given <strong>in</strong> Code Fragment 6.8<br />

<strong>and</strong> is illustrated <strong>in</strong> Figure 6.7. Recall<strong>in</strong>g our use of header <strong>and</strong> trailer sent<strong>in</strong>els,<br />

note that this algorithm works even if p is the first, last, or only real position <strong>in</strong> the<br />

list.<br />

Code Fragment 6.8: Remov<strong>in</strong>g an element e stored<br />

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

Figure 6.7: Remov<strong>in</strong>g the object stored at the<br />

position for "PVD": (a) before the removal; (b) l<strong>in</strong>k<strong>in</strong>g<br />

out the old node; (c) after the removal (<strong>and</strong> garbage<br />

collection).<br />

336

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

Saved successfully!

Ooh no, something went wrong!