Chapter 5b - Data Structures
Why Data Structures?β
A data structure is a way of organizing data in memory so that operations on it (insert, delete, search) are efficient. Choosing the right one is one of the most important decisions in software engineering.
| Structure | Access | Insert/Delete | Search | Use when |
|---|---|---|---|---|
Array / vector | O(1) | O(n) (middle) | O(n) | Random access, cache-friendly |
| Linked List | O(n) | O(1) (at known node) | O(n) | Frequent insert/delete, unknown size |
| Stack | O(1) top | O(1) | β | LIFO: undo, call stack, DFS |
| Queue | O(1) front | O(1) | β | FIFO: task queue, BFS |
Map (unordered_map) | O(1) avg | O(1) avg | O(1) avg | Key-value lookup |
Arrays (Fixed Size)β
C-style arrays have a fixed size set at compile time.
int scores[5] = {90, 85, 78, 92, 88};
cout << scores[2] << endl; // 78
for (int i = 0; i < 5; i++) {
cout << scores[i] << " ";
}
Mini quiz β arraysβ
Why structures, and arrays
1 / 3A data structure is mainlyβ¦
Pick an answer to see if you are right.
vector β Dynamic Array (STL)β
std::vector is a resizable array. Prefer it over raw arrays almost always.
#include <vector>
using namespace std;
vector<int> nums = {1, 2, 3};
nums.push_back(4); // append
nums.push_back(5);
cout << nums.size() << endl; // 5
cout << nums[2] << endl; // 3
Iterating a Vectorβ
for (int n : nums) {
cout << n << " ";
}
π§ͺ Vector operations
Linked List (Manual Implementation)β
A linked list is a chain of nodes, each holding data and a pointer to the next node.
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
π§ͺ Linked list
Mini quiz β vector and listsβ
vector and linked lists
1 / 3std::vector canβ¦
Pick an answer to see if you are right.
Stackβ
A stack is LIFO (Last In, First Out) β like a stack of plates. Use std::stack from <stack>.
#include <stack>
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << s.top() << endl; // 3
s.pop();
Application: Balanced Parenthesesβ
π§ͺ Balanced parentheses
Challenge: balanced parensβ
Finish isBalanced so it returns yes if every ( ) pair matches, otherwise no. Only round parentheses are tested.
| Input | Return |
|---|---|
() | yes |
(() | no |
Queueβ
A queue is FIFO (First In, First Out) β like a line of people. Use std::queue from <queue>.
#include <queue>
queue<string> q;
q.push("Alice");
q.push("Bob");
cout << q.front() << endl; // Alice
q.pop();
Application: Print Queue Simulationβ
π§ͺ Print queue
Map (Hash Map / Dictionary)β
std::unordered_map stores key-value pairs with O(1) average access.
#include <unordered_map>
unordered_map<string, int> ages;
ages["Alice"] = 22;
ages["Bob"] = 19;
cout << ages["Alice"] << endl; // 22
Word Frequency Counterβ
π§ͺ Word frequency
Mini quiz β queue and mapβ
Queue and map
1 / 3A queue isβ¦
Pick an answer to see if you are right.
set β Unique Sorted Elementsβ
#include <set>
set<int> s = {3, 1, 4, 1, 5, 9, 2, 6, 5};
for (int x : s) cout << x << " ";
// Output: 1 2 3 4 5 6 9
π§ͺ set
Choosing a Containerβ
Do you need key-value pairs?
YES β unordered_map (fast) or map (sorted)
NO β
Do you need unique elements?
YES β unordered_set or set
NO β
Do you need LIFO? β stack
Do you need FIFO? β queue
Otherwise β vector (default choice)
Mini quiz β set and choosing containersβ
set and container choice
1 / 3A set storesβ¦
Pick an answer to see if you are right.
Exercisesβ
Without using std::stack, implement a Stack<int> class backed by a linked list with push, pop, top, and isEmpty methods.