- Define and explain Using Queues in your own words
- Use key terms such as queue accurately
- Apply what you have learned to new examples and questions
- Avoid the common mistakes learners make with this topic
This lesson focuses on Using Queues: store data first-in, first-out, like a real queue.
Store data first-in, first-out, like a real queue.
Key ideas
LIFO versus FIFO changes everything
Stacks reverse order — perfect for undo, back buttons and checking balanced brackets. Queues preserve order — perfect for print spooling and handling requests fairly. The operations differ too: stacks push and pop at the top, while queues enqueue at the rear and dequeue at the front. Picking the wrong one silently produces wrong answers.
Graphs model the real world
Roads, friendships, computer networks and dependencies are all graphs: nodes for places or things, edges for connections. Weighted edges let algorithms such as Dijkstra's find the cheapest route. Unlike trees, graphs can contain cycles — paths that loop back — which algorithms must handle to avoid going round forever.
Key term — queue: A first-in, first-out structure: items join at the rear and leave from the front — like a supermarket checkout queue.
A queue holds [A, B, C] with A at the front. After one dequeue and one enqueue of D, what is the queue?
[B, C, D] — A leaves from the front and D joins at the rear.
Answer: [B, C, D] — A leaves from the front and D joins at the rear.
- Using a stack where a queue is needed Correction: ask 'must the first arrival be served first?' — if yes, you need a queue; a stack would serve the last arrival first.
- Popping or dequeuing from an empty structure Correction: always check the structure is not empty first — removing from nothing causes an underflow error.
Practice
A stack — each new page is pushed, and Back pops the most recent page, which is last-in, first-out.
3 — it goes left of root 5, and then 1 goes left of 3.
The values in ascending sorted order.
Nodes are junctions (or towns), edges are the roads between them, and weights are distances or travel times.
Quick check
Which of these best defines "queue"?
True or false: a stack can be used to check whether brackets in an expression are balanced.
- Using Queues: store data first-in, first-out, like a real queue.
- LIFO versus FIFO changes everything: Stacks reverse order — perfect for undo, back buttons and checking balanced brackets.
- node: A single element in a tree or graph, holding data plus links to other nodes.
- Watch out for: using a stack where a queue is needed