BeginnerTopic 19 of 104Software & Computing
Stacks and queues
LIFO and FIFO structures used in parsing, scheduling and buffering.
In depth
A stack is last-in-first-out (push, pop), used for function calls, undo and expression parsing. A queue is first-in-first-out (enqueue, dequeue), used for scheduling and BFS. Deques and circular buffers generalise them. Implementations use arrays or linked lists.
Key points
- O(1) operations
- Call stack
- Producer-consumer queues
ExampleBalanced-parentheses checking uses a stack.
Stacks and queues enforce order of access, which is the basis of undo features, parsers, schedulers and message buffers.
What is Stacks and queues?
LIFO and FIFO structures used in parsing, scheduling and buffering.
Why is it important to understand stacks and queues?
Stacks and queues enforce order of access, which is the basis of undo features, parsers, schedulers and message buffers.
When do I use a stack?
When the most recent item must be handled first, such as function calls, undo history or depth-first search.
When do I use a queue?
When items must be processed in arrival order, such as tasks, requests or messages.
How is Stacks and queues used in practice?
Typical applications include implementing undo in an editor with a stack, processing print jobs in the order they arrive with a queue and checking balanced brackets in an expression with a stack.
What should I learn before Stacks and queues?
Study "Sorting algorithms" first (topic 18 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Stacks and queues?
O(1) operations. Call stack. Producer-consumer queues.
Can you give an example of Stacks and queues?
Balanced-parentheses checking uses a stack.
What comes after Stacks and queues?
The next topic is "Linked lists", which builds directly on this one.
How difficult is Stacks and queues?
It is classed as Beginner in Software & Computing (topic 19 of 104). It is an entry-level topic. It relies only on basic school mathematics and the ideas introduced earlier in this discipline.