C++ Unit-2 Notes
C++ Unit-2 Notes
C++ (24CSH-103)
Unit 2 : Elementary Data Structures
Unit 2 Syllabus ::
Compiled by : Subhayu
Chapter 1: Introduction to Data
Structure
1. Concept of Data and Information
📌 What is Data?
● Data refers to raw facts and figures without meaning.
● Examples: "John", 23, "Delhi", 3.14, etc.
📌 What is Information?
● Infor mation is processed data that has meaning and context.
● Example: "John is 23 years old and li ves in Delhi."
Key Difference:
Feature Data Information
Definition Ra w facts Processed data
student = 87.6"
maps)
Operation Description
Insertion Adding a ne w element
int main() {
retur n 0;
5. Algorithm Complexity
📌 What is Algorithm Complexity?
● Complexity measures the efficiency of an algorithm in ter ms of time and
space.
● Helps in selecting the best approach for a problem.
📌 Types of Complexity
1️⃣ Time Complexity
● Time taken by an algorithm to run as input size inc reases.
Solution:
#include <iostream>
retur n ma x;
int main() {
cout << "Ma ximum element: " << findMa x(ar r, n) << endl;
retur n 0;
}
Output:
Ma ximum element: 89
Summary:
✅ Data = Raw facts, Information = Processed data
✅ Data Structures = Organized way to store and manipulate data
✅ Types: Linear (Array, Stack, Queue) & Non-Linear (Tree, Graph, Hash
Table)
✅ Operations: Insertion, Deletion, Tra versal, Searching, Sorting
✅ Algorithm Complexity: Time (O(1) to O(2ⁿ)) & Space Complexity
Importance:
● Improves performance in searching, sorting, and data manipulation.
● Helps in efficient memory utilization and a voids redundancy.
6️⃣ What is the difference between Abstract Data Type (ADT) and
Data Structure?
Answer:
● Abstract Data Type (ADT) → A conceptual model that defines how data is
stored and operated on.
7️⃣ Explain the significance of choosing the right Data Structure for
a problem.
Answer:
● The choice of data structure affects performance, memory usage, and
execution speed.
● Example:
Example:
● ar r[3] → 2012
3️⃣ Traversing an Array
Definition: Visiting each element of the ar ray sequentially.
Example (Traversing an array in C++):
#include <iostream>
int main() {
retur n 0;
int main() {
ar r[pos] = value;
retur n 0;
int main() {
int pos = 2;
retur n 0;
}
5️⃣ Searching in Arrays
1. Linear Search (Sequential Search)
● Checks elements one by one.
● Best-case: O(1), Worst-case: O(n).
● Works for unsorted data.
retur n -1;
}
6️⃣ Sorting in Arrays
1. Bubble Sort (Repeated swapping)
● Time Complexity: O(n²)
void bubbleSort(int ar r[], int n) {
int j = i - 1;
ar r[j+1] = ar r[j];
j--;
ar r[j+1] = key;
int minIndex = i;
minIndex = j;
Traversing a 2D Array
for(int i = 0; i < 3; i++) {
Operations on 2D Arrays
1. Addition of Matrices
2. Multiplication of Matrices
3. Transpose of a Matrix
Sort) Sort)
contiguously in memory.
Advantages:
● Efficient indexing: Fast access using index (O(1) time complexity).
● Memory locality: Elements are stored in consecuti ve memory locations,
improving cache efficiency.
Data Type)
For example, if ar r[0] is stored at 2000 and int occupies 4 bytes, then:
● ar r[1] → 2004
● ar r[2] → 2008
data data
scanning
● Insertion Sort: Picks elements one by one and inserts them in the cor rect
position (O(n²)).
● Selection Sort: Finds the minimum element and swaps it with the first
unsorted element (O(n²)).
Declaration:
int matri x[3][3]; // 3x3 matri x
Initialization:
int matri x[2][2] = {{1, 2}, {3, 4}};
❌ Disadvantages:
● Fi xed size (wasted memory if not fully used).
int main() {
retur n 0;
}
2️⃣ Implement a program for Linear Search in C++.
int linearSearch(int ar r[], int n, int key) {
retur n -1;
retur n -1;
}
}
int j = i - 1;
ar r[j+1] = ar r[j];
j--;
ar r[j+1] = key;
int minIndex = i;
minIndex = j;
}
7️⃣ Implement Matrix Addition for 2D Arrays in C++.
#include <iostream>
int main() {
retur n 0;
}
Chapter 3: Pointers - Detailed Notes
📌 Introduction to Pointers
A pointer is a variable that stores the memory address of another variable.
Instead of holding an actual value, it stores a reference to where the value is
stored in memory.
graphs.
💡 The * operator is used to dereference a pointer (get the value stored at that
memory address).
📌 Pointer Arithmetic
💡 Pointer arithmetic allows operations on memory addresses.
Operatio Meaning
n
ptr++ Moves to the next memory location
Example:
int *ptr = ar r;
*ptr = 50;
Deallocating Memory
delete ptr; // Frees the allocated memory
int main() {
int x = 10;
modify(&x);
Example:
int x = 10;
int data;
Node* next;
};
📌 Summary
✔Pointers store memory addresses and allow direct memory manipulation.
✔ Pointer arithmetic helps in navigation through memory.
Ans: Pointers are c rucial for dynamic memory allocation, efficient data
structure implementation, function optimization, and low-level memory
access.
What is pointer dereferencing?
3️⃣
Ans: Dereferencing a pointer means accessing the value stored at the memory
address it points to. This is done using the * operator.
👉 Example:
int x = 10;
👉 Example:
int ar r[] = {10, 20, 30};
int *ptr = ar r;
👉 Example:
int x = 10;
compile time.
Ans: Pointers allow dynamic node c reation in linked lists. Each node contains
data and a pointer to the next node.
👉 Example:
struct Node {
int data;
Node* next;
};
int main() {
int x = 10;
retur n 0;
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y;
retur n 0;
*ptr = 50;
cout << "Dynamically allocated value: " << *ptr << endl;
delete ptr;
retur n 0;
int main() {
int *ptr = ar r;
cout << "Element " << i << ": " << *(ptr + i) << endl;
retur n 0;
5️⃣ Write a program to create and delete a dynamic array using pointers.
#include <iostream>
int main() {
int n;
cout << "Enter size: ";
cin >> n;
retur n 0;
int main() {
int x = 10;
retur n 0;
}
7️⃣ Write a program to create a simple linked list node using pointers.
#include <iostream>
struct Node {
int data;
Node* next;
};
int main() {
head->data = 5;
head->next = NULL;
delete head;
retur n 0;