Loading lessons...
Deques, Priority Queues & Multiple Stacks/Queues
Deques, Priority Queues & Multiple Stacks/Queues
Ordinary queues serve strictly in arrival order from one end. Two useful variations relax this: a deque allows insertion and deletion at both ends, and a priority queue serves the most important element first, whatever its arrival time. This lesson also shows how several stacks or queues can share one array.
1. Deque (Double-Ended Queue)
A deque (pronounced "deck") is a linear list in which elements can be inserted and deleted at both ends - the FRONT and the REAR - but not in the middle.
insert/delete insert/delete
<--> +----+----+----+----+ <-->
FRONT | 10 | 20 | 30 | 40 | REAR
+----+----+----+----+A deque is a generalization of both a stack and a queue:
- Use only one end → it behaves as a stack.
- Insert at one end, delete at the other → it behaves as a queue.
Operations
| Operation | Meaning |
|---|---|
insertFront(x) | Add x at the front |
insertRear(x) | Add x at the rear |
deleteFront() | Remove the front element |
deleteRear() | Remove the rear element |
getFront(), getRear() | Read either end |
isEmpty(), isFull() | Test the state |
Types of deque
| Type | Insertion | Deletion |
|---|---|---|
| Input-restricted deque | At one end only (rear) | At both ends |
| Output-restricted deque | At both ends | At one end only (front) |
Circular array implementation
The deque is stored in a circular array DQ[0 … N−1] with FRONT and REAR, exactly like a circular queue. The two new operations move the indices backwards with wrap-around.
Algorithm INSERT_FRONT(DQ, N, FRONT, REAR, ITEM)
if (FRONT = 0 and REAR = N - 1) or FRONT = REAR + 1 then
Write "OVERFLOW" and Exit
if FRONT = -1 then // empty deque
FRONT ← 0
REAR ← 0
else if FRONT = 0 then
FRONT ← N - 1 // wrap to the last cell
else
FRONT ← FRONT - 1
DQ[FRONT] ← ITEM
ExitinsertRear and deleteFront are the ordinary circular-queue insert and delete. All four operations are O(1). A doubly linked list with head and tail pointers is the natural linked implementation, also O(1) at both ends.
Applications of deques
- Palindrome checking: remove one character from each end and compare, until the deque is empty or has one character.
- Undo/redo history with a limited size (oldest actions drop off the other end).
- Sliding-window maximum - keep candidate indices in a deque.
- Work-stealing schedulers - a thread takes work from its own end; idle threads steal from the other end.
- A-Steal job scheduling, browser history with a size limit.
2. Priority Queue
A priority queue is a collection of elements, each with a priority, in which:
- An element of higher priority is processed before any element of lower priority.
- Two elements with the same priority are processed in the order in which they were added (FIFO).
In many books a smaller number means higher priority (priority 1 is served first). Priority queues are used for CPU scheduling, Dijkstra's and Prim's algorithms, Huffman coding, event simulation and emergency rooms.
Two kinds:
- Ascending (min) priority queue: delete removes the element with the smallest key.
- Descending (max) priority queue: delete removes the element with the largest key.
2.1 One-way list representation
Each node holds INFO, a priority number PRN and LINK. The list is kept sorted by priority; within the same priority, older elements come first.
START
|
v
+---+---+--+ +---+---+--+ +---+---+--+ +---+---+----+
| A | 1 |*-+-->| B | 2 |*-+-->| C | 2 |*-+-->| D | 4 |NULL|
+---+---+--+ +---+---+--+ +---+---+--+ +---+---+----+
INFO PRN LINK- Delete (serve): remove the first node - O(1).
- Insert ITEM with priority P: walk the list and insert ITEM after the last node whose priority number is ≤ P - O(n).
Algorithm PQ_INSERT(START, ITEM, P)
NEW ← new node; INFO[NEW] ← ITEM; PRN[NEW] ← P
if START = NULL or P < PRN[START] then
LINK[NEW] ← START // goes first
START ← NEW
Exit
PTR ← START
while LINK[PTR] ≠ NULL and PRN[LINK[PTR]] ≤ P do
PTR ← LINK[PTR] // skip equal priorities (FIFO)
LINK[NEW] ← LINK[PTR]
LINK[PTR] ← NEW
ExitExample: inserting E with priority 2 into the list above places it after C (the last node with priority 2): A1 → B2 → C2 → E2 → D4.
2.2 Array representation (one queue per priority)
Keep a separate circular queue for each priority level (a 2D array, row k = queue for priority k, with FRONT[k] and REAR[k]).
- Insert with priority k: add to the rear of queue k - O(1).
- Delete: find the first non-empty queue (lowest k) and remove its front - O(m) for m priority levels.
This works well when priorities are a small set of integers.
2.3 Heap representation
A binary heap (Unit II) gives O(log n) insertion and O(log n) deletion of the highest priority element, and is the standard implementation.
Comparison
| Representation | Insert | Delete highest priority |
|---|---|---|
| Unsorted array / list | O(1) | O(n) - search |
| Sorted array | O(n) - shift | O(1) |
| Sorted linked list (INFO, PRN, LINK) | O(n) - walk | O(1) |
| One queue per priority level | O(1) | O(m), m = number of levels |
| Binary heap | O(log n) | O(log n) |
3. Multiple Stacks in One Array
Two stacks
Stack 1 grows upward from the left end (TOP1 starts at −1) and stack 2 grows downward from the right end (TOP2 starts at N). Neither stack has a fixed share: overflow happens only when the whole array is full.
index: 0 1 2 3 4 5 6 7
+----+----+----+----+----+----+----+----+
| a1 | a2 | a3 | | | b2 | b1 | |
+----+----+----+----+----+----+----+----+
^ ^
TOP1 TOP2 (here N = 8, stack 2 began at index 7)Algorithm PUSH1(A, TOP1, TOP2, ITEM)
if TOP1 + 1 = TOP2 then
Write "OVERFLOW" and Exit // the two tops have met
TOP1 ← TOP1 + 1
A[TOP1] ← ITEM
ExitPOP1 returns A[TOP1] and decrements TOP1 (underflow when TOP1 = −1); POP2 returns A[TOP2] and increments TOP2 (underflow when TOP2 = N).
k stacks or k queues
Divide the array into k equal parts with boundary indices; each stack has its own TOP (each queue its own FRONT and REAR) that must stay within its part. This is simple but one part can overflow while others are empty. A more flexible method keeps a next[ ] array linking free cells (like the AVAIL list), so any stack can use any free cell.
Key points to remember - Deque: insert and delete at both ends; input-restricted (insert one end) and output-restricted (delete one end). - Priority queue: highest priority first, FIFO within equal priority. - Linked priority queue: sorted by PRN; delete O(1), insert O(n) after the last node with priority ≤ P. - Heap-based priority queue: O(log n) for both operations. - Two stacks in one array grow towards each other; overflow only when TOP1 + 1 = TOP2.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is a deque? Explain input-restricted and output-restricted deques. Write an algorithm to insert at the front of a circular deque. (7.5 Marks)
- Write: definition with diagram, the two types table,
INSERT_FRONTwith wrap-around, applications. - Q2. What is a priority queue? Explain its linked and array representations. (7.5 Marks)
- Write: the two rules, the INFO/PRN/LINK diagram with insertion example, one-queue-per-priority array, and the comparison table with the heap.
- Q3. How can two stacks be implemented in a single array? Write the PUSH algorithms. (5 Marks)