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) O ( 1 ) .
Wrapping around
Intuition
Seats around a round table are numbered 0 to n − 1 n - 1 n − 1 . After seat n − 1 n - 1 n − 1 comes seat 0 again. Guests sit down at the “rear” and leave from the “front”, and both positions go round and round the table.
Definition 5.6 · Circular queue
A circular queue is an array queue in which the indices wrap around: after the last index n − 1 n - 1 n − 1 , the next index is 0. In C the next index is computed as (i + 1) % n.
The modulo operator % gives the remainder after division, so (i + 1) % n equals i + 1 for i < n − 1 i < n - 1 i < n − 1 and 0 for i = n − 1 i = n - 1 i = n − 1 .
A circular queue of 5 cells after wrapping (items 30 40 50 60 0) Copy 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:
Method Empty Full Capacity Keep a count of items count == 0count == nn n n itemsLeave one cell always unused (front points one before the first item, Horowitz–Sahni style) front == rear(rear + 1) % n == frontn − 1 n - 1 n − 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.
Example 5.7 A circular queue of 5 cells
Goal. Fill a 5-cell circular queue, try to overflow it, delete two items, add two more (they wrap to cells 0 and 1, and one of them is the value 0), then empty it.
Program.
C · circular_queue.c Copy #include <stdio.h>
#define MAX 5
typedef struct {
int items[MAX];
int front; /* index of the first element */
int rear; /* index of the last element */
int count; /* number of elements: tells full/empty */
} CQueue;
void create(CQueue * q) { q->front = 0 ; q->rear = MAX - 1 ; q->count = 0 ; }
int add(CQueue * q, int x)
{
if (q->count == MAX)
return 0 ; /* overflow */
q->rear = (q->rear + 1 ) % MAX; /* after MAX-1 comes 0 */
q->items[q->rear] = x;
q->count ++ ;
return 1 ;
}
int delete_front(CQueue * q, int * x)
{
if (q->count == 0 )
return 0 ; /* underflow */
* x = q->items[q->front];
q->front = (q->front + 1 ) % MAX;
q->count -- ;
return 1 ;
}
void show( const CQueue * q)
{
printf( "front=%d rear=%d count=%d queue:" , q->front, q->rear, q->count);
for ( int k = 0 , i = q->front; k < q->count; k ++ , i = (i + 1 ) % MAX)
printf( " %d" , q->items[i]);
printf( " \n " );
}
int main( void )
{
CQueue q;
int x;
create( & q);
for ( int v = 10 ; v <= 50 ; v += 10 )
add( & q, v);
show( & q);
printf( "add 60: %s \n " , add( & q, 60 ) ? "ok" : "overflow" );
delete_front( & q, & x);
delete_front( & q, & x);
show( & q);
add( & q, 60 ); /* goes into cell 0 */
add( & q, 0 ); /* the value 0 is stored normally */
show( & q);
while (delete_front( & q, & x))
printf( "deleted %d \n " , x);
show( & q);
return 0 ;
}
Run it.
Output Copy front=0 rear=4 count=5 queue: 10 20 30 40 50
add 60: overflow
front=2 rear=4 count=3 queue: 30 40 50
front=2 rear=1 count=5 queue: 30 40 50 60 0
deleted 30
deleted 40
deleted 50
deleted 60
deleted 0
front=2 rear=1 count=0 queue:
Result. After two deletions the queue accepts two more items in cells 0 and 1, so no cell is wasted; the value 0 is stored and deleted like any other value.
Correction · Program 5.6 and the circular queue algorithm
Study material says: §5.5 detects overflow when “front points where rear is pointing and the queue holds a not NULL value”, and Program 5.6 uses if (front == rear && queue[front] != NULL) for “full” and if (front == NULL) for “empty”, writes queue[front] = NULL after deleting, and moves rear before checking for overflow. The text also says front and rear both start at index 1, while the program starts with front = 0, rear = -1.
Correct version: Tell full from empty with a counter (count == n / count == 0) or by leaving one cell unused ((rear + 1) % n == front / front == rear), never by looking at the stored values. Check for overflow before moving rear, and use one consistent set of initial values.
Why: front == NULL is the same as front == 0, so the program reports “Queue empty” whenever front is at index 0: a test run that added 5 and 6 and then deleted printed “Error - Queue empty”. Using 0 (NULL) as an “empty cell” marker also means the value 0 can never be stored safely. NULL is a null-pointer constant and should not be compared with or stored in int cells.
Key idea
A circular queue moves front and rear with (index + 1) % n, so freed cells are reused and every operation is O ( 1 ) O(1) O ( 1 ) ; full and empty must be told apart with a counter or by keeping one cell unused.
TEE Exam note
Circular queue questions are very common (5–8 marks). Draw the array as a ring or a row with front and rear marked, show one wrap-around, write the add and delete algorithms with the modulo operator, and state your full/empty conditions clearly (counter method or one-empty-cell method). Check Your Progress 2, Q3: in C the index of a circular queue starts at 0 .
Common mistake
Writing rear = rear + 1 without % n. The index then runs past the end of the array instead of wrapping to 0.
Common mistake
Using front == rear as the “full” test with the one-empty-cell method. In that method front == rear means empty ; full is (rear + 1) % n == front.
Key takeaways
Next index in a circular queue: (i + 1) % n.
Freed cells are reused, so a circular queue of n n n cells really holds up to n n n (or n − 1 n - 1 n − 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) 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?
A 8 B 0 C 1 D 7
Check answer
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?
A No, it is empty. B No, it holds 1 item. C Yes, because (rear + 1) % 6 == front. D Yes, because rear < front.
Check answer
Show answer Answer: C. Yes, because (rear + 1) % 6 == front.
(1 + 1) % 6 = 2 = front, the full condition, and the queue holds 6 − 1 = 5 6 - 1 = 5 6 − 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 Copy #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 ;
} A front=2 rear=5 count=4 first=3 B front=2 rear=1 count=4 first=3 C front=0 rear=1 count=4 first=5 D front=2 rear=1 count=6 first=1
Check answer
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?
A Because comparing integers is slow B Because cells are always 0 after deletion C Because the modulo operator changes the stored values D Because 0 may be a real item, which would then look like an empty cell
Check answer
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?
Check answer
Show answer Answer: C. Cells freed at the front can be reused, so space is not wasted.
Both queues have O ( 1 ) 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 n n n 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 − 1 n - 1 n − 1 items; both operations are O ( 1 ) O(1) O ( 1 ) .