linear structures
Linear lists are built two ways, with an array and with pointers, then stacks and queues are restricted versions of that list That sentence reorganised how I think about these tools
They are not three unrelated ideas They are one idea with different rules about where you may touch it
The array
#include <vector>
std::vector<int> ports{22, 80, 443};
ports.push_back(8080);
int value = ports[2];
An array gives every element a position Indexing is constant time The price is that inserting in the middle moves everything after the hole
Array and linked representations are compared with experimental data That comparison is the point There is no universally better representation, only a better one for a set of operations
When the array wins
- you need random access
- you iterate far more than you insert
- cache behaviour matters, contiguous memory is friendly
- you know the size or tolerate growth
When it loses
- you insert and delete in the middle constantly
- you remove from the front repeatedly
- you need stable references to elements across growth
std::vector::erase(begin()) shifts every element
That call has turned more scripts from fast to slow than any other line I know
The vector does not warn you
It just does exactly what you asked, in linear time
The stack
A stack is defined by restricting insertion and removal to the same end Last in, first out
#include <stack>
std::stack<std::string> opens;
opens.push("file");
opens.push("record");
std::string last = opens.top();
opens.pop();
Output value:
record
Six applications: parenthesis matching Towers of Hanoi railroad shunting switch box routing equivalence classes rat in a maze
Those are not museum pieces Parenthesis matching is nested markup Maze backtracking is directory traversal A stack is how you remember where you came from
A real parenthesis check
bool balanced(const std::string &text) {
std::stack<char> opens;
for (char ch : text) {
if (ch == '(' || ch == '[' || ch == '{') opens.push(ch);
else if (ch == ')' || ch == ']' || ch == '}') {
if (opens.empty()) return false;
char open = opens.top();
opens.pop();
if (!((ch == ')' && open == '(') ||
(ch == ']' && open == '[') ||
(ch == '}' && open == '{'))) return false;
}
}
return opens.empty();
}
Run it on "([{}])" and "([)]"
The second fails
That failure is the reason a stack exists
The queue
Queues are covered in the next chapter First in, first out
#include <queue>
std::queue<std::string> work;
work.push("alpha");
work.push("beta");
std::string next = work.front();
work.pop();
Output value:
alpha
Use std::queue for fair scheduling
Use std::deque when you need both ends
Use a priority queue when “fair” is not the rule
Where queues appear
- breadth first traversal
- crawlers and scanners
- buffering between producer and consumer
- job pipelines with rate limits
Choosing
| Need | Structure |
|---|---|
| indexed access | vector |
| undo or nested parsing | stack |
| fair scheduling | queue |
| priority scheduling | priority_queue |
| both ends | deque |
Mistakes
- using a stack when arrival order matters
- using a queue when you need the most recent item
- erasing from the front of a vector in a loop and calling it fine
- forgetting a stack can grow without bound on malformed input
Exercises
- Write
balancedfrom memory and test an empty string - Explain why Towers of Hanoi needs a stack and not a queue
- Build a work queue for hosts and process them in order
- Find one place in a tool you use where a queue would prevent a race
What to remember
A stack remembers the last thing you saw A queue remembers the first thing you saw A vector remembers where everything is Pick the memory that matches the question
Posted