1111 M Ss LM
1111 M Ss LM
CAP4103
School of Engineering
Department of Computer Science and Engineering
Submitted By
Student Name Rakesh Dey sarkar
Enrolment Number 230160307023
Programme Masters in Computer Application
Department Computer Science and Engineering
Session/Semester 2023-2025/Second Semester
Submitted To
Faculty Name Ms. Sapna Sharma
Q1.What is Data Structure ? Explain Various Types of Data Structure
.
Ans: A data structure is a way of organizing and storing data to perform operations
efficiently. It defines a set of rules or conventions for organizing and managing data,
which allows for easier access, modification, and processing. Data structures are
essential components in computer science and are used in various applications to
solve different types of problems.
1. Arrays:
An array is a collection of elements, each identified by an index or a
key.
Elements in an array are stored in contiguous memory locations.
Accessing elements in an array is fast, O(1) time complexity.
Insertion and deletion operations can be slow, especially in the middle,
as shifting may be required.
2. Linked Lists:
A linked list is a linear data structure where elements are stored in
nodes, and each node points to the next node in the sequence.
Dynamic in nature, which means it can grow or shrink during program
execution.
Insertion and deletion are generally faster compared to arrays,
especially in the middle.
Random access to elements is slower, O(n) time complexity.
3. Stacks:
A stack is a last-in, first-out (LIFO) data structure.
Elements can only be added or removed from the top of the stack.
Common operations include push (to add an element) and pop (to
remove the top element).
Used for managing function calls, undo mechanisms, and parsing
expressions.
4. Queues:
A queue is a first-in, first-out (FIFO) data structure.
Elements are added at the rear (enqueue) and removed from the front
(dequeue).
Common operations include enqueue and dequeue.
Used in scenarios like job scheduling, breadth-first search algorithms.
5. Trees:
A hierarchical data structure with a root node and subtrees of child
nodes.
Binary Trees have at most two children per node.
Binary Search Trees (BST) maintain an ordering property that allows for
efficient search, insertion, and deletion.
Common tree-based algorithms include binary search, AVL trees, and
heaps.
6. Graphs:
A collection of nodes (vertices) and edges connecting these nodes.
Directed graphs have edges with a direction, while undirected graphs
do not.
Used to represent relationships between entities and solve problems
like shortest path algorithms and network flow.
7. Hash Tables:
Utilizes a hash function to map keys to indices in an array.
Provides fast insertion, deletion, and lookup operations on average,
with O(1) complexity.
Collision resolution methods, like chaining or open addressing, handle
situations where multiple keys map to the same index.
8. Heaps:
A specialized tree-based data structure that satisfies the heap property.
Max Heap: Parent nodes are greater than or equal to their child nodes.
Min Heap: Parent nodes are less than or equal to their child nodes.
Used for priority queues and heap sort.
These data structures serve as building blocks for designing algorithms and solving
various computational problems. The choice of the appropriate data structure
depends on the specific requirements and characteristics of the problem at hand.
Q.2 What do you mean by Array? Describe storage structure of
array. Also explain various types of Arrays in Details.
# Declaring an array
my_array = [1, 2, 3, 4, 5]
# Accessing elements
# Modifying elements
1. Unordered Collection: Sets do not maintain any order Python provides several control statements that can alter the behavior of
among elements. loops:
2. Unique Elements: Sets only store unique elements. If you
try to add a duplicate element, it won't be added. - `break`: Terminates the loop immediately.
3. Mutable: You can modify a set after it has been created.
Elements can be added or removed. - `continue`: Skips the rest of the code inside the loop for the current
4. Immutable Elements: Elements in a set must be iteration and proceeds to the next iteration.
immutable (e.g., numbers, strings, tuples).
5. Set Operations: Sets support various operations such as - `pass`: Used when a statement is syntactically required but you want to
union, intersection, difference, and symmetric difference. do nothing.
6. Use Cases: Sets are often used for tasks like removing
Example with loop control statements:
duplicates from a list, checking membership, or
performing mathematical operations involving sets.
```python
for i in range(10):
A dictionary in Python is a data structure used to store collections of
key-value pairs. Here are some key points about dictionaries:
if i == 3:
A `for` loop is used to iterate over a sequence (such as a list, tuple, string, or Lists:
range) or any iterable object. It executes a block of code for each element in
the sequence.
Lists are ordered collections of elements, which means
Example:
the elements are stored in a specific sequence and can
be accessed by their index.
```python
Lists can contain elements of different data types,
fruits = ["apple", "banana", "cherry"] such as integers, floats, strings, or even other lists.
```python
If-Else Statement:
count = 0
The elif statement allows you to check for multiple conditions. It stands You can combine multiple conditions using logical operators to
for "else if". You can have multiple elif statements followed by an create more complex conditions.
optional else statement.
x=5
String Methods:
y = 20
4. split(): Splits the string into a list of substrings based on a delimiter Bitwise operators in Python are used to perform operations on
(default is whitespace). individual bits of integers. Here's an explanation of the bitwise
operators you mentioned:
5. join(): Joins the elements of an iterable (e.g., list) into a string using
the specified separator.
& (Bitwise AND):
6. replace(): Replaces occurrences of a substring with another substring.
8. startswith() and endswith(): Check if the string starts or ends with a a=5 # Binary: 101
specified substring.
b=3 # Binary: 011
9. count(): Count the occurrences of a substring within the string.
result = a & b
y = 10 result = a | b
The or operator returns True if at least one of the operands is True, The ~ operator performs a bitwise NOT operation on a single
otherwise, it returns False. integer. It returns the one's complement of the integer, which
means it flips all the bits of the integer, turning 0s into 1s and vice
versa.
x=5
not Operator:
For loop and while loop A for loop is used when you know how
many times you want to execute a block of code. It iterates over a
The not operator is a unary operator that returns the opposite of the sequence (such as a list, tuple, string, or range) and executes the
boolean value of its operand. It negates the condition. block of code for each item in the sequence.
x=5
Differences:
Unary + Operator:
Binary + Operator:
Example:
Unary - Operator:
Binary - Operator:
Arrays are widely used in programming for tasks like storing lists of items, iterating
through collections, and implementing algorithms that require constant-time access
to elements. Despite their efficiency for random access, arrays may have limitations,
such as fixed size and potential inefficiencies in insertions and deletions, particularly
in the middle of the array. In such cases, other data structures like linked lists may be
more suitable.
In computer science, arrays come in various types, each serving specific purposes and
addressing different requirements. Here are some of the common types of arrays:
1.One-dimensional Array:
A simple, linear array where elements are stored in a single line or row.
Accessing elements involves using a single index.
Examples include arrays of integers, characters, or floating-point
numbers.
# One-dimensional array in Python
my_array = [1, 2, 3, 4, 5]
2.Multi-dimensional Array:
Arrays with more than one dimension. Common types include 2D arrays
(matrices) and 3D arrays.
Elements are accessed using multiple indices corresponding to the array's
dimensions.
Useful for representing tables, grids, and matrices.
# Two-dimensional array (matrix) in Python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
3. Dynamic Array:
An array that can dynamically resize during runtime.
Provides a higher-level abstraction than traditional fixed-size arrays.
Commonly used in languages like Python with lists or Java with ArrayList.
# Dynamic array in Python using a list
dynamic_array = [1, 2, 3]
5. Sparse Array:
An array in which most of the elements have the same default value, typically
zero or null.
Efficiently stores and represents arrays with a small number of non-default
elements.
Often used in applications dealing with large datasets with sparse
characteristics.
# Sparse array in Python using a dictionary
sparse_array = {0: 1, 2: 3, 5: 6}
6. String Array:
An array where each element is a string.
Useful for storing and manipulating a collection of strings.
// String array in Java
7. Circular Array:
An array where the last element is followed by the first element, creating a
circular structure.
Useful in scenarios where cyclic operations are required, like rotating
elements.
# Circular array in Python