A stack is a LIFO data structure — the last item added is the first to be removed. Like a stack of plates. The top is the only accessible point.
Push: add an item to the top of the stack
Pop: remove the top item from the stack
Peek: view the top item without removing it
Real-world use: undo function in software, call stack in programming, back button in browsers
The Queue — FIFO
First In, First Out
A queue is a FIFO data structure — the first item added is the first to be removed. Like a queue of people at a checkout.
Enqueue: add item to the back of the queue
Dequeue: remove item from the front of the queue
Real-world use: print queue, job scheduling, buffering in streaming
Circular queue: when the end wraps around to the beginning — efficient use of memory
Stack vs Queue in Python
Using Lists to Implement Both
# Stack using list
stack = []
stack.append(10) # push
stack.append(20)
top = stack.pop() # pop → 20
# Queue using list
queue = []
queue.append("A") # enqueue
front = queue.pop(0) # dequeue → "A"
Exam Practice
Have a go at this question
Edexcel-style question
A program uses a stack. The following operations are performed in order: Push 3, Push 7, Push 1, Pop, Push 5, Pop. State the contents of the stack after all operations.