DS - Unit 1
DS - Unit 1
DS - Unit 1
UNIT 1:
1. Data Storage
Lists and Collections: Arrays are often used to store lists of items where
each item can be accessed using an index. For example, storing a list of
student names or a collection of integers.
3. Data Analysis
5. Algorithm Implementation
6. Image Processing
Pixel Data: Images are often represented as 2D arrays where each element
corresponds to a pixel's color value.
Filters and Transformations: Arrays are used to apply filters and
transformations to images, such as blurring or edge detection.
7. Database Management
8. Networking
9. Scientific Computing
Symbol Tables: Arrays are used to implement symbol tables that store
variable names and their associated attributes.
Syntax Parsing: Arrays are used in parsers to handle expressions and syntax
trees.
Time Series Analysis: Arrays are used to manage and analyze time series
data, such as stock prices over time.
Portfolio Management: Arrays are used to handle and compute financial
portfolios, including risk and return calculations.
1.5 Sparse Matrix
Sparse matrix is a matrix in which most of the elements are zero. Representing a
sparse matrix efficiently is crucial because storing all elements of a large matrix,
where the majority are zeros, would be wasteful in terms of both memory and
computation. Here’s a detailed look at sparse matrices and their various
representations:
A. Definition
Description: stores a list of non-zero elements along with their row and
column indices.
Structure: three separate lists (or arrays):
o Values: stores the non-zero values.
o Row_indices: stores the row indices of the non-zero values.
o Col_indices: stores the column indices of the non-zero values.
[0 0 3 0 4 0 5 0 0]
o Values = [3, 4, 5]
o Row_indices = [0, 1, 2]
o Col_indices = [2, 1, 0]
o Values = [3, 4, 5]
o Column_indices = [2, 1, 0]
o Row_pointers = [0, 1, 2, 3]
This indicates:
o Values = [5, 4, 3]
o Row_indices = [2, 1, 0]
o Column_pointers = [0, 1, 2, 3]
This indicates:
[1 2 0 4 5 6 0 8 9]
Linear lists can be implemented using various data structures, each with different
characteristics:
A. Array
B. Linked List
Int data;
Node* next;
};
o Doubly Linked List: Each node has references to both the next and
previous nodes.
Example:
Struct Node {
Int data;
Node* next;
Node* prev;
A singly linked list is a linear data structure where each element (node) contains
data and a reference (or pointer) to the next node in the sequence. Here’s a
comprehensive overview of its implementation, as well as insertion, deletion, and
searching operations.
1. Implementation
#include <iostream>
Public:
// Constructor to initialize the list
Singlylinkedlist() : head(nullptr) {}
Int main() {
Singlylinkedlist list;
List.insert(10);
List.insert(20);
List.insert(30);
Cout << "Searching for 20: " << (list.search(20) ? "Found" : "Not Found") << endl;
List.deletevalue(20);
Cout << "List after deleting 20: ";
List.display();
Return 0;
}
2. Operations
A. Insertion
To insert a new node into a singly linked list, follow these steps:
1. Create a New Node: Allocate memory for a new node and set its data.
2. Insert at the End: If the list is empty, make the new node the head.
Otherwise, traverse to the end of the list and link the new node there.
Insertion Example:
1. Find the Node: Traverse the list to find the node to be deleted.
2. Update Pointers: If the node to delete is found, update the pointer of the
previous node to skip the node to be deleted and delete the node.
Deletion Example:
1. Traverse the List: Start from the head and traverse through each node.
2. Check Value: Compare each node's data with the target value.
Search Example: