Comprehensive Data Structures Notes
Data Structure and Its Types
A data structure is a way of organizing and storing data efficiently to perform operations like
searching, sorting, insertion, and deletion. Choosing the right data structure improves algorithm
efficiency and reduces memory usage.
### Types of Data Structures:
1. **Primitive Data Structures:**
- Integer, Float, Character, Boolean.
2. **Non-Primitive Data Structures:**
- **Linear Structures:**
- Array: Fixed-size, contiguous memory allocation.
- Linked List: Dynamic size, non-contiguous storage.
- Stack: LIFO structure used in function calls.
- Queue: FIFO structure used in scheduling.
- **Non-Linear Structures:**
- Trees: Hierarchical representation of data.
- Graphs: Collection of nodes and edges.
### Importance of Data Structures:
- Efficient problem-solving.
- Memory optimization.
- Faster processing of data.
### Real-World Applications:
- Arrays: Used in databases.
- Linked Lists: Used in memory management.
- Stacks: Used in expression evaluation.
- Queues: Used in job scheduling.
Linked List
A **linked list** is a dynamic data structure where elements, called nodes, are connected using
pointers. Each node contains:
- **Data:** Stores the value.
- **Pointer:** Points to the next node in the sequence.
### Types of Linked Lists:
1. **Singly Linked List:** Each node has a pointer to the next node.
2. **Doubly Linked List:** Each node has pointers to both the previous and next nodes.
3. **Circular Linked List:** The last node links back to the first node.
### Advantages of Linked Lists:
- Dynamic memory allocation.
- Efficient insertion and deletion.
### Disadvantages:
- Extra memory needed for pointers.
- Sequential access is slower than arrays.
### Applications of Linked Lists:
- Managing memory allocation in OS.
- Implementing stacks and queues.
Garbage Collection
Garbage collection is an automatic memory management process that removes unused objects to
free up memory.
### Phases of Garbage Collection:
1. **Mark Phase:** Identifies active objects in memory.
2. **Sweep Phase:** Deletes unused objects to free memory.
3. **Compaction Phase:** Reduces fragmentation by reorganizing memory.
### Advantages:
- Prevents memory leaks.
- Automates memory management.
### Disadvantages:
- Can slow down program execution.
- Less control for programmers.
### Applications:
- Java and Python use automatic garbage collection.
- Operating systems manage process memory using garbage collection.
Binary Tree
A **binary tree** is a hierarchical data structure where each node has at most two children.
### Types of Binary Trees:
1. **Full Binary Tree:** Each node has either 0 or 2 children.
2. **Complete Binary Tree:** All levels except possibly the last are fully filled.
3. **Perfect Binary Tree:** All internal nodes have 2 children, and all leaves are at the same level.
4. **Binary Search Tree (BST):** Left subtree has smaller values, right subtree has larger values.
### Advantages:
- Fast searching and sorting.
- Efficient hierarchical data representation.
### Disadvantages:
- Unbalanced trees can be inefficient.
### Applications:
- Used in databases for indexing.
- Binary search trees are used in search engines.
Queue
A **queue** is a linear data structure that follows the FIFO (First In, First Out) principle.
### Types of Queues:
1. **Simple Queue:** Insertions happen at the rear, deletions at the front.
2. **Circular Queue:** The last element connects back to the first.
3. **Priority Queue:** Elements are dequeued based on priority.
4. **Deque (Double-Ended Queue):** Insertions and deletions occur from both ends.
### Advantages:
- Used in scheduling tasks.
- Helps in order processing.
### Disadvantages:
- Fixed size in arrays.
- Limited access to elements.
### Applications:
- Process scheduling in OS.
- Used in breadth-first search algorithms.
Array
An **array** is a collection of elements of the same type stored in contiguous memory locations.
### Types of Arrays:
1. **One-Dimensional Array:** Stores elements in a single row.
2. **Multi-Dimensional Array:** Stores elements in multiple rows and columns.
### Advantages:
- Fast access to elements using index.
- Used for matrix operations.
### Disadvantages:
- Fixed size.
- Costly insertion and deletion.
### Applications:
- Used in database indexing.
- Matrix operations in scientific computing.
File Handling
File handling is a process that allows a program to read and write data to files.
### File Modes:
- **Read (r):** Opens a file for reading.
- **Write (w):** Opens a file for writing (overwrites existing content).
- **Append (a):** Adds new data to the end of the file.
- **Read & Write (r+, w+):** Opens a file for both reading and writing.
### Advantages:
- Stores large amounts of data permanently.
- Data can be easily shared between programs.
### Disadvantages:
- Risk of data corruption.
- Slower access compared to memory storage.
### Applications:
- Storing user data in applications.
- Logging system activities in software.
Comprehensive Data Structures Notes
Data Structure and Its Types
A data structure is a way of organizing and storing data efficiently to perform operations like
searching, sorting, insertion, and deletion. Choosing the right data structure improves algorithm
efficiency and reduces memory usage.
### Types of Data Structures:
1. **Primitive Data Structures:**
- Integer, Float, Character, Boolean.
2. **Non-Primitive Data Structures:**
- **Linear Structures:**
- Array: Fixed-size, contiguous memory allocation.
- Linked List: Dynamic size, non-contiguous storage.
- Stack: LIFO structure used in function calls.
- Queue: FIFO structure used in scheduling.
- **Non-Linear Structures:**
- Trees: Hierarchical representation of data.
- Graphs: Collection of nodes and edges.
### Importance of Data Structures:
- Efficient problem-solving.
- Memory optimization.
- Faster processing of data.
### Real-World Applications:
- Arrays: Used in databases.
- Linked Lists: Used in memory management.
- Stacks: Used in expression evaluation.
- Queues: Used in job scheduling.
Linked List
A **linked list** is a dynamic data structure where elements, called nodes, are connected using
pointers. Each node contains:
- **Data:** Stores the value.
- **Pointer:** Points to the next node in the sequence.
### Types of Linked Lists:
1. **Singly Linked List:** Each node has a pointer to the next node.
2. **Doubly Linked List:** Each node has pointers to both the previous and next nodes.
3. **Circular Linked List:** The last node links back to the first node.
### Advantages of Linked Lists:
- Dynamic memory allocation.
- Efficient insertion and deletion.
### Disadvantages:
- Extra memory needed for pointers.
- Sequential access is slower than arrays.
### Applications of Linked Lists:
- Managing memory allocation in OS.
- Implementing stacks and queues.
Garbage Collection
Garbage collection is an automatic memory management process that removes unused objects to
free up memory.
### Phases of Garbage Collection:
1. **Mark Phase:** Identifies active objects in memory.
2. **Sweep Phase:** Deletes unused objects to free memory.
3. **Compaction Phase:** Reduces fragmentation by reorganizing memory.
### Advantages:
- Prevents memory leaks.
- Automates memory management.