How do stacks and queues control the order items are added and removed, and where are they used?
Describe stacks (LIFO) and queues (FIFO), implement their core operations, and identify suitable applications of each
A focused answer to the H2 Computing outcome on stacks and queues. The LIFO stack and FIFO queue, their push, pop, enqueue and dequeue operations with overflow and underflow, and typical applications of each.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to describe stacks and queues, implement their core operations, and identify where each is used. The central idea is that both restrict where items can be added and removed: a stack works last in, first out, and a queue works first in, first out, and these disciplines match many real processing orders.
The answer
Stacks: last in, first out (LIFO)
A stack allows access only at one end, the top. The most recently added item is the first removed:
- push(x) - add
xto the top. - pop() - remove and return the top item.
- peek() - look at the top without removing it.
- isEmpty() - test if empty.
class Stack:
def __init__(self):
self.items = []
def push(self, x):
self.items.append(x)
def pop(self):
if self.is_empty():
raise IndexError("stack underflow")
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
Popping an empty stack is underflow; pushing onto a full fixed-size stack is overflow.
Queues: first in, first out (FIFO)
A queue adds at the rear and removes from the front, like people in a line:
- enqueue(x) - add
xto the rear. - dequeue() - remove and return the front item.
from collections import deque
class Queue:
def __init__(self):
self.items = deque()
def enqueue(self, x):
self.items.append(x)
def dequeue(self):
if self.is_empty():
raise IndexError("queue underflow")
return self.items.popleft()
def is_empty(self):
return len(self.items) == 0
Circular buffers for array queues
In a fixed-size array, dequeuing from the front would leave a gap, and shifting everything forward is . A circular buffer instead advances front and rear indices that wrap around the array end, so both operations stay with no shifting.
When to use each
All four core operations are . Choose by the order you need:
- Stack - when the most recent item must come out first: call stacks, undo, bracket matching, depth-first search.
- Queue - when items must be served in arrival order: print spooling, request handling, breadth-first search.
Examples in context
Example 1. The browser back button. Each page you visit is pushed onto a stack; pressing back pops the most recent one, returning you to where you just were. The last-in-first-out order is exactly what "go back to the previous page" means, which is why a stack models browsing history.
Example 2. A printer queue. Documents sent to a shared printer are enqueued and printed in the order they arrived, so the first job submitted prints first. First-in-first-out fairness is the whole point of a print spooler, and the same queue discipline schedules requests arriving at a web server.
Try this
Q1. A stack has push(1), push(2), push(3), pop() applied. What is returned and what is on top afterward? [2 marks]
- Cue. pop returns 3 (last in); 2 is now on top.
Q2. Define stack overflow. [1 mark]
- Cue. Attempting to push onto a stack that is already full, with no space for another item.
Q3. Why is a queue, not a stack, used for breadth-first search? [2 marks]
- Cue. BFS must process nodes in the order discovered (level by level), which is first-in-first-out - a queue; a stack would process them depth-first.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original6 marksA stack is initially empty. Show the contents of the stack after each of these operations in order: push(4), push(7), pop(), push(2), push(9), pop(). State the value returned by each pop and what is on top at the end.Show worked answer →
A stack is last in, first out: push adds to the top, pop removes from the top.
| Operation | Stack (bottom to top) | Returned |
|---|---|---|
| push(4) | [4] | - |
| push(7) | [4, 7] | - |
| pop() | [4] | 7 |
| push(2) | [4, 2] | - |
| push(9) | [4, 2, 9] | - |
| pop() | [4, 2] | 9 |
The first pop returns 7 (the most recent item then), the second returns 9. At the end the stack is [4, 2] with 2 on top.
Markers reward LIFO behaviour, the correct returned values (7 then 9), and the final top of 2.
Original5 marks(a) Define the terms stack overflow and queue underflow. (b) For a queue implemented in a fixed-size array, explain why a circular buffer is used rather than shifting elements. (c) Give one real application each for a stack and a queue.Show worked answer →
(a) Stack overflow occurs when a push is attempted on a stack that is already full (no space for another item). Queue underflow occurs when a dequeue is attempted on an empty queue (nothing to remove).
(b) In a fixed array queue, dequeuing from the front would leave a gap; shifting every remaining element forward to fill it is per dequeue. A circular buffer instead moves front and rear pointers around the array, wrapping past the end, so enqueue and dequeue are with no shifting.
(c) A stack: managing function calls (the call stack), undo history, or matching brackets. A queue: print job scheduling, handling requests to a server in arrival order, or breadth-first search.
Markers reward the overflow/underflow definitions, the circular-buffer reason (O(1) without shifting), and one valid application of each.
Related dot points
- Describe arrays and records as data structures, explain constant-time array indexing and contrast static with dynamic arrays
A focused answer to the H2 Computing outcome on arrays and records. Contiguous storage and constant-time indexing, one and two dimensional arrays, records as fields of mixed type, and static versus dynamic arrays.
- Describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array
A focused answer to the H2 Computing outcome on linked lists. Nodes and pointers, traversal, inserting and deleting nodes by relinking, and the trade-offs against arrays for access and modification.
- Implement and trace breadth-first and depth-first traversal of a graph, and contrast the two strategies and their applications
A focused answer to the H2 Computing outcome on graph traversal. Breadth-first search with a queue and depth-first search with a stack or recursion, tracing each on a graph, and choosing between them for shortest paths and exploration.
- Define recursion in terms of base and recursive cases, trace recursive calls, and compare recursion with iteration
A focused answer to the H2 Computing outcome on recursion. Base and recursive cases, tracing the call stack, the divide-and-conquer pattern, and the trade-offs between recursion and iteration.
- Describe the fetch-decode-execute cycle, the registers involved, and how the program counter sequences instructions
A focused answer to the H2 Computing outcome on the fetch-execute cycle. The fetch, decode and execute stages, the program counter, memory address and data registers, the instruction register, and how branching changes the flow.