Module 1 DSA
Module 1 DSA
Module – 1
Introduction: Data Structures, Classifications (Primitive & Non-Primitive), Data structure
operations (Traversing, inserting, deleting, searching, and sorting). Review of Arrays. Structures:
Array of structures Self-Referential Structures.
Dynamic Memory Allocation Functions. Representation of Linear Arrays in Memory,
dynamically allocated arrays and Multidimensional Arrays.
Demonstration of representation of Polynomials and Sparse Matrices with arrays.
Data Structure
The logical or mathematical model of a particular organization of data is called a data structure.
These are basic data structures and are directly operated upon by the machine instructions.
These are derived from the primitive data structures. The non-primitive data structures
emphasize on structuring of a group of homogeneous or heterogeneous data items. Example:
Arrays, Lists and Files etc.
Create: The Create operation results in reserving memory for the program elements. The
creation of data structures may take place either during compile time or during run time.
Destroy: The Destroy operation destroys the memory space allocated for the specified data
structure.
Selection: The Selection operation deals with accessing a particular data within a data
structure.
Updating: The Update operation updates or modifies the data in the data structure.
Searching: The Searching operation finds the presence of the desired data item in the list of
data items.
a
Sorting: Sorting is the process of arranging all the data items in the data structure in
particular order, say for example, either in ascending order or in descending order.
Merging: Merging is a process of combing the data items of two different sorted list into a
single list.
Arrays
An array is collection of data items of same data type, which are in consecutive memory
locations. Each value (data item) in an array is indicated by same name that is array name and an
index which indicates the position of value in an array.
Types of Arrays
The one dimensional array or single dimensional array is the simplest type of array that contains
only one row for storing the values of same type.
Syntax:
Where,
data type defines the type of an array or type of the data stores in it.
array name is the name given to represent the array.
array size tells number of values that can be stores in an array.
Example: int a[25];
Array size=25
Index for an array for n elements starts from zero and ends with
n-1 Consider int array[n];
The elements of linear array are stored in consecutive memory locations. The computer does
not keep track of address of each element of array. It only keeps track of the base address of the
array and on the basis of this base address the address or location of any element can be found.
We can find out the location of any element by using following formula:
Here,
th
LOC(LA [K]) is the location of the K element of LA.
K=3
LB=0
= 1000 + 2 (3)
= 1000 + 6
= 1006
Multi-Dimensional Arrays
C allows array of three or more dimensions. The general form of a multi dimentional array is,
data_type array_name[s1][s2][s3]…[sn];
Where,
In the above example array survey may represent a survey data of rain fall during the three
years from January to December in five cities.
If the first index denotes year, the second index city and third index month.
The three dimensional array can be represented as a series of two dimensional arrays shown
as below,
Consider,
survey[2][3][10] Denotes the rainfall in the month of October during second year in second city
a[upper0][upper1] [upper2]… [uppern-1], then the number of elements in the array is,
Where П is the product of the upperi’s. For Example, if we declare a as a[10][10][10], then we
require 10 x 10 x 10 = 1000 units of storage to hold the array. Multidimensional arrays are
represented using row major order and column major order. Row major order stores
multidimensional arrays by rows.
int i, n, *list;
if(n < 1)
exit(EXIT_FAILURE);
MALLOC(list, n * sizeof(int));
exit(EXIT_FAILURE);
calloc( )
The function calloc allocates a user specified amount of memory and initializes the allocated
memory to 0 (i.e., all allocated bits are set to 0); a pointer to the start of the allocated memory is
returned. In case there is insufficient memory to make the allocation, the returned value is NULL.
int *x;
x = calloc(n, sizeof(int));
The above statements can be used to define a one dimensional array of integers, capacity of this
array is n, and x[0:n-1] are initially 0. A macro CALLOC as to be defined as follows, #define
CALLOC(p,n,s) \
exit(EXIT_FAILURE);\
realloc( )
The function realloc resizes memory previously allocated by either malloc or calloc.
realloc(p, s);
The above statement changes the size of the memory block pointed at by p to s. The contents of
the first min{s, oldsize} bytes of the block are unchanged as a result of this resizing.
When s > oldsize, the additional s-oldsize have a new unspecified value and,
When s < oldsize, the rightmost oldsize-s bytes of the old block are freed.
When realloc is able to do the resizing, it returns a pointer to the start of the new block and when
it is unable to do the resizing, the old block is unchanged and the function returns the value
NULL. The below code snippet defines a macro for calloc, #define REALLOC(p, s) \
exit(EXIT_FAILURE);\
Consider the code snippet below for creating a two dimensional array at run time,
int** make2dArray(int rows, int cols)
return x;
}
The above function may be used in the following way,
int **myArray;
Example:
20 5 4 3 2
A(x) = 3x + 2x + 4 and B(x) = x + 10x + 3x + 1
Polynomial Representation
Polynomials can be represented in C using typedef as follows,
#define MAX_DEGREE 101
typedef struct
{
int degree;
float coef[MAX_DEGREE];
}polynomial;
An array is used to store the different coefficients of the polynomial. The size of the array is
equal the degree+1 where degree is the degree of the polynomial,
6 3 2
Example: A(x) = x + 10x + 3x + 1
1 0 3 10 0 0 1
index 0 1 2 3 4 5 6
Sparse Matrix
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total
m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix.
typedef struct
int col;
int row;
int value;
}term;
term a[MAX_TERMS];
Consider the sparse matrix below represented in triple < row, col, value >