By LearnAllStudents Editorial Team. Source check dates are listed below.
Worked trace: the same inputs, different removals
Put A, B and C into an initially empty stack, in that order. The bottom-to-top order is A, B, C. A pop removes C, leaving A, B. Push D, then pop again: D is returned. The surviving stack is A, B. For an initially empty FIFO queue, enqueue A, B and C; dequeue returns A. Enqueue D and dequeue again returns B, leaving C, D.
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| Operation pair | Stack returned item | FIFO queue returned item |
|---|---|---|
| Add A, B, C; remove once | C | A |
| Add D; remove once | D | B |
Draw a clear top marker for a stack and a front marker for a queue. A list drawn left-to-right can represent either structure, so an unlabelled diagram is ambiguous. The same printed sequence can be interpreted differently if the removal end changes.
Operations and boundary cases
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| Stack operation | Meaning | Ordinary queue counterpart |
|---|---|---|
| push(value) | Add at the top | enqueue(value) at the rear |
| pop() | Remove and return the top | dequeue() removes the front |
| peek() | Read top without removing | Read front without removing |
Removing from an empty structure is an underflow condition in a bounded model. Adding to a full fixed-capacity structure can cause overflow. How a program reports either condition depends on its API or pseudocode convention: it might return a status or raise an error. Do not invent a numeric return value unless the task defines one. Peek and pop must not be treated as synonyms.
Choose a structure from the required order
A simple undo history naturally uses a stack: the latest recorded action is the first candidate to reverse. An ordinary arrival-order service queue naturally uses FIFO. A priority queue is different: removal depends on priority rather than simply the earliest arrival. Real applications may impose additional rules, so describe the simplified task before choosing the structure.
A circular queue reuses array positions when the rear reaches the end. It does not reverse the logical FIFO order. Front and rear conventions vary: some point to occupied cells and some point to the next insertion slot. Follow the given convention when tracing indices. Logical item order and physical array layout are separate views.
Try it yourself
- Stack: push 4, push 9, peek, pop, push 2, pop. What do peek and the two pops return?
- FIFO queue: enqueue 4, enqueue 9, dequeue, enqueue 2, dequeue. What remains?
- Does peek reduce the number of stored items?
- Can a priority queue always be explained as FIFO?
Answers: peek returns 9; the pops return 9 then 2; the FIFO queue retains 2; peek does not remove an item; no, priority changes the removal rule. Re-run the stack trace and check that 4 remains at the bottom. Count additions and removals as an independent check on the final size.
For parents: make a paper trace
Write three different letters on slips. Ask your child to show the allowed addition and removal ends before you give any operations. After each operation, record the returned value separately from the remaining contents. If they accidentally remove the wrong end, ask which rule they used rather than telling them the answer. This checks the abstract behaviour without requiring a particular programming language or software installation.
How a tutor would approach this
Bring a trace with the top, front or rear convention from your course. Work through empty, one-item and full cases as well as the ordinary example.
Frequently asked questions
What does LIFO mean?
Last in, first out: the most recently added surviving item is removed first.
Does peek remove an item?
No. It reads the relevant end without removing that item.
Is every queue FIFO?
An ordinary queue is FIFO. A priority queue selects by priority, so specify which model is being used.
Does a circular queue change the item order?
No. It reuses storage positions while preserving the logical order of the queue model.
Official sources
- AQA A Level Computer Science: data structures (checked 2026-11-27)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo