MCS-208 · Unit 5

Queues

Section 5 of 8

Block 2 · Unit 5Queues7 min · Section 5 of 8

5.5Circular Queues Using Arrays

Covers IGNOU MCS-208 §5.5, 5.5.1

The linear array queue wastes the cells freed at the front. A circular queue treats the array as a ring: after the last cell comes the first cell again. All cells can then be reused, and every operation stays O(1)O(1).

Wrapping around

The modulo operator % gives the remainder after division, so (i + 1) % n equals i + 1 for i<n−1i < n - 1 and 0 for i=n−1i = n - 1.

A circular queue of 5 cells after wrapping (items 30 40 50 60 0)
index:     0     1     2     3     4
         [ 60 |  0  | 30  | 40  | 50 ]
                  ^     ^
                rear  front        (the queue reads 30 40 50 60 0)

Telling full from empty

With front = first item and rear = last item, both a full and an empty circular queue can have rear just before front. Two standard ways to tell them apart:

MethodEmptyFullCapacity
Keep a count of itemscount == 0count == nnn items
Leave one cell always unused (front points one before the first item, Horowitz–Sahni style)front == rear(rear + 1) % n == frontn−1n - 1 items

Both are correct. The first uses all cells; the second avoids an extra variable.

Algorithms (with a counter)

  • Add x: if count == n, overflow; else rear = (rear + 1) % n, items[rear] = x, count++.
  • Delete: if count == 0, underflow; else x = items[front], front = (front + 1) % n, count--.

Initially front = 0, rear = n - 1 and count = 0, so the first add puts its item in cell 0.

Key takeaways

  • Next index in a circular queue: (i + 1) % n.
  • Freed cells are reused, so a circular queue of nn cells really holds up to nn (or n−1n - 1) items.
  • Full/empty: keep a count, or leave one cell unused; never use special values like 0 or NULL.
  • Add and delete are both O(1)O(1).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a circular queue of size 8, rear = 7. At which index is the next item stored?

Show answer

Answer: B. 0

(7 + 1) % 8 = 0. Index 8 does not exist in an array of size 8.

Question 2 · multiple choice

A circular queue with the one-empty-cell method has size 6, front = 2 and rear = 1. Is it full?

Show answer

Answer: C. Yes, because (rear + 1) % 6 == front.

(1 + 1) % 6 = 2 = front, the full condition, and the queue holds 6−1=56 - 1 = 5 items. rear < front only shows that the queue has wrapped around.

Question 3 · multiple choice

What does this program print?

C · q5-circular.c
#include <stdio.h>

#define N 4

int main(void)
{
    int q[N], front = 0, rear = N - 1, count = 0;
    for (int v = 1; v <= 3; v++) {          /* add 1, 2, 3 */
        rear = (rear + 1) % N; q[rear] = v; count++;
    }
    front = (front + 1) % N; count--;        /* delete */
    front = (front + 1) % N; count--;        /* delete */
    for (int v = 4; v <= 6; v++) {          /* add 4, 5, 6 */
        rear = (rear + 1) % N; q[rear] = v; count++;
    }
    printf("front=%d rear=%d count=%d first=%d\n", front, rear, count, q[front]);
    return 0;
}
Show answer

Answer: B. front=2 rear=1 count=4 first=3

1, 2, 3 go to cells 0–2; two deletions move front to 2; 4, 5, 6 go to cells 3, 0, 1. The queue holds 3 4 5 6 with rear = 1. Index 5 cannot occur with % 4.

Question 4 · multiple choice

Why is queue[i] != 0 a bad way to decide whether a cell of a circular queue is in use?

Show answer

Answer: D. Because 0 may be a real item, which would then look like an empty cell

The queue state must be kept in the indices (and a counter), not in the data. Program 5.6 cannot store 0 safely for this reason.

Question 5 · multiple choice

What is the main advantage of a circular queue over a linear array queue?

Show answer

Answer: C. Cells freed at the front can be reused, so space is not wasted.

Both queues have O(1)O(1) operations; the difference is space use. A circular array queue still has a fixed capacity.

Question 6 · short answer

Write the algorithms to add and delete an item in a circular queue of size nn using the one-empty-cell method (front points one position before the first item).

Show worked answer

Initially front = rear = 0 (empty). ADD(x): (1) next = (rear + 1) % n; (2) if next == front, print “queue overflow” and stop; (3) rear = next; (4) q[rear] = x. DELETE(): (1) if front == rear, print “queue empty” and stop; (2) front = (front + 1) % n; (3) return q[front]. The queue holds at most n−1n - 1 items; both operations are O(1)O(1).

↑ ↓ move↵ openTab switch type