Lesson 9 +30 XP

Queues, Circular Queues, DEQueue & Priority Queues

Queues, Circular Queues, DEQueue & Priority Queues

A Queue is a linear FIFO (First-In, First-Out) ADT: insertions (Enqueue) happen at REAR, and deletions (Dequeue) happen at FRONT.

Circular Queue Ring BufferCircular Queue Ring Buffer

1. Linear Queue & Its Drawback

In a fixed-size array linear queue, FRONT advances on every dequeue. Once REAR == MAX - 1, the queue reports Overflow even if indices 0 .. FRONT-1 are empty!

2. Circular Queue

A Circular Queue wraps indices around a ring buffer using modulo arithmetic:

  • Empty Condition: FRONT == -1
  • Full Condition: (REAR + 1) % MAX == FRONT
C
void enqueue(int q[], int *front, int *rear, int max, int val) {
    if ((*rear + 1) % max == *front) { printf("Overflow\n"); return; }
    if (*front == -1) *front = 0;
    *rear = (*rear + 1) % max;
    q[*rear] = val;
}

int dequeue(int q[], int *front, int *rear, int max) {
    if (*front == -1) { printf("Underflow\n"); return -1; }
    int val = q[*front];
    if (*front == *rear) *front = *rear = -1; // Queue becomes empty
    else *front = (*front + 1) % max;
    return val;
}
  • Linked Queue: Maintains front and rear pointers on a Singly Linked List for O(1) dynamic Enqueue and Dequeue with no fixed capacity.

3. DEQueue (Double-Ended Queue)

Supports insertFront, insertRear, deleteFront, and deleteRear in O(1) (via circular array or doubly linked list):

  • Input-Restricted DEQueue: Insert at one end only; delete from both ends.
  • Output-Restricted DEQueue: Insert at both ends; delete from one end only.
  • Applications: Undo/Redo buffers, Palindrome verification (compare front & rear chars), Sliding Window Maximum, and CPU work-stealing queues.

4. Priority Queue

Every element carries a priority; the highest-priority item is dequeued first (FIFO breaks ties):

  • Multiple-Queue Array: Separate FIFO queue per priority level (O(1) enqueue, O(P) dequeue where P = priority levels).
  • Linked Representation: Sorted singly linked list with nodes [INFO | PRN | LINK] ordered by priority PRN (O(n) insert, O(1) extract-highest).
  • Binary Heap (Unit II): O(log n) insert and O(log n) extract-best general-purpose implementation!
  1. Multiple Stacks & Multiple Queues in One Array (Practice Classic)

To share a single array A[0 .. MAX-1] efficiently without wasting space:

  • Two Stacks in One Array: Stack 1 grows upward from top1 = -1 (0, 1, ...), and Stack 2 grows downward from top2 = MAX (MAX-1, MAX-2, ...). Overflow occurs only when top1 + 1 == top2!
  • k Stacks / k Queues in One Array: Divide MAX into k partitions using top[k] / front[k], rear[k] and bound[k] boundary pointers (or a next[] free-list array for O(1) dynamic space sharing).

Queue Algorithms (from the notes)

Linear queue-QINSERT(QUEUE, N, FRONT, REAR, ITEM), empty queue has FRONT = REAR = −1:

  1. If REAR = N − 1, write OVERFLOW and Exit.
  2. If FRONT = −1 (empty), set FRONT := 0 and REAR := 0; else set REAR := REAR + 1.
  3. Set QUEUE[REAR] := ITEM.
  4. Exit.

Linear queue-QDELETE(QUEUE, FRONT, REAR, ITEM)

  1. If FRONT = −1 (or FRONT > REAR), write UNDERFLOW and Exit.
  2. Set ITEM := QUEUE[FRONT].
  3. If FRONT = REAR (only one element), set FRONT := REAR := −1; else FRONT := FRONT + 1.
  4. Exit.

Circular queue-CQDELETE

  1. If FRONT = −1, write UNDERFLOW and Exit.
  2. Set ITEM := QUEUE[FRONT].
  3. If FRONT = REAR, set FRONT := REAR := −1 (queue becomes empty).
  4. Else if FRONT = N − 1, set FRONT := 0 (wrap around).
  5. Else set FRONT := FRONT + 1.
  6. Exit.

Linked queue-insert: get NEW (OVERFLOW if no memory); INFO[NEW] := ITEM; LINK[NEW] := NULL; if FRONT = NULL then FRONT := REAR := NEW, else LINK[REAR] := NEW and REAR := NEW.

Linked queue-delete: if FRONT = NULL, UNDERFLOW; ITEM := INFO[FRONT]; TEMP := FRONT; FRONT := LINK[FRONT]; if FRONT = NULL set REAR := NULL; free TEMP.

Priority Queue Representations Compared

RepresentationInsertDelete highest priorityNotes
Unsorted arrayO(1) (append)O(n) (search + shift)Simple
Sorted arrayO(n) (shift to make room)O(1) (take from one end)
One array (queue) per priority levelO(1)O(k) for k levelsGood when priorities are few and small integers
Sorted linked list (node = INFO, PRN, LINK)O(n) (walk to position)O(1) (remove first node)Equal priorities keep FIFO order
Binary heapO(log n)O(log n)The standard choice-see the Heaps lesson

In the linked representation each node holds INFO, its priority number PRN and LINK; a new element is inserted after all nodes with the same or higher priority, so equal priorities are served first-come-first-served.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What is the drawback of a Linear Queue? Write C functions for Enqueue and Dequeue in a Circular Queue. (7.5 Marks)
  • Write: False-overflow explanation when REAR == MAX - 1, modulo wrap-around formulas, (REAR + 1) % MAX == FRONT full check, and clean C code.
  • Q2. Explain DEQueue (Input-Restricted vs Output-Restricted) and Priority Queue (Array vs Linked vs Heap). (5 Marks)
  • Write: Definitions of both DEQueue types + applications, and Priority Queue representations ([INFO | PRN | LINK] vs Binary Heap).