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.

Return the position of the element of S preced<strong>in</strong>g the one at position p; an<br />

error occurs if p is the first position.<br />

next(p):<br />

Return the position of the element of S follow<strong>in</strong>g the one at position p; an<br />

error occurs if p is the last position.<br />

The above methods allow us to refer to relative positions <strong>in</strong> a list, start<strong>in</strong>g at the<br />

beg<strong>in</strong>n<strong>in</strong>g or end, <strong>and</strong> to move <strong>in</strong>crementally up or down the list. These positions<br />

can <strong>in</strong>tuitively be thought of as nodes <strong>in</strong> the list, but note that there are no specific<br />

references to node objects. Moreover, if we provide a position as an argument to a<br />

list method, then that position must represent a valid position <strong>in</strong> that list.<br />

Node List Update Methods<br />

In addition to the above methods <strong>and</strong> the generic methods size <strong>and</strong> isEmpty,<br />

we also <strong>in</strong>clude the follow<strong>in</strong>g update methods for the node list ADT, which take<br />

position objects as parameters <strong>and</strong>/or provide position objects as return values.<br />

set(p, e):<br />

Replace the element at position p with e, return<strong>in</strong>g the element formerly<br />

at position p.<br />

addFirst(e):<br />

Insert a new element e <strong>in</strong>to S as the first element.<br />

addLast(e):<br />

Insert a new element e <strong>in</strong>to S as the last element.<br />

addBefore(p, e):<br />

Insert a new element e <strong>in</strong>to S before position p.<br />

addAfter(p, e):<br />

Insert a new element e <strong>in</strong>to S after position p.<br />

remove(p):<br />

Remove <strong>and</strong> return the element at position p <strong>in</strong> S, <strong>in</strong>validat<strong>in</strong>g this<br />

position <strong>in</strong> S.<br />

328

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

Saved successfully!

Ooh no, something went wrong!