Definition

Linear data structure storing elements in contiguous memory, accessed via index.

Why Useful
  • Constant-time random access: O(1)
  • Cache-friendly: Improves performance
Software Applications
  • Image Processing: Images stored as 2D arrays (pixels)
  • Databases: Fixed indexing tables
  • Operating Systems: Page tables in memory management
Real-world Analogy

Apartment building with numbered flats - you can go directly to Flat #203 without checking others.

C++
// Array - Static & Dynamic #include <iostream> #include <vector> using namespace std; int main() { // Static array int arr[5] = {10, 20, 30, 40, 50}; // Dynamic array (vector) vector<int> vec = {1, 2, 3, 4, 5}; // Access - O(1) cout << "arr[2]: " << arr[2] << endl; cout << "vec[2]: " << vec[2] << endl; return 0; }
Definition

Sequence of nodes where each stores data and pointer to next node.

Why Useful
  • Efficient insertions/deletions: O(1) compared to arrays
  • Dynamic size: No need to know size in advance
Applications
  • Music Playlists: Next/Previous navigation
  • Undo/Redo: Action history
  • Memory Management: Free block lists
Real-world Analogy

Train coaches - easy to attach/detach without shifting all others.

C++
// Linked List - Node Structure #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; int main() { // Create nodes Node* head = new Node(10); head->next = new Node(20); head->next->next = new Node(30); // Traverse Node* temp = head; while (temp != nullptr) { cout << temp->data << " -> "; temp = temp->next; } cout << "NULL" << endl; return 0; }
Principle

LIFO - Last In First Out

Why Useful
  • Natural for "last done, first undone" problems
  • Easy backtracking
Applications
  • Function Calls: Call stack in programming
  • Browser: Back button history
  • Compilers: Expression evaluation
Real-world Analogy

Stack of plates - last plate placed is first removed.

C++
// Stack - LIFO Structure #include <iostream> #include <stack> using namespace std; int main() { stack<int> st; // Push elements st.push(10); st.push(20); st.push(30); // Top element cout << "Top: " << st.top() << endl; // Pop element st.pop(); cout << "After pop: " << st.top() << endl; // Size cout << "Size: " << st.size() << endl; return 0; }
Principle

FIFO - First In First Out

Why Useful
  • Ensures fairness (first come, first served)
Applications
  • CPU Scheduling: Process queue in OS
  • Printers: Print job queue
  • Networking: Packet transmission order
Real-world Analogy

Queue at ticket counter - first person in line is served first.

C++
// Queue - FIFO Structure #include <iostream> #include <queue> using namespace std; int main() { queue<int> q; // Enqueue elements q.push(10); q.push(20); q.push(30); // Front element cout << "Front: " << q.front() << endl; // Dequeue element q.pop(); cout << "After pop: " << q.front() << endl; // Size cout << "Size: " << q.size() << endl; return 0; }
Definition

Hierarchical structure with root and children nodes.

Why Useful
  • Efficient search/insert/delete: O(log n) in balanced trees
  • Perfect for hierarchical data
Applications
  • File Systems: Directories and subdirectories
  • Databases: B-Trees for indexing
  • HTML DOM: Web page structure
Real-world Analogy

Family tree - grandparents → parents → children.

C++
// Binary Tree - Node Structure #include <iostream> using namespace std; struct TreeNode { int data; TreeNode* left; TreeNode* right; TreeNode(int val) : data(val), left(nullptr), right(nullptr) {} }; int main() { // Create tree TreeNode* root = new TreeNode(10); root->left = new TreeNode(5); root->right = new TreeNode(15); root->left->left = new TreeNode(3); root->left->right = new TreeNode(7); cout << "Root: " << root->data << endl; cout << "Left child: " << root->left->data << endl; return 0; }
Definition

Nodes (vertices) connected by edges.

Why Useful
  • Models relationships and networks
  • Algorithms for shortest path, connectivity
Applications
  • Google Maps: Shortest path algorithms
  • Social Networks: Friend connections
  • Networking: Router connections
Real-world Analogy

City map - intersections are nodes, roads are edges.

C++
// Graph - Adjacency List #include <iostream> #include <vector> using namespace std; int main() { int V = 5; // Number of vertices vector<vector<int>> adj(V); // Add edges (undirected graph) adj[0].push_back(1); adj[1].push_back(0); adj[0].push_back(2); adj[2].push_back(0); adj[1].push_back(3); adj[3].push_back(1); // Print adjacency list for (int i = 0; i < V; i++) { cout << i << " -> "; for (int j : adj[i]) cout << j << " "; cout << endl; } return 0; }
Definition

Key-value pairs with O(1) average lookup.

Why Useful
  • O(1) average time: For search, insert, delete
  • Perfect for caching and quick lookups
Applications
  • Caching: Fast data retrieval
  • Databases: Quick record lookup
  • Compilers: Symbol tables
Real-world Analogy

Dictionary - word (key) maps to definition (value).

C++
// HashMap - unordered_map #include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<string, int> map; // Insert key-value pairs map["Alice"] = 25; map["Bob"] = 30; map["Charlie"] = 35; // Access - O(1) cout << "Alice's age: " << map["Alice"] << endl; // Check existence if (map.find("Bob") != map.end()) cout << "Bob found!" << endl; // Size cout << "Size: " << map.size() << endl; return 0; }
Arrays
  • Video frames storage
  • Comments list
  • Thumbnail rendering
Linked Lists
  • Playlists (next/previous)
  • Watch history navigation
Stack
  • Undo/Redo in video editor
  • Back button navigation
Queue
  • Video upload processing
  • Frame buffering
Trees
  • Category/subcategory structure
  • Comment threads (nested replies)
Graphs
  • Recommendation system
  • Social connections
HashMap
  • Video metadata caching
  • User session management
Heaps
  • Trending videos (priority)
  • Live stream packet management