0% found this document useful (0 votes)
9 views3 pages

Unit 2 Cheat Sheet

This document serves as a cheat sheet for data structures in C++, covering definitions, types, operations, and examples. It outlines linear and non-linear data structures, basic operations like insertion and deletion, and includes algorithms for searching and sorting. Additionally, it provides insights into arrays, pointers, and linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Unit 2 Cheat Sheet

This document serves as a cheat sheet for data structures in C++, covering definitions, types, operations, and examples. It outlines linear and non-linear data structures, basic operations like insertion and deletion, and includes algorithms for searching and sorting. Additionally, it provides insights into arrays, pointers, and linked lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Structures in C++ – Clean Cheat Sheet

1 Introduction to Data Structures

• Data: Raw facts and figures (e.g., 23, ”John”, 1001).

• Information: Processed data that makes sense (e.g., ”John is 23 and his ID
is 1001”).

1.1 Why Use Data Structures?

• To store and organize data efficiently.

• To perform operations like searching, sorting, insertion, and deletion faster.

2 Types of Data Structures

2.1 Linear Data Structures

Data is organized in a linear sequence.

• Examples: Arrays, Linked Lists, Stacks, Queues

2.2 Non-Linear Data Structures

Data is organized hierarchically.

• Examples: Trees, Graphs

3 Operations on Data Structures

Operation Description
Insertion Add new data to a structure
Deletion Remove data from a structure
Traversal Visit all elements
Searching Find an element
Sorting Arrange elements in a specific order

1
4 Arrays in C++

Definition: Fixed-size collection of elements of the same type.


1 int arr[5] = {10, 20, 30, 40, 50}; // Indexed from 0 to 4

4.1 Operations

Traversal:
1 for(int i = 0; i < 5; i++) {
2 cout << arr[i] << ” ”;
3 }

Insertion:
1 int pos = 2, val = 25;
2 for (int i = 4; i >= pos; i--) {
3 arr[i+1] = arr[i];
4 }
5 arr[pos] = val;

Deletion:
1 int pos = 2;
2 for (int i = pos; i < 4; i++) {
3 arr[i] = arr[i+1];
4 }

5 Searching Algorithms

Linear Search: O(n)


1 int linearSearch(int arr[], int size, int key) {
2 for (int i = 0; i < size; i++)
3 if (arr[i] == key) return i;
4 return -1;
5 }

Binary Search (sorted array only): O(log n)


1 int binarySearch(int arr[], int low, int high, int key) {
2 while (low <= high) {
3 int mid = low + (high - low) / 2;
4 if (arr[mid] == key) return mid;
5 else if (arr[mid] < key) low = mid + 1;
6 else high = mid - 1;
7 }
8 return -1;
9 }

2
6 Sorting Algorithms

Sorting Type Time Com- Description


plexity
Bubble Sort O(n2 ) CompareCompare adjacent el-
ements
Insertion Sort O(n2 ) Insert elements in sorted or-
der
Selection Sort O(n2 ) Select min & place in front
1 void bubbleSort(int arr[], int size) {
2 for (int i = 0; i < size - 1; i++)
3 for (int j = 0; j < size - i - 1; j++)
4 if (arr[j] > arr[j+1])
5 swap(arr[j], arr[j+1]);
6 }

7 2D Arrays

1 int arr[2][2] = {{1, 2}, {3, 4}};


2 for (int i = 0; i < 2; i++) {
3 for (int j = 0; j < 2; j++) {
4 cout << arr[i][j] << ” ”;
5 }
6 }

8 Pointers in C++

Pointer: Variable that stores the address of another variable.


1 int x = 10;
2 int *ptr = &x;
3 cout << *ptr; // Output: 10

9 Linked Lists (Intro)

1 struct Node {
2 int data;
3 Node* next;
4 };

Each node contains:

• Data: actual value

• Next: pointer to the next node

You might also like