Variations of the queue solve the waste of the linear queue and serve special needs such as priorities.

Circular Queue

In a circular queue, the array is treated as a circle: after the last position comes the first. The indices move with rear = (rear + 1) % MAX and front = (front + 1) % MAX, so freed cells at the front are reused. The queue is empty when front = −1, and full when (rear + 1) % MAX = front. Another common method keeps a count of the elements. In a linked list, a circular queue can be made by keeping a rear pointer whose next points to the front node. Circular queues are used in CPU scheduling, keyboard buffers, traffic light control and streaming buffers.

Definition: Circular queue. A queue in which the last position of the array is followed by the first, so that space freed by deletions is reused, using indices taken modulo the size of the array.

Double-Ended Queue

A double-ended queue (deque) allows insertion and deletion at both ends. An input-restricted deque allows insertion at one end only, and an output-restricted deque allows deletion at one end only. A deque can act as both a stack and a queue. It is used in the undo-redo lists of editors and in the sliding window technique of algorithms.

Priority Queue

In a priority queue, each element has a priority, and the element with the highest priority is removed first, whatever the order of arrival. Elements of equal priority are served in FIFO order. It can be held in an array or sorted linked list, with O(n) insertion, or more efficiently in a heap, with O(log n) insertion and deletion. Uses are CPU scheduling by priority, emergency rooms in hospitals, Dijkstra's shortest path algorithm, and Huffman coding.

Applications of Queues

Queues are used wherever things are served in order of arrival: print spooling, CPU and disk scheduling, buffers in networks and input-output, call centres, breadth-first search of graphs, and simulations of real queues.

Table 3.6: Types of queues

Type Rule Use
Simple Insert rear, delete front Ticket line
Circular Indices wrap around CPU round robin, buffers
Deque Insert and delete at both ends Undo-redo, sliding window
Priority Highest priority first Emergency room, Dijkstra

Example 3.6: Circular queue wrap-around

Problem: In a circular queue of size 5, enqueue 10, 20, 30, 40, 50, dequeue twice, then enqueue 60 and 70. Show the array.

Solution:

  1. After five enqueues: front = 0, rear = 4. The queue is full.
  2. Two dequeues: front = 2.
  3. Enqueue 60: rear = (4 + 1) % 5 = 0. Enqueue 70: rear = 1.
  4. The array is [60, 70, 30, 40, 50], with front = 2, rear = 1.

Result: The freed cells 0 and 1 are reused, and the queue in order is 30 40 50 60 70.

#include <stdio.h>
#define MAX 5
int q[MAX], front = -1, rear = -1;
void enq(int v) {
    if ((rear + 1) % MAX == front) { printf("Full\n"); return; }
    if (front == -1) front = 0;
    rear = (rear + 1) % MAX; q[rear] = v;
}
int deq(void) {
    int v = q[front];
    if (front == rear) front = rear = -1;
    else front = (front + 1) % MAX;
    return v;
}
int main(void) {
    for (int v = 10; v <= 50; v += 10) enq(v);
    deq(); deq();
    enq(60); enq(70);
    int i = front;
    do {
        printf("%d ", q[i]); i = (i + 1) % MAX;
    } while (i != (rear + 1) % MAX);
    printf("(front=%d rear=%d)\n", front, rear);
    return 0;
}

Output

30 40 50 60 70 (front=2 rear=1)

Common mistakes

  • Forgetting the modulo when moving front or rear in a circular queue.
  • Using front = rear as the full condition, which is the same as empty in some designs.
  • Serving a priority queue in arrival order.

Key takeaways

  • A circular queue reuses space with rear = (rear + 1) % MAX.
  • It is full when (rear + 1) % MAX = front.
  • A deque allows insertion and deletion at both ends.
  • A priority queue removes the highest-priority element first, best done with a heap.

Practice questions

  1. How does a circular queue avoid wasted space?
  2. What is a deque?
  3. Give two applications of a priority queue.

Answers to the practice questions

  1. Its indices wrap round using the modulo of the array size, so freed cells at the front are reused.
  2. A double-ended queue that allows insertion and deletion at both ends.
  3. Priority CPU scheduling and Dijkstra's shortest path algorithm.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III