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.

where n is the number of elements <strong>in</strong> the list. The worst case for this k<strong>in</strong>d of<br />

search occurs when<br />

r= n/2.<br />

Thus, the runn<strong>in</strong>g time is still O(n).<br />

Operations add(i,e) <strong>and</strong> remove(i) also must perform l<strong>in</strong>k hopp<strong>in</strong>g to<br />

locate the node stor<strong>in</strong>g the element with <strong>in</strong>dex i, <strong>and</strong> then <strong>in</strong>sert or delete a node.<br />

The runn<strong>in</strong>g times of these implementations of add(i,e) <strong>and</strong> remove(i) are<br />

likewise<br />

O(m<strong>in</strong>(i+ 1, n−i+1)),<br />

which is O(n). One advantage of this approach is that, if i = 0 or i = n − 1, as is the<br />

case <strong>in</strong> the adaptation of the array list ADT to the deque ADT given <strong>in</strong> Section<br />

6.1.1, then add <strong>and</strong> remove run <strong>in</strong> O(1) time. But, <strong>in</strong> general, us<strong>in</strong>g array-list<br />

methods with a java.util. L<strong>in</strong>kedList object is <strong>in</strong>efficient.<br />

6.4.3 Sequences<br />

A sequence is an ADT that supports all of the methods of the deque ADT (Section<br />

5.3), the array list ADT (Section 6.1), <strong>and</strong> the node list ADT (Section 6.2). That is,<br />

it provides explicit access to the elements <strong>in</strong> the list either by their <strong>in</strong>dices or by<br />

their positions. Moreover, s<strong>in</strong>ce it provides this dual access capability, we also<br />

<strong>in</strong>clude, <strong>in</strong> the sequence ADT, the follow<strong>in</strong>g two "bridg<strong>in</strong>g" methods that provide<br />

connections between <strong>in</strong>dices <strong>and</strong> positions:<br />

atIndex(i): Return the position of the element with <strong>in</strong>dex i; an error<br />

condition occurs if i < 0 or i > size() − 1.<br />

<strong>in</strong>dexOf(p): Return the <strong>in</strong>dex of the element at position p.<br />

Multiple Inheritance <strong>in</strong> the Sequence ADT<br />

The def<strong>in</strong>ition of the sequence ADT as <strong>in</strong>clud<strong>in</strong>g all the methods from three<br />

different ADTs is an example of multiple <strong>in</strong>heritance (Section 2.4.2). That is, the<br />

sequence ADT <strong>in</strong>herits methods from three "super" abstract data types. In other<br />

words, its methods <strong>in</strong>clude the union of the methods of these super ADTs. See<br />

Code Fragment 6.18 for a <strong>Java</strong> specification of the sequence ADT as a <strong>Java</strong><br />

<strong>in</strong>terface.<br />

Code Fragment 6.18: The Sequence <strong>in</strong>terface<br />

def<strong>in</strong>ed via multiple <strong>in</strong>heritance. It <strong>in</strong>cludes all the<br />

methods of the Deque, IndexList, <strong>and</strong><br />

353

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

Saved successfully!

Ooh no, something went wrong!