C++_and_Data_Structure_Study_Material_Updated
C++_and_Data_Structure_Study_Material_Updated
public:
int rollNo;
void display() {
.UNIT 1 - Detailed Topic Breakdown cout << "Roll No: " << rollNo;
}
Tokens, Keywords, Identifiers, Variables };
Tokens are the smallest units in a C++ program. They include:
- Keywords: Reserved words like int, float, class. ✅ Object
- Identifiers: User-defined names like age, main.
- Constants: Fixed values like 10, 3.14.
- Operators: Symbols like +, -, *, /. Instance of a class.
- Punctuators: Symbols like ;, {}, ().
cpp
Variables are named storage used to hold data, declared using CopyEdit
types such as int, float, etc. Student s1;
Example: int age = 21; s1.rollNo = 101;
s1.display(); // Output: Roll No: 101
Example:
✅ Constructor
#include <iomanip>
cout << setw(10) << setprecision(2) << 3.14159;
Special function that initializes objects.
Expressions and Control Structures Same name as class.
Expressions combine variables, constants, and operators to No return type (not even void).
produce a value. Invoked automatically.
Control structures include:
- Conditional: if, if-else, switch
cpp
- Looping: for, while, do-while
CopyEdit
- Branching: break, continue, return class Book {
public:
Example: string title;
if (a > b) { cout << "a is greater"; } Book(string t) {
title = t;
}
Functions in C++ };
Functions modularize code. The main() function is the entry point. Book b1("C++ Basics");
✅ Operator Overloading
✅ Virtual Functions
✅ Standard Streams
cpp
CopyEdit
ifstream fin;
cin → standard input
fin.open("input.txt");
cout → standard output if (!fin) {
cerr → unbuffered error cout << "File not found!";
}
clog → buffered error
fin.close();
cpp
📊 Diagrams CopyEdit
while (!fin.eof()) {
string line;
🔸 Types of Inheritance (Example: Multilevel) getline(fin, line);
cout << line << endl;
}
plaintext
CopyEdit
Class A
↑
Class B ✅ File Pointers & Their Manipulation
↑
Class C
seekg() – move get pointer
seekp() – move put pointer
🔸 Polymorphism (Virtual Function Dispatch)
tellg() – get current get pointer position
tellp() – get current put pointer position
plaintext
CopyEdit
Base* ptr → [ Derived Object ] cpp
CopyEdit
|→ virtual table → fin.seekg(0, ios::end);
int length = fin.tellg();
Derived::show()UNIT 3 – File Handling & Basic Data
Structures
cpp
CopyEdit
int sortedArr[] = {10, 20, 30, 40};
🔹 3. Asymptotic Notations
📊 Diagrams
✅ Types of Notation
🔸 Time Complexity Graphs
cpp
CopyEdit
int factorial(int n) {
if (n == 0) return 1;
📘 UNIT 4 – Stacks, Queues & Linked Lists
return n * factorial(n - 1);
}
🔹 1. Stacks 🔹 2. Queues
✅ Definition ✅ Definition
A LIFO (Last In, First Out) data structure. A FIFO (First In, First Out) data structure.
Access is only at one end: top of the stack. Insertion at rear, deletion from front.
✅ Applications
Job scheduling
CPU & printer queues
Breadth-first search
Reversing strings
Expression evaluation
Recursion
Infix to Postfix conversion
Maze solving / Backtracking ✅ Circular Queue
cpp
✅ Infix to Postfix Conversion
CopyEdit
rear = (rear + 1) % size;
Convert: A + B * C → Postfix: A B C * +
✅ Operations
✅ Applications
Insertion (beginning, middle, end)
Deletion (by value or position)
Traversal Browser forward/back buttons
Music playlist navigation
cpp Undo/redo functionality
CopyEdit
Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
📊 Diagrams
✅ Applications
🔸 Stack (LIFO)
Dynamic memory use
Implementing stack/queue plaintext
CopyEdit
Polynomial representation | 40 | <- top
| 30 |
| 20 |
|____|
For addition:
🔸 Doubly Linked List
✅ Structure
🔹 1. Binary Trees
cpp
CopyEdit
struct Node {
int data;
Node* prev; ✅ Definition
Node* next;
};
A tree structure where each node has at most two
children: left and right.
The top node is the root. 2. Take all subsequent siblings as the right child of the
previous one.
plaintext
CopyEdit
Forest:
✅ Node Structure A B
/ \ \
C D E
cpp
CopyEdit
Binary Tree:
struct Node {
A
int data;
/
Node* left;
C
Node* right;
\
};
D
✅ Tree Traversals
🔹 3. Graphs
Type Order
Inorder Left → Root → Right
Preorder Root → Left → Right
Postorder Left → Right → Root ✅ Definition
cpp
CopyEdit
void inorder(Node* root) { A graph is a collection of vertices (nodes) and edges
if (root) { (connections).
inorder(root->left);
cout << root->data << " ";
inorder(root->right); ✅ Types
}
}
Type Description
Directed Edges have direction
Undirected No direction in edges
✅ Example Tree Structure Weighted Edges have weights
Unweighted All edges are equal
plaintext
CopyEdit
10
/ \ ✅ Representations
5 15
1. Adjacency Matrix
Inorder: 5 10 15
Preorder: 10 5 15
cpp
Postorder: 5 15 10
CopyEdit
int adj[5][5]; // 2D array
2. Adjacency List
🔹 4. Graph Traversals
To convert into a binary tree:
✅ Steps
cpp
CopyEdit
1. Take the first child as the left child.
void DFS(int v, vector<bool>& visited, pq.push({dist[v], v});
vector<int> graph[]) { }
visited[v] = true; }
for (int u : graph[v]) }
if (!visited[u]) }
DFS(u, visited, graph);
}
🔹 6. Hashing
Uses queue.
✅ Definition
cpp
CopyEdit
void BFS(int start, vector<int> graph[]) {
Technique to map data (key) to a fixed size table
(index) using a hash function.
queue<int> q;
vector<bool> visited(10, false);
q.push(start);
visited[start] = true;
while (!q.empty()) {
int v = q.front(); q.pop(); ✅ Hash Function Example
for (int u : graph[v]) {
if (!visited[u]) { cpp
visited[u] = true; CopyEdit
q.push(u); int hashFunction(int key, int size) {
} return key % size;
} }
}
}
✅ Collision Handling
🔹 5. Dijkstra’s Algorithm – Shortest Path
Method Description
Linear Probing Search next available spot
Chaining Use linked list at each index
✅ Purpose
✅ Chaining Example
Finds shortest paths from a source vertex to all other
vertices in a weighted graph with non-negative edges.
cpp
CopyEdit
vector<int> hashTable[10];
cpp 📊 Diagrams
CopyEdit
void dijkstra(int V, int src, vector<pair<int,
int>> graph[]) { 🔸 Binary Tree Traversals
vector<int> dist(V, INT_MAX);
priority_queue<pair<int, int>,
plaintext
vector<pair<int, int>>, greater<>> pq;
CopyEdit
A
dist[src] = 0;
/ \
pq.push({0, src});
B C
Inorder: B A C
while (!pq.empty()) {
Preorder: A B C
int u = pq.top().second;
Postorder: B C A
pq.pop();
plaintext
CopyEdit
Index | Elements
------|---------
0 |
1 | 11 → 21
2 | 12 → 22