Stacks, queues and deferred work
Stacks reverse the order in which pending items are removed. Queues preserve arrival order. Trace the stored sequence after every operation; distinguish removal from inspection.
2.1 Stack states and permutations
Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks
A stack is last in, first out (LIFO). push adds at the top; pop removes and returns the top; peek reads without removal. With an array of capacity and top equal to the number of items, empty means top == 0, full means top == C; push writes a[top++], pop reads a[--top] after checking the boundary. A fixed-array or linked-head push/pop is ; an array stack occupies allocated space.
To test an output permutation for a fixed input order, push incoming items until the next requested output is at the top, then pop it. If it is already buried under a different item and no later input can help, the permutation is impossible. With finite capacity, reject a trace as soon as its occupancy exceeds that capacity.
2.2 Expressions and stack annotations
Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks
For postfix evaluation, scan left to right. Push operands. For a binary operator, pop the right operand first, then the left, compute left operator right, and push the result. A well-formed expression ends with exactly one item. Prefix evaluation scans right to left; the first pop is the left operand.
For infix-to-postfix conversion, send operands directly to output. Hold operators on a stack. Before pushing a left-associative operator, pop operators of higher or equal precedence; for a right-associative operator, pop only higher precedence. Push (; on ), pop through the matching ( and discard both brackets. Exponentiation in mathematical expressions is often right-associative; C's ^ means bitwise XOR, not exponentiation.
A minimum stack keeps a minimum-so-far value or pointer with every pushed record. On pushing key , save the smaller of and the previous minimum; popping restores the previous record's saved minimum. Push, pop and minimum lookup stay with extra saved information. One global minimum pointer alone cannot recover the next minimum after the current minimum is popped. Store equal minima too, or maintain their multiplicities.
2.3 Queues, circular buffers and deques
Past papers · CS 2014–2026 (23 papers): 3 questions, 3 marks · DA 2024–2026 (3 papers): 1 question, 1 mark
A queue is first in, first out (FIFO): enqueue at rear, dequeue at front. A deque permits insertion and deletion at both ends; input-restricted and output-restricted deques restrict insertion or deletion, respectively, to one end.
A seven-slot circular queue: front 5, next-free rear 2, four occupied slots.
A linked queue with head and tail pointers enqueues at tail and dequeues at head in time. Removing its sole node must also reset tail. Reversing these ends makes deletion of the tail in a singly linked list, even with a tail pointer.
2.4 Activation stacks and simulated queues
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
An activation record holds a call's parameters, automatic locals and pending return information. Calls push records; returns pop them. The total number of calls is not the maximum number of simultaneous live records. A static variable is shared across calls; it is not copied into each record.
Implement a queue with an input stack and an output stack. Enqueue pushes onto input. Dequeue pops output; only when output is empty, transfer all input items to output first. Each item is pushed and popped at most once on each stack, so a sequence of queue operations uses stack operations. One dequeue can cost , but its amortised cost is .
A stack with a unit-cost reverse operation can implement a queue with expensive enqueue: reverse, push, reverse; dequeue is pop. The claimed cost depends on reverse being an allowed primitive. An ordinary array reversal is linear.
2.5 Tracing mixed operations
Past papers · CS 2014–2026 (23 papers): 5 questions, 9 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
Write stacks bottom-to-top and queues front-to-rear. Use separate columns for returned values and stored items. A pop changes the next top; a peek does not. With two stacks or queues, track both capacities and preserve operation order.
To implement a stack using two ordinary queues, one design enqueues a new item into the empty auxiliary queue, moves all old items behind it, then exchanges queue roles. Push is linear and pop constant time; shifting that work to pop gives the opposite tradeoff. A queue can be reversed using a stack in linear time and linear extra storage. If only two queues are allowed, repeated rotations in the destination queue can reverse arrival order without enqueuing anything back into the source.
Last-minute recall
- LIFO reverses outstanding arrivals; FIFO preserves them.
- A postfix operator pops its right operand first.
- A stack permutation must be feasible at every prefix.
- Circular-buffer formulae depend on index conventions.
- Reset both linked-queue ends after deleting its only node.
- A call count and a peak stack depth answer different questions.
- Two-stack queues have constant amortised cost, not constant worst-case dequeue cost.
- Store previous minima to support constant-time minimum queries.