Data Structures in C++ are a fundamental concept in computer science. They define how data is organized, stored, and manipulated efficiently. To understand them deeply, we should cover:
- Concept of Data Structures
- Classification
- Detailed explanation of each structure
- Memory representation
- Operations
- Time complexity
- Practical usage in systems
I will explain step-by-step. ๐
1๏ธโฃ What is a Data Structure
A data structure is a way of organizing and storing data so it can be accessed and modified efficiently.
Example:
If you store 1 million numbers:
- Searching randomly โ slow
- Organizing them โ faster operations
Example real life:
| Real world | Data structure |
|---|---|
| Phone contacts | Map |
| Stack of plates | Stack |
| Queue at bank | Queue |
| Road network | Graph |
| Folder system | Tree |
2๏ธโฃ Classification of Data Structures
Primitive Data Structures
Basic built-in types:
intfloatchardoublebool
Example:
int age = 25;
float salary = 5000.5;
Non-Primitive Data Structures
Two types:
Linear Data Structures
Data arranged sequentially
Examples:
- Array
- Linked List
- Stack
- Queue
- Vector
- Deque
Structure example:
10 โ 20 โ 30 โ 40
Non-Linear Data Structures
Data arranged hierarchically or graph-like
Examples:
- Tree
- Graph
- Heap
- Trie
Example:
A
/ \
B C
3๏ธโฃ Memory Representation
Contiguous Memory
Elements stored next to each other
Example:
Array
Address
100 โ 10
104 โ 20
108 โ 30
Advantages:
- Fast indexing
- Cache friendly
Used by:
- Arrays
- Vectors
Non-Contiguous Memory
Nodes stored anywhere in memory.
Node1 โ Node2 โ Node3
Used by:
- Linked lists
- Trees
- Graphs
4๏ธโฃ Arrays (Detailed)
Array = collection of elements stored in contiguous memory
Declaration
int arr[5];
Memory representation:
arr[0] arr[1] arr[2] arr[3] arr[4]
Address formula:
Address = base + (index ร element_size)
Advantages
- Fast random access
- Simple structure
Disadvantages
- Fixed size
- Costly insertion
Example:
#include <iostream>
using namespace std;int main() {
int arr[5] = {1,2,3,4,5}; for(int i=0;i<5;i++)
cout<<arr[i]<<" ";
}
Time complexity:
| Operation | Time |
|---|---|
| Access | O(1) |
| Search | O(n) |
| Insert | O(n) |
| Delete | O(n) |
5๏ธโฃ Linked List (Deep Explanation)
A linked list contains nodes.
Each node contains:
Data + Pointer
Structure:
struct Node {
int data;
Node* next;
};
Example:
10 โ 20 โ 30 โ NULL
Advantages:
- Dynamic size
- Efficient insert/delete
Disadvantages:
- Extra memory for pointers
- Slow random access
Example:
#include <iostream>
using namespace std;struct Node {
int data;
Node* next;
};int main() { Node* head = new Node{10,NULL};
head->next = new Node{20,NULL}; cout<<head->data<<" ";
cout<<head->next->data;
}
Types:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
6๏ธโฃ Stack (LIFO)
Last In First Out
Example:
Push 10
Push 20
Push 30Pop โ 30
Operations
- push()
- pop()
- top()
Example:
#include <stack>
#include <iostream>
using namespace std;int main() { stack<int> s; s.push(10);
s.push(20); cout<<s.top();
}
Applications:
- Function calls
- Expression evaluation
- Undo operations
- Backtracking
7๏ธโฃ Queue (FIFO)
First In First Out
Example:
10 โ 20 โ 30
Operations
- push()
- pop()
- front()
Example:
#include <queue>
#include <iostream>
using namespace std;int main() { queue<int> q; q.push(10);
q.push(20); cout<<q.front();
}
Applications:
- CPU scheduling
- Printer queue
- Network packets
8๏ธโฃ Trees
Tree = hierarchical structure.
Example:
10
/ \
5 20
Terminology:
| Term | Meaning |
|---|---|
| Root | top node |
| Leaf | node with no children |
| Parent | node above |
| Child | node below |
Types:
- Binary Tree
- Binary Search Tree
- AVL Tree
- Red Black Tree
- Heap
Example node:
struct Node { int data;
Node* left;
Node* right;
};
Applications:
- Databases
- File systems
- Search engines
9๏ธโฃ Graph
Graph = collection of nodes and edges.
Example:
A โ B
| |
C โ D
Graph representation:
Adjacency List
vector<int> graph[5];
Adjacency Matrix
A B C
A 0 1 1
B 1 0 0
C 1 0 0
Applications:
- Social networks
- GPS navigation
- Network routing
๐ Hash Tables
Stores key โ value pairs
Example:
Key โ Value
ID โ Name
C++ container:
unordered_map<int,string> m;
Example:
#include <unordered_map>
#include <iostream>
using namespace std;int main() { unordered_map<int,string> m; m[1] = "Venkat"; cout<<m[1];
}
Time complexity:
Average โ O(1)
Worst โ O(n)
Time Complexity Overview
| Structure | Access | Insert | Delete |
|---|---|---|---|
| Array | O(1) | O(n) | O(n) |
| Vector | O(1) | O(1) | O(n) |
| Linked List | O(n) | O(1) | O(1) |
| Stack | O(1) | O(1) | O(1) |
| Queue | O(1) | O(1) | O(1) |
| Tree | O(log n) | O(log n) | O(log n) |
| Hash Table | O(1) | O(1) | O(1) |
Real Systems Using Data Structures
| System | Data Structure |
|---|---|
| Web browsers | Stack |
| Operating systems | Queue |
| Routing systems | Graph |
| Database indexes | B-tree |
| Compiler | Tree |
โ Important Data Structures Every C++ Developer Should Master
- Array
- Vector
- Linked List
- Stack
- Queue
- Tree
- Heap
- Graph
- Hash Table
- Trie