Chapters List
- A Mental Model for Data Structures and Algorithms
- Why Data Structures Exist
- Measuring Efficiency: Time and Space
- Arrays: The Foundation of Data Storage
- Linked Structures
- Stacks and Queues: Controlling Order
- Hash Tables: Fast Lookup
- Trees: Hierarchical Data
- Graphs: Modeling Relationships
- Common Algorithm Techniques
- Sorting and Searching Algorithms
- Data Structures in Modern Systems
data structures and algorithms
Stacks and Queues: Controlling Order
Learn how LIFO and FIFO structures shape processing order in real software systems.
In the previous chapter we looked at linked structures.
Now we explore two classic abstract data types that control processing order.
Stack: Last In, First Out
A stack follows LIFO.
The most recently added item is removed first.
Core operations:
pushadd itempopremove latest itempeekinspect latest item
Common use cases:
- function call stack
- undo/redo history
- expression evaluation
Queue: First In, First Out
A queue follows FIFO.
The earliest added item is removed first.
Core operations:
enqueueadd itemdequeueremove earliest itemfrontinspect earliest item
Common use cases:
- task scheduling
- request buffering
- print job handling
Visual Model
graph TD
A[Stack LIFO] --> B[Newest leaves first]
C[Queue FIFO] --> D[Oldest leaves first]
These structures are simple but extremely important across operating systems and backend services.
Implementation Note
Both stacks and queues can be implemented using arrays or linked structures.
The main concept is not implementation detail, but operation order semantics.
Key Ideas to Remember
- Stacks enforce LIFO order.
- Queues enforce FIFO order.
- Both are abstract structures with common core operations.
- They appear in call management, scheduling, and buffering.
- Order constraints make many systems predictable and manageable.
What Comes Next
Stacks and queues manage order.
Next we learn a structure optimized for fast key-based access:
hash tables and hashing.