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.

less queue capacity than this, but if we have a good capacity estimate, then the<br />

array-based implementation is quite efficient.<br />

5.2.3 Implement<strong>in</strong>g a Queue with a Generic L<strong>in</strong>ked List<br />

We can efficiently implement the queue ADT us<strong>in</strong>g a generic s<strong>in</strong>gly l<strong>in</strong>ked list. For<br />

efficiency reasons, we choose the front of the queue to be at the head of the list, <strong>and</strong><br />

the rear of the queue to be at the tail of the list. In this way, we remove from the<br />

head <strong>and</strong> <strong>in</strong>sert at the tail. (Why would it be bad to <strong>in</strong>sert at the head <strong>and</strong> remove at<br />

the tail?) Note that we need to ma<strong>in</strong>ta<strong>in</strong> references to both the head <strong>and</strong> tail nodes of<br />

the list. Rather than go <strong>in</strong>to every detail of this implementation, we simply give a<br />

<strong>Java</strong> implementation for the fundamental queue methods <strong>in</strong> Code Fragment 5.15.<br />

Code Fragment 5.15: Methods enqueue <strong>and</strong><br />

dequeue <strong>in</strong> the implementation of the queue ADT by<br />

means of a s<strong>in</strong>gly l<strong>in</strong>ked list, us<strong>in</strong>g nodes from class<br />

Node of Code Fragment 5.6.<br />

294

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

Saved successfully!

Ooh no, something went wrong!