Machine-translated by Google. Questions are authored in English.
Top

Discussion

If a queue is implemented by a circular array QUEUE [0..14]. The number of elements in the queue if FRONT = 11 and REAR = 4 will be

  • A.4
  • B.5
  • C.6
  • D.8

Answer: D

The queue holds 8 elements.

QUEUE [0..14] has 15 slots. When REAR has wrapped round past the end of the array and is now less than FRONT, the count is found by adding the array size before taking the difference.

\[ \begin{aligned} \text{count} &= (\text{REAR} - \text{FRONT} + N) \bmod N \\ &= (4 - 11 + 15) \bmod 15 \\ &= 8 \end{aligned} \]

Counting slots directly gives the same answer: positions 11, 12, 13 and 14 at the end of the array, then 0, 1, 2 and 3 after wrapping, which is 8. Here FRONT marks the first element and REAR the next free slot. The value 4 is just REAR itself, 5 and 6 match no count of the occupied slots, and treating REAR as the last element instead would give 9.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.