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.

An <strong>in</strong>terface for a queue ADT, but us<strong>in</strong>g different method names. Methods<br />

<strong>in</strong>clude peek() (same as front()), offer(e) (same as<br />

enqueue(e)), <strong>and</strong> poll() (same as dequeue()).<br />

Set:<br />

An <strong>in</strong>terface extend<strong>in</strong>g Collection to sets.<br />

The <strong>Java</strong> Collections Framework also <strong>in</strong>cludes several concrete classes<br />

implement<strong>in</strong>g various comb<strong>in</strong>ations of the above <strong>in</strong>terfaces. Rather than list each of<br />

these classes here, however, we discuss them at more appropriate places <strong>in</strong> this<br />

book. One topic we would like to stress now, however, is that any class<br />

implement<strong>in</strong>g the java.util.Collection <strong>in</strong>terface also implements the<br />

java.lang.Iterable <strong>in</strong>terface; hence, it <strong>in</strong>cludes an iterator() method<br />

<strong>and</strong> can be used <strong>in</strong> a for-each loop. In addition, any class implement<strong>in</strong>g the<br />

java.util.List <strong>in</strong>terface also <strong>in</strong>cludes a listIterator() method, as well.<br />

As we observed above, such <strong>in</strong>terfaces are useful for loop<strong>in</strong>g through the elements<br />

of a collection or list.<br />

6.4.2 The java. util.L<strong>in</strong>kedList Class<br />

The java.util.L<strong>in</strong>ked List class conta<strong>in</strong>s a lot of methods, <strong>in</strong>clud<strong>in</strong>g all of<br />

the methods of the deque ADT (Section 5.3) <strong>and</strong> all of the methods from the array<br />

list ADT (Section 6.1). In addition, as we mentioned above, it also provides<br />

functionality similar to that of the node list ADT through the use of its list iterator.<br />

Performance of the java.util.L<strong>in</strong>kedList Class<br />

The documentation for the java.util.L<strong>in</strong>kedList class makes it clear that<br />

this class is implemented with a doubly l<strong>in</strong>ked list. Thus, all of the update<br />

methods of the associated list iterator run <strong>in</strong> O(1) time each. Likewise, all of the<br />

methods of the deque ADT also run <strong>in</strong> O(1) time each, s<strong>in</strong>ce they merely <strong>in</strong>volve<br />

updat<strong>in</strong>g or query<strong>in</strong>g the list at its ends. But the methods from the array list ADT<br />

are also <strong>in</strong>cluded <strong>in</strong> the java.util.L<strong>in</strong>kedList, which are, <strong>in</strong> general, not<br />

well-suited to an implementation of a doubly l<strong>in</strong>ked list.<br />

In particular, s<strong>in</strong>ce a l<strong>in</strong>ked list does not allow for <strong>in</strong>dexed access to its elements,<br />

perform<strong>in</strong>g the operation get(i), to return the element at a given <strong>in</strong>dex i, requires<br />

that we perform l<strong>in</strong>k "hopp<strong>in</strong>g" from one of the ends of the list, count<strong>in</strong>g up or<br />

down, until we locate the node stor<strong>in</strong>g the element with <strong>in</strong>dex i. As a slight<br />

optimization, we observe that we can start this hopp<strong>in</strong>g from the closer end of the<br />

list, thus achiev<strong>in</strong>g a runn<strong>in</strong>g time that is<br />

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

352

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

Saved successfully!

Ooh no, something went wrong!