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.

Sec. 4.3 Queues 127frontfront20 51217rear41217330(a)(b)rearFigure 4.26 The circular queue with array positions increasing in the clockwisedirection. (a) The queue after the initial four numbers 20, 5, 12, <strong>and</strong> 17 have beeninserted. (b) The queue after elements 20 <strong>and</strong> 5 are deleted, following which 3,30, <strong>and</strong> 4 are inserted.position in the array to the lowest-numbered position. This is easily implementedthrough use of the modulus oper<strong>at</strong>or (denoted by % in Java). In this way, positionsin the array are numbered from 0 through size−1, <strong>and</strong> position size−1 is definedto immedi<strong>at</strong>ely precede position 0 (which is equivalent to position size %size). Figure 4.26 illustr<strong>at</strong>es this solution.There remains one more serious, though subtle, problem to the array-basedqueue implement<strong>at</strong>ion. How can we recognize when the queue is empty or full?Assume th<strong>at</strong> front stores the array index for the front element in the queue, <strong>and</strong>rear stores the array index for the rear element. If both front <strong>and</strong> rear have thesame position, then with this scheme there must be one element in the queue. Thus,an empty queue would be recognized by having rear be one less than front (takinginto account the fact th<strong>at</strong> the queue is circular, so position size−1 is actuallyconsidered to be one less than position 0). But wh<strong>at</strong> if the queue is completely full?In other words, wh<strong>at</strong> is the situ<strong>at</strong>ion when a queue with n array positions availablecontains n elements? In this case, if the front element is in position 0, then therear element is in position size−1. But this means th<strong>at</strong> the value for rear is oneless than the value for front when the circular n<strong>at</strong>ure of the queue is taken intoaccount. In other words, the full queue is indistinguishable from the empty queue!You might think th<strong>at</strong> the problem is in the assumption about front <strong>and</strong> rearbeing defined to store the array indices of the front <strong>and</strong> rear elements, respectively,<strong>and</strong> th<strong>at</strong> some modific<strong>at</strong>ion in this definition will allow a solution. Unfortun<strong>at</strong>ely,the problem cannot be remedied by a simple change to the definition for front<strong>and</strong> rear, because of the number of conditions or st<strong>at</strong>es th<strong>at</strong> the queue can be in.Ignoring the actual position of the first element, <strong>and</strong> ignoring the actual values ofthe elements stored in the queue, how many different st<strong>at</strong>es are there? There canbe no elements in the queue, one element, two, <strong>and</strong> so on. At most there can be

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

Saved successfully!

Ooh no, something went wrong!