📜  数据结构 |队列 |问题 11

📅  最后修改于: 2021-09-08 13:49:18             🧑  作者: Mango

假设一个容量为 (n – 1) 个元素的循环队列是用 n 个元素的数组实现的。假设插入和删除操作分别使用REAR和FRONT作为数组索引变量。最初,REAR = FRONT = 0。检测队列满和队列空的条件是
(A)满:(REAR+1) mod n == FRONT,空:REAR == FRONT
(B)满:(REAR+1) mod n == FRONT,空:(FRONT+1) mod n == REAR
(C)满:REAR == FRONT,空:(REAR+1) mod n == FRONT
(D)满:(FRONT+1) mod n == REAR,空:REAR == FRONT答案:(一)
解释:

Suppose we start filling the queue.

Let the maxQueueSize ( Capacity of the Queue) is 4.
So the size of the array which is used to implement 
this circular queue is 5, which is n.

In the beginning when the queue is empty, FRONT and REAR 
point to 0 index in the array.

REAR represents insertion at the REAR index.
FRONT represents deletion from the FRONT index.

enqueue("a"); REAR = (REAR+1)%5; ( FRONT = 0, REAR = 1)
enqueue("b"); REAR = (REAR+1)%5; ( FRONT = 0, REAR = 2)
enqueue("c"); REAR = (REAR+1)%5; ( FRONT = 0, REAR = 3)
enqueue("d"); REAR = (REAR+1)%5; ( FRONT = 0, REAR = 4)

Now the queue size is 4 which is equal to the maxQueueSize. 
Hence overflow condition is reached.

Now, we can check for the conditions.

When Queue Full :

( REAR+1)%n = (4+1)%5 = 0

FRONT is also 0.

Hence ( REAR + 1 ) %n is equal to FRONT.

When Queue Empty :

REAR was equal to FRONT when empty ( because in the starting 
before filling the queue FRONT = REAR = 0 )

Hence Option A is correct. 

这个问题的测验