In this chapter you will learn three fundamental data structures: Stacks (LIFO), Queues (FIFO) and Heaps (priority queues), with diagrams, time complexity and Python implementations.
What you will learn
list, deque and heapqA stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last element added is the first one removed – like a stack of plates.
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last element added is the first one removed – like a stack of plates.
| Operation | Description | Time |
|---|---|---|
push(x) | Insert item onto the top of the stack | O(1) |
pop() | Remove and return top item | O(1) |
peek() | Return top item without removing it | O(1) |
is_empty() | Check if stack has no elements | O(1) |
Python's built-in list can be used as a stack with
append() and pop() methods.
Here is a simple implementation of a Stack class in Python:
Stacks are widely used in various applications, such as:
A queue is a linear data structure that follows FIFO (First In, First Out). The first element added is the first one removed – like a line of people waiting for tickets.
A queue is a linear data structure that follows FIFO (First In, First Out). The first element added is the first one removed – like a line of people waiting for tickets.
The main operations for a queue are:
| Operation | Description | Time |
|---|---|---|
enqueue(x) | Insert item at the back of the queue | O(1) |
dequeue() | Remove and return front item | O(1) with deque |
peek() | View the front item without removing it | O(1) |
is_empty() | Check if queue has no elements | O(1) |
pop(0)?
If you implement a queue as a list and use pop(0) to dequeue, each removal
shifts all remaining elements one step – this is O(n).
Instead, Python provides collections.deque, which is optimized for fast
appends and pops from both ends.
deque
Here is how to use deque for a queue:
Here is a simple implementation of a Queue class using deque:
Queues are commonly used in various applications, such as:
A heap is a specialized tree-based data structure that satisfies the heap property. It is commonly used to implement priority queues, where elements are ordered based on priority rather than insertion order.
A heap is a special tree-based data structure that satisfies the heap property and is always a complete binary tree (all levels filled from left to right).
Two main types:
Here is an example of a min-heap:
Min-heap property: every parent node is ≤ its children. The smallest value is always at the top.
heapq Module
Python does not have a dedicated Heap class, but the
heapq module provides a very efficient min-heap implementation
built on top of a list.
heapq
Here are the main operations using the heapq module:
You can convert an existing list into a heap in-place using heapq.heapify():
Python's heapq supports only a min-heap, but we can simulate a max-heap by
pushing negative values.
A common use of a heap is to implement a priority queue.
Output:
0 High priority
1 Low priority
2 Very low priority
The time complexities for key heap operations are:
| Operation | Description | Time |
|---|---|---|
heappush | Insert element | O(log n) |
heappop | Remove smallest element | O(log n) |
heapify | Build heap from list | O(n) |
Heaps are widely used in various applications, such as:
Here is a summary comparison of stacks, queues and heaps:
| Feature | Stack | Queue | Heap |
|---|---|---|---|
| Order | LIFO (Last In, First Out) | FIFO (First In, First Out) | Priority (min or max element first) |
| Python Implementation | list (append, pop) |
collections.deque |
heapq module |
| Main Use Cases | Undo, recursion, DFS | Scheduling, BFS, messaging | Priority queues, shortest paths |
| Insertion | append(): O(1) |
append(): O(1) |
heappush(): O(log n) |
| Removal | pop(): O(1) |
popleft(): O(1) |
heappop(): O(log n) |
Here are guidelines on when to choose each data structure: