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.

(8,3,5)<br />

set(p 3 ,7)<br />

3<br />

(8,7,5)<br />

addAfter(first(),2)<br />

-<br />

(8,2,7,5)<br />

The node list ADT, with its built-<strong>in</strong> notion of position, is useful <strong>in</strong> a number of<br />

sett<strong>in</strong>gs. For example, a program that simulates a game of cards could model each<br />

person's h<strong>and</strong> as a node list. S<strong>in</strong>ce most people keep cards of the same suit<br />

together, <strong>in</strong>sert<strong>in</strong>g <strong>and</strong> remov<strong>in</strong>g cards from a person's h<strong>and</strong> could be<br />

implemented us<strong>in</strong>g the methods of the node list ADT, with the positions be<strong>in</strong>g<br />

determ<strong>in</strong>ed by a natural order<strong>in</strong>g of the suits. Likewise, a simple text editor<br />

embeds the notion of positional <strong>in</strong>sertion <strong>and</strong> removal, s<strong>in</strong>ce such editors typically<br />

perform all updates relative to a cursor, which represents the current position <strong>in</strong><br />

the list of characters of text be<strong>in</strong>g edited.<br />

A <strong>Java</strong> <strong>in</strong>terface represent<strong>in</strong>g the position ADT is given <strong>in</strong> Code Fragment 6.4.<br />

Code Fragment 6.4:<br />

ADT.<br />

<strong>Java</strong> <strong>in</strong>terface for the position<br />

An <strong>in</strong>terface for the node list ADT, called Position List, is given <strong>in</strong> Code<br />

Fragment 6.5. This <strong>in</strong>terface uses the follow<strong>in</strong>g exceptions to <strong>in</strong>dicate error<br />

conditions.<br />

BoundaryViolationException: Thrown if an attempt is made at<br />

access<strong>in</strong>g an element whose position is outside the range of positions of the list<br />

(for example, call<strong>in</strong>g method next on the last position of the sequence).<br />

Invalid Position Exception: Thrown if a position provided as<br />

argument is not valid (for example, it is a null reference or it has no associated<br />

list).<br />

331

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

Saved successfully!

Ooh no, something went wrong!