Loading lessons...
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 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
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
frontandrearpointers 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 priorityPRN(O(n) insert, O(1) extract-highest). - Binary Heap (Unit II): O(log n) insert and O(log n) extract-best general-purpose implementation!
- 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 fromtop2 = MAX(MAX-1, MAX-2, ...). Overflow occurs only whentop1 + 1 == top2! - k Stacks / k Queues in One Array: Divide
MAXinto k partitions usingtop[k]/front[k], rear[k]andbound[k]boundary pointers (or anext[]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:
- If REAR = N − 1, write OVERFLOW and Exit.
- If FRONT = −1 (empty), set FRONT := 0 and REAR := 0; else set REAR := REAR + 1.
- Set QUEUE[REAR] := ITEM.
- Exit.
Linear queue-QDELETE(QUEUE, FRONT, REAR, ITEM)
- If FRONT = −1 (or FRONT > REAR), write UNDERFLOW and Exit.
- Set ITEM := QUEUE[FRONT].
- If FRONT = REAR (only one element), set FRONT := REAR := −1; else FRONT := FRONT + 1.
- Exit.
Circular queue-CQDELETE
- If FRONT = −1, write UNDERFLOW and Exit.
- Set ITEM := QUEUE[FRONT].
- If FRONT = REAR, set FRONT := REAR := −1 (queue becomes empty).
- Else if FRONT = N − 1, set FRONT := 0 (wrap around).
- Else set FRONT := FRONT + 1.
- 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
| Representation | Insert | Delete highest priority | Notes |
|---|---|---|---|
| Unsorted array | O(1) (append) | O(n) (search + shift) | Simple |
| Sorted array | O(n) (shift to make room) | O(1) (take from one end) | |
| One array (queue) per priority level | O(1) | O(k) for k levels | Good 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 heap | O(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.
📝 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 == FRONTfull 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).