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

Discussion

In a circular queue implemented using an array of size 5, if front = 2 and rear = 4, how many elements are currently in the queue ?

  • A.2
  • B.3
  • C.4
  • D.Cannot be determined

Answer: B

There are 3 elements in the queue.

With front pointing to the first element and rear to the last, the queue occupies positions 2, 3 and 4. Since rear is not behind front, there is no wrap-around:

\[ \begin{aligned} \text{count} &= \text{rear} - \text{front} + 1 \\ &= 4 - 2 + 1 = 3 \end{aligned} \]

2 is the slip of computing rear minus front without adding 1. In general a circular queue uses \((\text{rear} - \text{front} + 1 + n) \bmod n\) for a non-empty queue, so the count can be determined.

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

Post your comment

Please log in or register to post a comment.