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.

means of an unsorted or sorted list, respectively. We<br />

assume that the list is implemented by a doubly l<strong>in</strong>ked<br />

list. The space requirement is O(n).<br />

Method<br />

Unsorted List<br />

Sorted List<br />

size, isEmpty<br />

O(1)<br />

O(1)<br />

<strong>in</strong>sert<br />

O(1)<br />

O(n)<br />

m<strong>in</strong>, removeM<strong>in</strong><br />

O(n)<br />

O(1)<br />

<strong>Java</strong> Implementation<br />

In Code Fragments 8.6 <strong>and</strong> 8.8, we show a <strong>Java</strong> implementation of a priority<br />

queue based on a sorted node list. This implementation uses a nested class, called<br />

MyEntry, to implement the Entry <strong>in</strong>terface (see Section 6.5.1). We do not<br />

show auxiliary method checkKey(k), which throws an<br />

InvalidKeyException if key k cannot be compared with the comparator of<br />

the priority queue. Class DefaultComparator, which realizes a comparator<br />

us<strong>in</strong>g the natural order<strong>in</strong>g, is shown <strong>in</strong> Code Fragment 8.7.<br />

Code Fragment 8.6: Portions of the <strong>Java</strong> class<br />

SortedListPriorityQueue, which implements the<br />

PriorityQueue <strong>in</strong>terface. The nested class MyEntry<br />

implements the Entry <strong>in</strong>terface. (Cont<strong>in</strong>ues <strong>in</strong> Code<br />

Fragment 8.8.)<br />

462

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

Saved successfully!

Ooh no, something went wrong!