Data Struct Unit 1
Data Struct Unit 1
Data Struct Unit 1
A data structure is a storage that is used to store and organize data. It is a way of
arranging data on a computer so that it can be accessed and updated efficiently.
A data structure is not only used for organizing the data. It is also used for
processing, retrieving, and storing data.
Complexities of an Algorithm
The complexity of an algorithm computes the amount of time and spaces
required by an algorithm for an input of size (n). The complexity of an
algorithm can be divided into two types. The time complexity and
the space complexity.
Time complexity
The time complexity of an algorithm is a measure of how much time it
takes to run as a function of the input size.
Space complexity
The space complexity of an algorithm is a measure of how much memory
it needs to run as a function of the input size.
Asymptotic Notation and Analysis
In Asymptotic Analysis, we evaluate the performance of an algorithm
in terms of input size (we don’t measure the actual running time). We
calculate, how the time (or space) taken by an algorithm increases with
the input size.
Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average-case complexity of an
algorithm.
Let g and f be the function from the set of natural numbers to itself. The
function f is said to be Θ(g), if there are constants c1, c2 > 0 and a
natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
Array representation
Address of A[I] = B + W * (I – LB)
I = Subset of element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified assume zero).
Applications of Array
1. Storing Data: Arrays are widely used to store collections of data elements of the same type,
such as integers, characters, or floating-point numbers.
2. Representing Strings: Arrays are often used to represent strings in programming languages,
where each element of the array corresponds to a character in the string.
3. Implementing Lists: Arrays can be used to implement list data structures like stacks and
queues. For example, in a stack, elements are added and removed from one end of the array.
4. Matrices and Multi-dimensional Arrays: Arrays can represent matrices and multi-
dimensional data structures, making them suitable for applications like image processing,
graphics, and scientific computing.
5. Sorting and Searching Algorithms: Arrays play a fundamental role in sorting and searching
algorithms such as bubble sort, quicksort, merge sort, linear search, and binary search.
6. Buffers and Caches: In computer systems, arrays are used as buffers and caches to store
data temporarily, facilitating efficient data processing and I/O operations.
7. Hash Tables: Arrays are used as the underlying data structure for hash tables, where each
element in the array represents a bucket that can store multiple key-value pairs.
8. Sparse Arrays: Sparse arrays, where most of the elements are empty or zero, are used in
applications like representing large matrices with mostly zero values efficiently.
Linked List
A Linked List is a linear data structure which looks like a chain of nodes,
where each node is a different element. Unlike Arrays, Linked List
elements are not stored at a contiguous location. They include a series of
connected nodes. Here, each node stores the data and the address of
the next node.
2. Set the next pointer of the new node to the current head of the list
2. If the list is empty, make the new node the head of the list and return
4. Traverse the list with the pointer variable until you reach the last
node
5. Set the next pointer of the last node to the new node
2. If the position is 1, make the new node the head of the list and
return
4. Traverse the list with the pointer variable until you reach the node
just before the insertion point
5. Set the next pointer of the new node to the next node of the pointer
variable
6. Set the next pointer of the pointer variable to the new node
Algorithm deleteFromBeginning(list):
3. Set the head of the list to the next node of the pointer variable
Algorithm deleteFromEnd(list):
1. If the list is empty, return null
2. If the list contains only one node, set the head of the list to null
3. Set a pointer variable to the head of the list
4. Traverse the list until the pointer variable points to the second-
last node
5. Set the next pointer of the second-last node to null
6. Free the memory occupied by the last node
7. Return the updated list
2. Traverse the list until the pointer variable points to the node
with the given value or until the end of the list is reached
3. If the node with the given value is found, return the node
4. If the end of the list is reached without finding the node, return
null
The first part contains the value of the coefficient of the term.
The second part contains the value of the exponent.
The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown below:
Consider a polynomial P(x) = 7x4 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients,
and 4,3,2,0 are the exponents of the terms in the polynomial. On representing this
polynomial using a linked list, we have
Addition of Polynomials:
Let us consider an example an example to show how the addition of two
polynomials is performed,
Q (x) = 5x3 + 4 x2 - 5
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two
polynomials. The first term of polynomials P and Q contain exponents 4
and 3, respectively. Since the exponent of the first term of the polynomial P
is greater than the other polynomial Q, the term having a larger exponent
is inserted into the new list. The new list initially looks as shown below:
3. We then compare the exponent of the next term of the list P with the
exponents of the present term of list Q. Since the two exponents are equal,
so their coefficients are added and appended to the new list as follows:
4. Then we move to the next term of P and Q lists and compare their
exponents. Since exponents of both these terms are equal and after
addition of their coefficients, we get 0, so the term is dropped, and no
node is appended to the new list after this,
5. Moving to the next term of the two lists, P and Q, we find that the
corresponding terms have the same exponents equal to 0. We add their
coefficients and append them to the new list for the resulting polynomial
as shown below:
1. Define Node Structure: Define a node structure for the linked list, where each
node contains two fields: coefficient and exponent.
2. Initialize Pointers: Initialize pointers ptrA and ptrB to the heads of the linked
lists representing polynomials A and B, respectively.
3. Traverse: Loop through both linked lists simultaneously until both are
exhausted.
If ptrA points to a node with a higher exponent than ptrB, append a
new node with the same coefficient and exponent to the result linked list.
If ptrB points to a node with a higher exponent than ptrA, append a
new node with the same coefficient and exponent to the result linked list.
If both ptrA and ptrB point to nodes with the same exponent, add their
coefficients and append a new node with the sum coefficient and the
same exponent to the result linked list.
4. Append Remaining Terms: If any of the linked lists have remaining terms after
the loop, append their nodes to the result linked list.
5. Output: The result linked list contains the sum of the two polynomials.