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 node list ADT allows us to view an ordered collection of objects <strong>in</strong> terms of<br />

their places, without worry<strong>in</strong>g about the exact way those places are<br />

represented.(See Figure 6.5.)<br />

Figure 6.5: A node list. The positions <strong>in</strong> the current<br />

order are p, q, r, <strong>and</strong> s.<br />

There may at first seem to be redundancy <strong>in</strong> the above repertory of operations for<br />

the node list ADT, s<strong>in</strong>ce we can perform operation addFirst(e) with<br />

addBefore(first(), e), <strong>and</strong> operation addLast(e) with<br />

addAfter(getLast(), e). But these substitutions can only be done for a<br />

nonempty list.<br />

Note that an error condition occurs if a position passed as argument to one of the<br />

list operations is <strong>in</strong>valid. Reasons for a position p to be <strong>in</strong>valid <strong>in</strong>clude:<br />

• p = null<br />

• p was previously deleted from the list<br />

• p is a position of a different list<br />

• p is the first position of the list <strong>and</strong> we call prev(p)<br />

• p is the last position of the list <strong>and</strong> we call next(p).<br />

We illustrate the operations of the node list ADT <strong>in</strong> the follow<strong>in</strong>g example.<br />

Example 6.3: We show below a series of operations for an <strong>in</strong>itially empty list<br />

node S. We use variables p 1 , p 2 , <strong>and</strong> so on, to denote different positions, <strong>and</strong> we<br />

show the object currently stored at such a position <strong>in</strong> parentheses.<br />

Operation<br />

Output<br />

S<br />

addFirst(8)<br />

329

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

Saved successfully!

Ooh no, something went wrong!