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.

The reverse operation of <strong>in</strong>sert<strong>in</strong>g a new element at the head of a l<strong>in</strong>ked list is to<br />

remove an element at the head. This operation is illustrated <strong>in</strong> Figure 3.13 <strong>and</strong> given<br />

<strong>in</strong> detail <strong>in</strong> Code Fragment 3.16.<br />

Figure 3.13: Removal of an element at the head of a<br />

s<strong>in</strong>gly l<strong>in</strong>ked list: (a) before the removal; (b) "l<strong>in</strong>k<strong>in</strong>g out"<br />

the old new node; (c) after the removal.<br />

Code Fragment 3.16: Remov<strong>in</strong>g the node at the<br />

beg<strong>in</strong>n<strong>in</strong>g of a s<strong>in</strong>gly l<strong>in</strong>ked list.<br />

Unfortunately, we cannot easily delete the tail node of a s<strong>in</strong>gly l<strong>in</strong>ked list. Even if<br />

we have a tail reference directly to the last node of the list, we must be able to<br />

access the node before the last node <strong>in</strong> order to remove the last node. But we cannot<br />

reach the node before the tail by follow<strong>in</strong>g next l<strong>in</strong>ks from the tail. The only way to<br />

169

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

Saved successfully!

Ooh no, something went wrong!