21CS32 DSA Module1 Notes
21CS32 DSA Module1 Notes
Lecture Notes on
21CS32
DATA STRUCTURES AND APPLICATIONS
Prepared by
Ms. Thapaswini P S
Dept. of Computer Science and Engineering,
VCET Puttur
SEPTEMBER 2022
Module-1 : INTRODUCTION
Contents
Lecture Notes | 21CS32-Data Structures and Applications| Module 1
Definition:
Data structure is a mathematical or logical model of storing and organizing the data in a
particular way in a computer required for designing and implementing efficient algorithms
and program development.
OR
Data Structure is a way of collecting and organizing data in such a way that we can perform
operations on these data in an effective way.
Therefore, the last element, that is the 10th element, will be stored in marks[9]. In the
memory, the array will be stored as shown in below Figure.
Formally an array is defined as a set of pairs <index,value> where index is a position and
value is a data stores in position index.
Initialization of an array:
Example:
Given the base address of an array M [1300…..1900] as 1020 and size of each element is 2
bytes in the memory. Find the address of M [1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
An ADT of an array provides details of various operations that can be performed on an array.
1.5 Structure:
Definition:
Structure is basically a user-defined data type.
It is defined as a group of similar/dissimilar data items where each item can be of a same or
different data type stored under a single name.
The major difference between a structure and an array is that an array can store only
information of same data type.
Structure Declaration:
struct struct–name
{
data_type var–name;
data_type var–name;
...............
};
Example:
Like any other data type, memory is allocated for the structure when we declare a variable of
the structure.
Two ways to declare structure variables:
1.
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
struct student stud1;
2.
Initialization of Structures:
A structure can be initialized in the same way as other data types are initialized. Initializing a
structure means assigning some constants to the members of the structure. When the user
does not explicitly initialize the structure, then C automatically does it. For int and float
members, the values are initialized to zero, and char and string members are initialized to '\0'
by default. The initializers are enclosed in braces and are separated by commas. However,
care must be taken to ensure that the initializers match their corresponding types in the
structure definition.
The general syntax to initialize a structure variable is as follows:
Below Figure illustrates how the values will be assigned to individual fields of the structure.
When all the members of a structure are not initialized, it is called partial initialization. In
case of partial initialization, first few members of the structure are initialized and those that
are uninitialized are assigned default values.
To input values for data members of the structure variable stud1, we may write
scanf("%d", &stud1.r_no);
scanf("%s", stud1.name);
Typedef structures:
The typedef (derived from type definition) keyword enables the programmer to create a new
data type name by using an existing data type.
Nested Structures:
A structure can be placed within another structure, i.e., a structure may contain another
structure as its member. A structure that contains another structure as its member is called a
nested structure.
Prior to execution, fixed size of No need to know size of memory prior to execution.
memory has to be decided.
Wastage/shortage of memory Memory is allocated as per requirement.
occurs.
scanf("%d",a+i);
sum=sum+*(a+i);
}
printf("sum=%d\n",sum);
free(a);
return 0;
}
Two-Dimensional Arrays:
C program for 2D dynamic arrays:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 3, c = 4;
int *arr = (int *)malloc(r * c * sizeof(int));
int i, j, count = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
*(arr + i*c + j) = ++count;
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d \t", *(arr + i*c + j));
}
printf("\n");
}
return 0;
}
Output:
While storing the elements of a 2-D array in memory, the elements are allocated contiguous
memory locations. Therefore, a 2-D array must be linearized so as to enable their storage.
There are two alternatives to achieve linearization: Row-Major order and Column-Major
order.
Example 1:
A 0 1 2 3
0 1 -1 5 15
1 0 12 5 2
2 9 7 5 6
Example 2:
A 1 2 3 4
1 1 -1 5 15
2 0 12 5 2
3 9 7 5 6
Example1:
A 1 2 3 4
1 1 -1 5 15
2 0 12 5 2
3 9 7 5 6
VTU QP Problem:
Suppose each student in a class of 25 students is given 4 tests, assume the students are
numbered from I to 25, and the test scores are assigned in the 25 x 4 matrix called SCORE.
Suppose base (SCORE): 200, w : 4 and the programming language uses row-major order. To
store this 2D array, then find the address of 3rd test of 12th student i.e SCORE (12, 3).
(Appeared in June/July 2018)
Solution:
Given i=12,j=3,m=25,n=4
A[i][j]=BaseAddress+(((i-1)*n)+(j-1))*size
A[12][3]=200+(((12-1)*4)+(3-1))*4
=200+(44+2)*4
=200+ 184=384
What is a polynomial? What is the degree of the polynomial? Write a function to add
two polynomials. (Appeared in JUNE/JULY 2017)
Polynomial Definition:
“A polynomial is a sum of terms, where each term has a form axe, where x is the variable, a
is the coefficient and e is the exponent.”
The largest (or leading) exponent of a polynomial is called its degree. Coefficients that are
zero are not displayed. The term with exponent equal to zero does not show the variable since
x raised to a power of zero is 1.
Polynomial Representation:
One way to represent polynomials in C is to use typedef to create the type polynomial as
below:
#define MAX-DEGREE 101 /*Max degree of polynomial+1*/
typedef struct
{
int degree;
float coef[MAX-DEGREE];
} polynomial;
Now if a is a variable and is of type polynomial and n < MAX_DEGREE, the polynomial
A(x) = Σ ai xi would be represented as:
a.degree = n
a.coef[i] = an-i , 0 ≤ i ≤ n
In this representation, the coefficients are stored in order of decreasing exponents, such that
a.coef [i] is the coefficient of xn-i provided a term with exponent n-i exists;
Otherwise, a.coef [i] =0. This representation leads to very simple algorithms for most of the
operations; it wastes a lot of space.
Suppose if term(a)>term(b) then case 1 will be executed. Else if a<b case -1 will be
executed. Else if a==b, case 0 will be matched.
Polynomial Addition:
1) Give ADT of sparse matrix and show with a suitable example sparse matrix
representation storing as triples. Give a sample transpose function to transpose sparse
matrix. (Appeared in JUNE/JULY 2017)- 8 Marks
The Abstract Data type: An ADT Sparse matrix is the one that shows the various operations
that can be performed on sparse matrices.
Consider the following sparse matrix, and the memory representation of sparse matrix stored
as triple.
Transposing of a Matrix:
To transpose a matrix, interchange the rows and columns. This means that each element
a[i][j] in the original matrix becomes element a[j][i] in the transpose matrix.
Laboratory Component:
1. Design, Develop and Implement a menu driven Program in C for the following Array
Operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Exit.
Support the program with functions for each of the above operations
2. Design, Develop and Implement a menu driven Program in C for the following Array
operations
a. Inserting an Element (ELEM) at a given valid Position (POS)
b. Deleting an Element at a given valid Position POS)