QUESTION 17
How does a count-based circular queue distinguish full from empty?
2 marksAnswer
count == 0 means empty and count == capacity means full. This representation can use every array position.
QUESTION 17
count == 0 means empty and count == capacity means full. This representation can use every array position.
QUESTION 18
A circular queue reuses the front positions of a bounded array after earlier items are removed.
Keep an array, front index and count. The next insertion index is (front + count) % capacity. This convention avoids confusing full and empty states when front and rear indices happen to be equal. Other valid conventions may reserve a slot and use different conditions.
With capacity 3, enqueue 10, 20 and 30, then dequeue 10. Enqueue 40 uses the newly available wrapped position. Logical order is 20, 30, 40 even though physical index order has wrapped. Display from front for count elements, applying modulo.
Each operation performs fixed work, giving O(1) time. The array uses O(capacity) storage. Capacity must be positive before modulo is used. Do not combine the full condition from a reserved-slot implementation with this count-based implementation without changing the rest of the design.
/* CodeBhavya | Data Structures AR25 | Learning example */
#include <stdio.h>
#define CAP 3
struct Queue { int data[CAP]; int front, count; };
int enqueue(struct Queue *q, int value)
{
if (q->count == CAP) { return 0; }
q->data[(q->front + q->count) % CAP] = value; ++q->count; return 1;
}
int dequeue(struct Queue *q, int *out)
{
if (q->count == 0) { return 0; }
*out = q->data[q->front]; q->front = (q->front + 1) % CAP; --q->count; return 1;
}
int main(void)
{
struct Queue q = {{0}, 0, 0}; int value;
if (!enqueue(&q, 10) || !enqueue(&q, 20) || !enqueue(&q, 30)) { return 1; }
if (dequeue(&q, &value)) { printf("Removed = %d\n", value); }
if (!enqueue(&q, 40)) { return 1; }
while (dequeue(&q, &value)) { printf("%d\n", value); }
return 0;
}The example uses values in the code.
Removed = 10
20
30
40Use the count-based queue in the program, with capacity 3. In the table, “next slot” means (front + count) % 3. It is an insertion position only when the queue is not full.
| Operation | front | count | Next slot | Logical queue |
|---|---|---|---|---|
| Initial state | 0 | 0 | 0 | Empty |
| Enqueue 10 | 0 | 1 | 1 | 10 |
| Enqueue 20 | 0 | 2 | 2 | 10, 20 |
| Enqueue 30 | 0 | 3 | 0 — full | 10, 20, 30 |
| Try enqueue 99 | 0 | 3 | 0 — full | Rejected; still 10, 20, 30 |
| Dequeue | 1 | 2 | 0 | 20, 30 |
| Enqueue 40 | 1 | 3 | 1 — full | 20, 30, 40 |
After the final insertion, the physical array contains 40, 20, 30. Reading physical indexes 0, 1 and 2 would print the wrong queue order. Starting at front gives indexes 1, 2 and 0, so the correct order is 20, 30, 40.
When count is 3, the formula points to the slot holding the oldest item. Writing 99 there before checking fullness would destroy an item that has not been removed. The enqueue function must reject the operation before changing either the array or count.
After all three items are removed, count is zero. Front need not return to zero for the queue to be empty. Another dequeue must return failure, and the caller must not treat the output variable as a newly removed value. The operation does not shift array elements; it updates only an index and a count.