Estructuras de datos | Cola | Pregunta 11

Supongamos que se implementa una cola circular de elementos de capacidad (n – 1) con una array de n elementos. Suponga que la operación de inserción y eliminación se lleva a cabo utilizando REAR y FRONT como variables de índice de array, respectivamente. Inicialmente, REAR = FRONT = 0. Las condiciones para detectar cola llena y cola vacía son
(A) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
(B) Full: (REAR+1) mod n == DELANTERO, vacío: (FRONTAL+1) mod n == TRASERO
(C) Completo: TRASERO == DELANTERO, vacío: (TRASERO+1) mod n == DELANTERO
(D) Completo: (FRONTAL+1) mod n == TRASERO, vacío: TRASERO == DELANTERO

Respuesta: (A)
Explicación:

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. 

Cuestionario de esta pregunta

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *