Data Structures using C++ (ICSC2CR3) - Notes
Module 1: Introduction to Data Structures
**Definition**: Data structures are specialized formats for organizing, processing, retrieving, and storing data.
They enable efficient data management and manipulation.
**Classification**:
1. **Primitive Data Structures**: int, float, char, bool.
2. **Non-Primitive Data Structures**:
- **Linear**: Arrays, Linked Lists, Stacks, Queues.
- **Non-Linear**: Trees, Graphs.
- **File Structures**: Data stored in secondary memory.
**Operations on Data Structures**:
- Traversal, Insertion, Deletion, Searching, Sorting, Merging.
**Memory Allocation**:
- **Static Memory Allocation**: Memory size is fixed at compile time. Example: arrays.
- **Dynamic Memory Allocation**: Memory allocated at runtime using `new`, and released using `delete`.
**Pointers**:
- Variables that store the address of other variables. Useful in dynamic memory and data structure
implementation.
**User-defined Data Types**:
- Structures: Group of variables under one name.
- Classes: Blueprint for objects in C++.
**Recursion**:
- A function calling itself. Base case and recursive case required.
- Example: Factorial, Fibonacci sequence, Tower of Hanoi.
Data Structures using C++ (ICSC2CR3) - Notes
Module 2: Arrays
**Linear Array**:
- Stored in contiguous memory locations.
- Elements accessed using index.
- Operations:
- **Traversal**: Visit each element once.
- **Insertion**: Add an element at specific position.
- **Deletion**: Remove element from array.
- **Searching**: Linear and binary search.
- **Sorting**: Organize in order.
**2D Arrays**:
- Array of arrays.
- Useful for matrix representation.
- Accessed using two indices: a[i][j].
**Multidimensional Arrays**:
- Arrays with more than two dimensions.
- Syntax: `int arr[3][4][5];`
- Complex data structures like 3D matrices can be represented.
Module 3: Search and Sort
**Searching Techniques**:
1. **Linear Search**:
- Traverse list sequentially.
- Time Complexity: O(n).
2. **Binary Search**:
- Requires sorted list.
- Repeatedly divide the search interval.
Data Structures using C++ (ICSC2CR3) - Notes
- Time Complexity: O(log n).
**Sorting Techniques**:
1. **Bubble Sort**:
- Repeatedly swaps adjacent elements if they are in wrong order.
- Time Complexity: O(n^2).
2. **Selection Sort**:
- Select the smallest element and place at beginning.
- Time Complexity: O(n^2).
3. **Merge Sort**:
- Divide and conquer algorithm.
- Divide array into halves, sort recursively, and merge.
- Time Complexity: O(n log n).
Module 4: Stack and Queue
**Stack**:
- LIFO (Last In First Out).
- Operations:
- `push`: Add item to top.
- `pop`: Remove item from top.
- `peek/top`: View top element.
- **Applications**:
- Expression conversion (Infix to Postfix/Prefix).
- Expression evaluation.
- Function call management.
**Expression Conversion**:
- Infix: a + b
Data Structures using C++ (ICSC2CR3) - Notes
- Prefix: + a b
- Postfix: a b +
**Queue**:
- FIFO (First In First Out).
- Operations:
- `enqueue`: Insert at rear.
- `dequeue`: Remove from front.
- Types:
- **Simple Queue**
- **Circular Queue**: Rear connects to front.
- **Double Ended Queue (Deque)**: Insert/delete from both ends.
- **Priority Queue**: Elements with high priority dequeued first.
Module 5: Linked Lists
**Linked List**:
- Collection of nodes where each node has data and a pointer to the next.
**Singly Linked List**:
- Node contains data and pointer to next.
- Operations:
- Creation, Insertion (at beginning, middle, end), Deletion, Searching, Display.
**Doubly Linked List**:
- Each node has pointer to next and previous.
- Allows traversal in both directions.
- Operations similar to singly linked list.
**Circular Linked List**:
- Last node points back to the first node.
Data Structures using C++ (ICSC2CR3) - Notes
- Efficient for round-robin or circular traversal tasks.
- Can be singly or doubly linked.
**Applications**:
- Dynamic memory allocation.
- Implementation of stacks, queues.
- Efficient insertion and deletion operations.