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.

which gives the correct result both <strong>in</strong> the "normal" configuration (when f ≤ r) <strong>and</strong><br />

<strong>in</strong> the "wrapped around" configuration (when r < f). The <strong>Java</strong> implementation of a<br />

queue by means of an array is similar to that of a stack, <strong>and</strong> is left as an exercise<br />

(P-5.4).<br />

Table 5.2 shows the runn<strong>in</strong>g times of methods <strong>in</strong> a realization of a queue by an<br />

array. As with our array-based stack implementation, each of the queue methods<br />

<strong>in</strong> the array realization executes a constant number of statements <strong>in</strong>volv<strong>in</strong>g<br />

arithmetic operations, comparisons, <strong>and</strong> assignments. Thus, each method <strong>in</strong> this<br />

implementation runs <strong>in</strong> O(1) time.<br />

Table 5.2: Performance of a queue realized by an<br />

array. The space usage is O(N), where N is the size of<br />

the array, determ<strong>in</strong>ed at the time the queue is created.<br />

Note that the space usage is <strong>in</strong>dependent from the<br />

number n < N of elements that are actually <strong>in</strong> the<br />

queue.<br />

Method<br />

Time<br />

size<br />

O(1)<br />

isEmpty<br />

O(1)<br />

front<br />

O(1)<br />

enqueue<br />

O(1)<br />

dequeue<br />

O(1)<br />

As with the array-based stack implementation, the only real disadvantage of the<br />

array-based queue implementation is that we artificially set the capacity of the<br />

queue to be some fixed value. In a real application, we may actually need more or<br />

293

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

Saved successfully!

Ooh no, something went wrong!