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.

Header <strong>and</strong> Trailer Sent<strong>in</strong>els<br />

To simplify programm<strong>in</strong>g, it is convenient to add special nodes at both ends of a<br />

doubly l<strong>in</strong>ked list: a header node just before the head of the list, <strong>and</strong> a trailer node<br />

just after the tail of the list. These "dummy" or sent<strong>in</strong>el nodes do not store any<br />

elements. The header has a valid next reference but a null prev reference, while<br />

the trailer has a valid prev reference but a null next reference. A doubly l<strong>in</strong>ked<br />

list with these sent<strong>in</strong>els is shown <strong>in</strong> Figure 3.14. Note that a l<strong>in</strong>ked list object would<br />

simply need to store references to these two sent<strong>in</strong>els <strong>and</strong> a size counter that<br />

keeps track of the number of elements (not count<strong>in</strong>g sent<strong>in</strong>els) <strong>in</strong> the list.<br />

Figure 3.14: A doubly l<strong>in</strong>ked list with sent<strong>in</strong>els,<br />

header <strong>and</strong> trailer, mark<strong>in</strong>g the ends of the list. An<br />

empty list would have these sent<strong>in</strong>els po<strong>in</strong>t<strong>in</strong>g to each<br />

other. We do not show the null prev po<strong>in</strong>ter for the<br />

171

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

Saved successfully!

Ooh no, something went wrong!