0% found this document useful (0 votes)
386 views

Module 1 DSA

The document discusses data structures and arrays. It defines data structures and classifies them as primitive and non-primitive. Arrays are discussed as a fundamental non-primitive data structure. Different types of arrays are described, including one-dimensional, two-dimensional, and multi-dimensional arrays. The representation of arrays in memory and dynamic memory allocation of arrays is also summarized.

Uploaded by

pradeepnagaral5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
386 views

Module 1 DSA

The document discusses data structures and arrays. It defines data structures and classifies them as primitive and non-primitive. Arrays are discussed as a fundamental non-primitive data structure. Different types of arrays are described, including one-dimensional, two-dimensional, and multi-dimensional arrays. The representation of arrays in memory and dynamic memory allocation of arrays is also summarized.

Uploaded by

pradeepnagaral5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Search Educations | VTU CSE 2021 Syllabus|21CS32

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

Data structure is a representation of the logical relationships existing between individual


elements of data. A data structure is a way of organizing all data items that considers not
only the elements stored but also their relationship to each other.

The logical or mathematical model of a particular organization of data is called a data structure.

Classification of Data Structures

Data Structures can be divided into two categories,

i) Primitive Data Structures

ii) Non-Primitive Data Structures

Search Educations Page 1

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Primitive Data Structures

These are basic data structures and are directly operated upon by the machine instructions.

Example: Integers, Floating Point Numbers, Characters and Pointers etc.

Non-Primitive Data Structures

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.

Search Educations Page 2

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Operations on Data Structures

The commonly used operations on data structures are as follows,


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

i) One dimensional array

ii) Two dimensional array

iii) Multi-dimensional array

Search Educations Page 3

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

One Dimensional Array

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.

Declaration of One-Dimensional Array

Syntax:

data _type array_name [array size];

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];


 

Search Educations Page 4

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Memory Occupied by One-Dimensional Array

Total memory = array size * size of (data type)

In the above example, int a[25];

Array size=25

Data type is int so sizeof (int) =2 bytes (in 16 bit machine)

So, total memory=25*2 =50 bytes

Structure of one-dimensional array:

Index for an array for n elements starts from zero and ends with
n-1 Consider int array[n];



Array[0] Array[1] Array[3] Array[4] Array[5] Array[5]

Hold The First Element Hold The Last Element

Search Educations Page 5

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Representation of Linear Arrays in Memory

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:

LOC (LA [K]) = Base (LA) + w (K – LB)

Here,

LA is the linear array.

th
LOC(LA [K]) is the location of the K element of LA.

Base (LA) is the base address of LA.

w is the number of bytes taken by one element.

K is the Kth element.

LB is the lower bound.

Search Educations Page 6

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Suppose we want to find out LOC (A [3]). Here,

Base (A) = 1000

w = 2 bytes (Because an integer takes two bytes in the memory).

K=3

LB=0

After putting these values in the given formula, we get:

LOC (A [3]) = 1000 + 2 (3 – 0)

= 1000 + 2 (3)

= 1000 + 6

= 1006

Search Educations Page 7

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

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,

 data_type is the type of items in the array (int,float,char etc.).



 array_name is the name to represent the array.

st
  s1 is size of 1 dimension.
nd
s2 is size of 2 dimension.

rd
s3 is size of 3 dimension.

:

th
sn is size of n dimension.

Example: int survey[3][5][12]; {Total memory = 3*5*12*sizeof(int)}

 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,

Search Educations Page 8

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Consider,

survey[2][3][10] Denotes the rainfall in the month of October during second year in second city

year city month

Representation of Multidimensional Arrays

In C, multidimensional arrays are represented using the array-of-array


representation. Consider an array declared as follows,

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.

Dynamically Allocated Arrays

One Dimensional Arrays

Consider the code below,

int i, n, *list;

printf(“Enter the number of numbers to


generate:”); scanf(“%d”, &n);

if(n < 1)

Search Educations Page 9

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

fprintf(stderr, “Improper value of n\n”);

exit(EXIT_FAILURE);

MALLOC(list, n * sizeof(int));

In the above code snippet MALLOC is a macro as defined below,


#define MALLOC(p,s) \

If(!((p) = mallloc (s))) {\

fprintf(stderr, “Insufficient memory”);\

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.

Consider the statements below,

int *x;

Search Educations Page 10

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

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) \

If(!((p) = callloc (n,s))) {\

fprintf(stderr, “Insufficient memory”);\

exit(EXIT_FAILURE);\

realloc( )

The function realloc resizes memory previously allocated by either malloc or calloc.

Consider the statement,

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.

Search Educations Page 11

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

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) \

If(!((p) = realloc (p,s))) {

fprintf(stderr, “Insufficient memory”);

exit(EXIT_FAILURE);\

Two Dimensional Arrays

Consider the code snippet below for creating a two dimensional array at run time,
int** make2dArray(int rows, int cols)

{/* Create a two dimensional rows and cols arrat */


int **x, i;

MALLOC(x, rows * sizeof(*x));; //get memory for row pointers

/*get memory for each row*/

for(i=0;i < rows;i++)

MALLOC(x[i], cols * sizeof(**x));

return x;

Search Educations Page 12

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

}
The above function may be used in the following way,
int **myArray;

myArray = make2dArray(5,10); //allocates memory for a 5 by 10 two dimensional array of


integers myArray[2][4]=6; //assigns the value 6 to the [2][4] element of this array

Polynomials and Sparse Matrices


e
A polynomial is a sum of terms, where each term has a form ax , where x is the variable, a is
the coefficient, and e is the exponent.

Example:
20 5 4 3 2
A(x) = 3x + 2x + 4 and B(x) = x + 10x + 3x + 1

The largest (or leading) exponent of a polynomial is called its degree.


Consider the two polynomials,
i i
A(x) = ∑aix and B(x) = ∑bix
i
A(x) + B(x) = ∑(ai + bi)x
i j
A(x) . B(x) = ∑(aix . ∑bjx ))

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;

Search Educations Page 13

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

polynomial a; // a is object of polynomial


Now if a is of type polynomial and n < MAX_DEGREE, the polynomial A(x) =∑ =0 ai
would be represented as,
a.degree = n;

a.coef[i] = an-i, 0 <= i <=n

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.

Sparse Matrix Representation

SparseMatrix Create(maxRow, maxCol) ::=

#define MAX_TERMS 101 /* maximum number of terms + 1 */

typedef struct

int col;

int row;

Search Educations Page 14

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

int value;

}term;

term a[MAX_TERMS];

Consider the sparse matrix below represented in triple < row, col, value >

row col value


a[0] 6 6 8
col0 col1 col2 col3 col4 col5
a[1] 0 0 15
row0 15 0 0 22 0 -15
a[2] 0 3 91
row1 0 11 3 0 0 0
a[3] 0 5 11
row2 0 0 0 -6 0 0
a[4] 1 1 3
row3 0 0 0 0 0 0
a[5] 1 2 28
row4 91 0 0 0 0 0
a[6] 2 3 22
row5 0 0 28 0 0 0
a[7] 4 0 -6
a[8] 5 2 -15

Search Educations Page 15

Availaible at VTU HUB (Android App)


Search Educations | VTU CSE 2021 Syllabus|21CS32

Above figure shows how a sparse matrix is represented in the array a.

a[0].row contains the number of rows.

a[0].col contains the number of columns.

a[0].value contains the total number of nonzero entries.


Positions 1 through 8 store the triples representing the nonzero entries. The row index is in the
field row, the column index is in the field col, and the value is in the field value.

Search Educations Page 16

Availaible at VTU HUB (Android App)

You might also like