Unit 1
Unit 1
Data Structure can be defined as the group of data elements which provides
an efficient way of storing and organising data in the computer so that it can
be used efficiently. Some examples of Data Structures are arrays, Linked List,
Stack, Queue, etc. Data Structures are widely used in almost every aspect of
Computer Science i.e. Operating System, Compiler Design, Artificial
intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as
they enable the programmers to handle the data in an efficient way. It plays
a vitle role in enhancing the performance of a software or a program as the
main function of the software is to store and retrieve the user’s data as fast
as possible
1|Pageskp
Basic Terminology
Data structures are the building blocks of any program or the software.
Choosing the appropriate data structure for a program is the most difficult
task for a programmer. Following terminology is used as far as data
structures are concerned
Group Items: Data items which have subordinate data items are called
Group item, for example, name of a student can have first name and the last
name.
Record: Record can be defined as the collection of various data items, for
example, if we talk about the student entity, then its name, address, course
and marks can be grouped together to form the record for the student.
2|Pageskp
Need of Data Structures
As applications are getting complexed and amount of data is increasing day
by day, there may arrise the following problems:
Processor speed: To handle very large amout of data, high speed processing
is required, but as the data is growing day by day to the billions of files per
entity, processor may fail to deal with that much amount of data.
in order to solve the above problems, data structures are used. Data is
organized to form a data structure in such a way that all items are not
required to be searched and required data can be searched instantly.
3|Pageskp
Reusability: Data structures are reusable, i.e. once we have implemented a
particular data structure, we can use it at any other place. Implementation
of data structures can be compiled into libraries which can be used by
different clients.
4|Pageskp
1. Primitive Data Structures are the data structures consisting of the numbers and the characters
that come in-built into programs.
2. These data structures can be manipulated or operated directly by machine-level instructions.
3. Basic data types like Integer, Float, Character, and Boolean come under the Primitive Data
Structures.
4. These data types are also called Simple data types, as they contain characters that can't be
divided further
Linear Data Structures: A data structure is called linear if all of its elements
are arranged in the linear order. In linear data structures, the elements are
stored in non-hierarchical way where each element has the successors and
predecessors except the first and last element.
Arrays: An array is a collection of similar type of data items and each data
item is called an element of the array. The data type of the element may be
any valid data type like char, int, float or double.
The elements of array share the same variable name but each one carries a
different index number known as subscript. The array can be one
dimensional, two dimensional or multidimensional.
5|Pageskp
age[0], age[1], age[2], age[3],……… age[98], age[99].
Linked List: Linked list is a linear data structure which is used to maintain a
list in the memory. It can be seen as the collection of nodes stored at non-
contiguous memory locations. Each node of the list contains a pointer to its
adjacent node.
Stack: Stack is a linear list in which insertion and deletions are allowed only
at one end, called top.
Queue: Queue is a linear list in which elements can be inserted only at one
end called rear and deleted only at the other end called front.
Non Linear Data Structures: This data structure does not form a sequence
i.e. each item or element is connected with two or more other items in a
non-linear arrangement. The data elements are not arranged in sequential
structure.
6|Pageskp
Tree data structure is based on the parent-child relationship among the
nodes. Each node in the tree can have more than one children except the
leaf nodes whereas each node can have atmost one parent except the root
node.
Hash table -A hash table is a data structure that is used to store keys/value
pairs. It uses a hash function to compute an index into an array in which an
element will be inserted or searched.
If the size of data structure is n then we can only insert n-1 data elements
into it.
7|Pageskp
3) Deletion: The process of removing an element from the data structure is
called Deletion. We can delete an element from the data structure at any
random location.
6) Merging: When two lists List A and List B of size M and N respectively, of
similar type of elements, clubbed or joined to produce the third list, List C of
size (M+N), then this process is called merging
Array
Definition
Arrays are defined as the collection of similar type of data items stored at
contiguous memory locations.
Arrays are the derived data type in C programming language which can store
the primitive type of data such as int, char, double, float, etc.
Array is the simplest data structure where each data element can be
randomly accessed by using its index number.
8|Pageskp
For example, if we want to store the marks of a student in 6 subjects, then
we don’t need to define different variable for the marks in different subject.
instead of that, we can define an array which can store the marks in each
subject at a the contiguous memory locations.
9|Pageskp
Following example illustrates, how array can be useful in writing code for a
particular problem.
1. #include <stdio.h>
2. void main ()
3. {
4. int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 5
6, marks_6 = 89;
5. float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6
)/6;
6. printf(avg);
7. }
1. #include <stdio.h>
2. void main ()
3. {
4. int marks[6] = {56,78,88,76,56,89);
5. int i;
6. float avg;
7. for (i=0; i<6; i++ )
8. {
9. avg = avg + marks[i];
10 | P a g e s k p
10. }
11. printf(avg);
12. }
Advantages of Array
Array provides the single name for the group of variables of the same type
therefore, it is easy to remember the name of all the elements of an array.
Traversing an array is a very simple process, we just need to increment the
base address of the array in order to visit each element one by one.
Any element in the array can be directly accessed by using the index.
1. 0 (zero – based indexing) : The first element of the array will be arr[0].
2. 1 (one – based indexing) : The first element of the array will be arr[1].
3. n (n – based indexing) : The first element of the array can reside at any
random index number.
11 | P a g e s k p
In 0 based indexing, If the size of an array is n then the maximum index
number, an element can have is n-1. However, it will be n if we use 1 based
indexing.
Example :
12 | P a g e s k p
1. In an array, A[-10 ….. +2 ], Base address (BA) = 999, size of an element = 2 by
tes,
2. find the location of A[-1].
3. L(A[-1]) = 999 + [(-1) – (-10)] x 2
4. = 999 + 18
5. = 1017
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
array[position-1] = value;
return 0;
}
13 | P a g e s k p
C Program to delete an element from array at given position
#include <stdio.h>
void main()
{
int arr[50], size, pos, i;
2D Array
2D array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.
14 | P a g e s k p
How to declare 2D Array
The syntax of declaring two dimensional array is very much similar to that of
a one dimensional array, given as follows.
1. int arr[max_rows][max_columns];
Above image shows the two dimensional array, the elements are organized
in the form of rows and columns. First element of the first row is
represented by a[0][0] where the number shown in the first index is the
number of that row while the number shown in the second index is the
number of the column.
15 | P a g e s k p
How do we access data in a 2D array
Due to the fact that the elements of 2D arrays can be random accessed.
Similar to one dimensional arrays, we can access the individual cells in a 2D
array by using the indices of the cells. There are two indices attached to a
particular cell, one is its row number while the other is its column number.
However, we can store the value stored in any particular cell of a 2D array to
some variable x by using the following syntax.
1. int x = a[i][j];
where i and j is the row and column number of the cell respectively.
Initializing 2D Arrays
We know that, when we declare and initialize one dimensional array in C
programming simultaneously, we don’t need to specify the size of the array.
However this will not work with 2D arrays. We will have to define at least
the second dimension of the array.
16 | P a g e s k p
The syntax to declare and initialize the 2D array is given as follows.
C Example :
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
9. printf(“Enter a[%d][%d]: “,i,j);
10. scanf(“%d”,&arr[i][j]);
11. }
12. }
13. printf(“\n printing the elements ….\n”);
14. for(i=0;i<3;i++)
15. {
16. printf(“\n”);
17. for (j=0;j<3;j++)
18. {
19. printf(“%d\t”,arr[i][j]);
20. }
21. }
22. }
17 | P a g e s k p
Sparse matrix
A sparse matrix is a one in which the majority of the values are zero. The
proportion of zero elements to non-zero elements is referred to as
the sparsity of the matrix. The opposite of a sparse matrix, in which the
majority of its values are non-zero, is called a dense matrix.
1. Array representation
2. Linked list representation
Method 1: Using Arrays
2D array is used to represent a sparse matrix in which there are three rows
named as
18 | P a g e s k p
Method 2: Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
A lower triangular matrix is a square matrix in which all entries above the
main diagonal are zero (only nonzero entries are found below the main
diagonal – in the lower triangle).
– Notation: An upper triangular matrix is typically denoted with U and a
19 | P a g e s k p
lower triangular matrix is typically denoted with L.
Triangular Matrix
A matrix is defined as a rectangular array of numbers that are arranged in rows
and columns. The size of a matrix can be determined by the number of rows and
columns in it. A matrix is said to be an “m by n” matrix when it has “m” rows and
“n” columns and is written as an “m × n” matrix. For example, a matrix of order
“5 × 6” has five rows and six columns. We have different types of matrices, such
as rectangular, square, triangular, symmetric, singular, etc.
What is a Triangular Matrix?
A triangular matrix is a special case of a square matrix, where all elements above
or below the principal diagonal are zeros. An upper triangular matrix is a square
matrix, whose all elements below the principal diagonal are zeros. A lower
triangular matrix is a square matrix, whose all elements above the principal
diagonal are zeros. The matrices in the image given below are upper triangular
and lower triangular matrices of order “4 × 4.”
20 | P a g e s k p
Lower Triangular Matrix: A lower triangular matrix is a square matrix, whose all
elements above the principal diagonal are zeros.
21 | P a g e s k p
dimensional array to the one dimensional array in order to store them in the
memory.
A 3 X 3 two dimensional array is shown in the following image. However, this
array needs to be mapped to a one dimensional array in order to store it into the
memory.
There are two main techniques of storing 2D array elements into memory
1. Row Major ordering
In row major ordering, all the rows of the 2D array are stored into the memory
contiguously. Considering the array shown in the above image, its memory
allocation according to row major order is shown as follows.
first, the 1st row of the array is stored into the memory completely, then the
2nd row of the array is stored into the memory completely and so on till the last
row.
22 | P a g e s k p
2. Column Major ordering
According to the column major ordering, all the columns of the 2D array are
stored into the memory contiguously. The memory allocation of the array which is
shown in in the above image is given as follows.
first, the 1st column of the array is stored into the memory completely, then the
2nd column of the array is stored into the memory completely and so on till the
last column of the array.
23 | P a g e s k p
rows and columns as array[M][N] where M is the number of rows and N is the
number of columns.
Example:
2-D array
To find the address of any element in a 2-Dimensional array there are the
following two ways-
1. Row Major Order
2. Column Major Order
1. Row Major Order:
Row major ordering assigns successive elements, moving across the rows and
then down the next row, to successive memory locations. In simple language, the
elements of an array are stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following
formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it
as zero),
N = Number of column given in the matrix.
24 | P a g e s k p
Example: Given an array, arr[1………10][1………15] with base value 100 and the
size of each element is 1 Byte in memory. Find the address of arr[8][6] with the
help of row-major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210
2. Column Major Order:
If elements of an array are stored in a column-major fashion means moving across
the column and then to the next column then it’s in column-major order. To find
the address of the element using column-major order use the following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as
zero),
M = Number of rows given in the matrix.
Example: Given an array arr[1………10][1………15] with a base value of 100 and the
size of each element is 1 Byte in memory find the address of arr[8][6] with the
help of column-major order.
25 | P a g e s k p
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
= 10 – 1 + 1
= 10
Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157
Tri-diagonal matrices
In linear algebra, a tridiagonal matrix is a band matrix that has nonzero elements
only on the main diagonal, the subdiagonal/lower diagonal (the first diagonal
below this), and the supradiagonal/upper diagonal (the first diagonal above the
main diagonal). For example, the following matrix is tridiagonal:
OR
What Is a Tridiagonal Matrix?
A tridiagonal matrix is a square matrix whose elements are zero away from the
main diagonal, the subdiagonal, and the superdiagonal. In other words, it is a
banded matrix with upper and lower bandwidths both equal to . It has the form
26 | P a g e s k p
An important example is the matrix that arises in discretizating the Poisson
partial differential equation by a standard five-point operator, illustrated for
by
27 | P a g e s k p
A tri-diagonal matrix can be represented as a vector of three
arrays:
v = [d1, a1, b1, d2, a2, b2, ..., dn, an, bn]
Where:
[1, 2, 0, 0]
[3, 4, 5, 0]
[0, 6, 7, 8]
[0, 0, 9, 10]
v = [1, 3, 2, 4, 6, 5, 7, 9, 8, 10]
This representation allows for efficient storage and computation, especially when
working with large sparse matrices.
This representation is useful for:
28 | P a g e s k p
- Efficient storage (only non-zero elements are stored)
- Faster computations (e.g., matrix-vector multiplication)
- Simplified implementation of algorithms (e.g., Thomas algorithm for solving
tridiagonal systems)
29 | P a g e s k p