Complete DSA Notes in C++
Complete DSA Notes in C++ (Topic-wise)
Complete DSA Notes in C++ (Beginner to Advanced)
1. C++ Basics
-------------
- Input/Output:
int x; cin >> x; cout << x;
- Loops & Conditions:
for(int i=0; i<5; i++) { ... }
if(x > 0) { ... } else { ... }
- Functions:
int sum(int a, int b) { return a + b; }
2. Arrays
---------
- Declare: int arr[5] = {1, 2, 3, 4, 5};
- Loop: for(int i=0; i<5; i++) cout << arr[i];
- Reverse Array: for(int i=0, j=n-1; i<j; i++, j--) swap(arr[i], arr[j]);
3. Strings
----------
- Declare: string s = "hello";
- Operations: s.length(), s.substr(0,2), s.find("lo");
4. Vectors (STL)
----------------
- vector<int> v;
v.push_back(10);
for(auto x : v) cout << x << " ";
Complete DSA Notes in C++
5. Sorting & Searching
----------------------
- sort(arr, arr+n);
- binary_search(arr, arr+n, key);
6. Linked List
--------------
struct Node {
int data;
Node* next;
};
- Reverse:
Node* reverse(Node* head) {
Node* prev = NULL;
while(head) {
Node* next = head->next;
head->next = prev;
prev = head;
head = next;
return prev;
7. Stack & Queue
----------------
- stack<int> s; s.push(10); s.top(); s.pop();
- queue<int> q; q.push(10); q.front(); q.pop();
8. Trees
--------
Complete DSA Notes in C++
struct Node {
int data;
Node *left, *right;
};
void inorder(Node* root) {
if(!root) return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
9. Binary Search Tree (BST)
---------------------------
- Insert:
Node* insert(Node* root, int val) {
if(!root) return new Node(val);
if(val < root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
10. Recursion & Backtracking
----------------------------
- Factorial:
int fact(int n) {
if(n <= 1) return 1;
return n * fact(n - 1);
- Backtracking: N-Queens, Subsets, Maze Path
11. Graphs
Complete DSA Notes in C++
----------
vector<int> adj[100];
bool visited[100];
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = true;
while(!q.empty()) {
int node = q.front(); q.pop();
for(int neigh : adj[node]) {
if(!visited[neigh]) {
visited[neigh] = true;
q.push(neigh);
12. Heaps & Priority Queue
--------------------------
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
13. Greedy Algorithms
---------------------
- Activity Selection:
Sort by end time, pick earliest finishing activity.
14. Dynamic Programming (DP)
----------------------------
- Fibonacci (Memoization):
Complete DSA Notes in C++
int dp[100];
int fib(int n) {
if(n <= 1) return n;
if(dp[n] != -1) return dp[n];
return dp[n] = fib(n-1) + fib(n-2);
15. Bit Manipulation
--------------------
- Check ith bit: if(n & (1 << i)) { ... }
- Set bit: n = n | (1 << i);
- Count bits: __builtin_popcount(n);
Tips:
- Use STL wisely: vector, map, set, stack, queue
- Practice on LeetCode, GFG, Codeforces
- Focus on clean logic and edge cases