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.

Deque Method<br />

Realization with Node-List Methods<br />

size(), isEmpty()<br />

size(), isEmpty()<br />

getFirst()<br />

first()·element()<br />

getLast()<br />

last()·element()<br />

addFirst(e)<br />

addFirst(e)<br />

addLast(e)<br />

addLast(e)<br />

removeFirst()<br />

remove(first())<br />

removeLast()<br />

remove(last())<br />

6.2.4 Doubly L<strong>in</strong>ked List Implementation<br />

Suppose we would like to implement the node list ADT us<strong>in</strong>g a doubly l<strong>in</strong>ked list<br />

(Section 3.3). We can simply make the nodes of the l<strong>in</strong>ked list implement the<br />

position ADT. That is, we have each node implement the Position <strong>in</strong>terface <strong>and</strong><br />

therefore def<strong>in</strong>e a method, element(), which returns the element stored at the<br />

node. Thus, the nodes themselves act as positions. They are viewed <strong>in</strong>ternally by<br />

the l<strong>in</strong>ked list as nodes, but from the outside, they are viewed only as positions. In<br />

the <strong>in</strong>ternal view, we can give each node v <strong>in</strong>stance variables prev <strong>and</strong> next that<br />

respectively refer to the predecessor <strong>and</strong> successor nodes of v (which could <strong>in</strong> fact<br />

be header or trailer sent<strong>in</strong>el nodes mark<strong>in</strong>g the beg<strong>in</strong>n<strong>in</strong>g <strong>and</strong> end of the list).<br />

Instead of us<strong>in</strong>g variables prev <strong>and</strong> next directly, we def<strong>in</strong>e methods getPrev,<br />

setPrev, getNext, <strong>and</strong> setNext of a node to access <strong>and</strong> modify these<br />

variables.<br />

333

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

Saved successfully!

Ooh no, something went wrong!