11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

126 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesfrontrear20 5 12 17(a)frontrear12 17 3 30 4(b)Figure 4.25 After repe<strong>at</strong>ed use, elements in the array-based queue will drift tothe back of the array. (a) The queue after the initial four numbers 20, 5, 12, <strong>and</strong> 17have been inserted. (b) The queue after elements 20 <strong>and</strong> 5 are deleted, followingwhich 3, 30, <strong>and</strong> 4 are inserted.Assume th<strong>at</strong> there are n elements in the queue. By analogy to the array-basedlist implement<strong>at</strong>ion, we could require th<strong>at</strong> all elements of the queue be stored in thefirst n positions of the array. If we choose the rear element of the queue to be inposition 0, then dequeue oper<strong>at</strong>ions require only Θ(1) time because the front elementof the queue (the one being removed) is the last element in the array. However,enqueue oper<strong>at</strong>ions will require Θ(n) time, because the n elements currently inthe queue must each be shifted one position in the array. If instead we chose therear element of the queue to be in position n − 1, then an enqueue oper<strong>at</strong>ion isequivalent to an append oper<strong>at</strong>ion on a list. This requires only Θ(1) time. Butnow, a dequeue oper<strong>at</strong>ion requires Θ(n) time, because all of the elements mustbe shifted down by one position to retain the property th<strong>at</strong> the remaining n − 1queue elements reside in the first n − 1 positions of the array.A far more efficient implement<strong>at</strong>ion can be obtained by relaxing the requirementth<strong>at</strong> all elements of the queue must be in the first n positions of the array.We will still require th<strong>at</strong> the queue be stored be in contiguous array positions, butthe contents of the queue will be permitted to drift within the array, as illustr<strong>at</strong>edby Figure 4.25. Now, both the enqueue <strong>and</strong> the dequeue oper<strong>at</strong>ions can beperformed in Θ(1) time because no other elements in the queue need be moved.This implement<strong>at</strong>ion raises a new problem. Assume th<strong>at</strong> the front element ofthe queue is initially <strong>at</strong> position 0, <strong>and</strong> th<strong>at</strong> elements are added to successivelyhigher-numbered positions in the array. When elements are removed from thequeue, the front index increases. Over time, the entire queue will drift towardthe higher-numbered positions in the array. Once an element is inserted into thehighest-numbered position in the array, the queue has run out of space. This happensdespite the fact th<strong>at</strong> there might be free positions <strong>at</strong> the low end of the arraywhere elements have previously been removed from the queue.The “drifting queue” problem can be solved by pretending th<strong>at</strong> the array iscircular <strong>and</strong> so allow the queue to continue directly from the highest-numbered

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

Saved successfully!

Ooh no, something went wrong!