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.

l<strong>in</strong>ked list without reveal<strong>in</strong>g this detail to a user. Likewise, we do not wish to allow<br />

a user to modify the <strong>in</strong>ternal structure of a list without our knowledge. Such<br />

modification would be possible, however, if we provided a reference to a node <strong>in</strong><br />

our list <strong>in</strong> a form that allowed the user to access <strong>in</strong>ternal data <strong>in</strong> that node (such as a<br />

next or prev field).<br />

To abstract <strong>and</strong> unify the different ways of stor<strong>in</strong>g elements <strong>in</strong> the various<br />

implementations of a list, we <strong>in</strong>troduce the concept of position, which formalizes<br />

the <strong>in</strong>tuitive notion of "place" of an element relative to others <strong>in</strong> the list.<br />

6.2.2 Positions<br />

So as to safely exp<strong>and</strong> the set of operations for lists, we abstract a notion of<br />

"position" that allows us to enjoy the efficiency of doubly or s<strong>in</strong>gly l<strong>in</strong>ked list<br />

implementations without violat<strong>in</strong>g object-oriented design pr<strong>in</strong>ciples. In this<br />

framework, we view a list as a collection of elements that stores each element at a<br />

position <strong>and</strong> that keeps these positions arranged <strong>in</strong> a l<strong>in</strong>ear order. A position is itself<br />

an abstract data type that supports the follow<strong>in</strong>g simple method:<br />

element(): Return the element stored at this position.<br />

A position is always def<strong>in</strong>ed relatively, that is, <strong>in</strong> terms of its neighbors. In a list, a<br />

position p will always be "after" some position q <strong>and</strong> "before" some position s<br />

(unless p is the first or last position). A position p, which is associated with some<br />

element e <strong>in</strong> a list S, does not change, even if the <strong>in</strong>dex of e changes <strong>in</strong> S, unless we<br />

explicitly remove e (<strong>and</strong>, hence, destroy position p). Moreover, the position p does<br />

not change even if we replace or swap the element e stored at p with another<br />

element. These facts about positions allow us to def<strong>in</strong>e a set of position-based list<br />

methods that take position objects as parameters <strong>and</strong> also provide position objects<br />

as return values.<br />

6.2.3 The Node List Abstract <strong>Data</strong> Type<br />

Us<strong>in</strong>g the concept of position to encapsulate the idea of "node" <strong>in</strong> a list, we can<br />

def<strong>in</strong>e another type of sequence ADT called the node list ADT. This ADT supports<br />

the follow<strong>in</strong>g methods for a list S:<br />

first():<br />

Return the position of the first element of S; an error occurs if S is empty.<br />

last():<br />

Return the position of the last element of S; an error occurs if S is empty.<br />

prev(p):<br />

327

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

Saved successfully!

Ooh no, something went wrong!