DS Module 1
DS Module 1
Module 1
Data Structures and Applications
Vinaykumar Y B
Assistant Professor
Department
1 of Computer Science, RLJIT
Outline
Introduction:
Data Structures, Classifications (Primitive & Non Primitive).Data structure
Operations
Review of Arrays
Structures, Self-Referential Structures, and Unions
Pointers
Dynamic Memory Allocation Functions
Representation of Linear Arrays in Memory
Dynamically allocated arrays
Array Operations:
Traversing, Inserting, Deleting, Searching and Sorting
Multidimensional Arrays
Polynomials
Sparse Matrices
Every item is related to its previous and Every item is attached with many other
next item. items
Data items can be traversed in a single run. Data cannot be traversed in a single run.
item Retrieve (A, i) ::= if i € index then return item stored in array
A at index position i.
else return error
Array Store(A, i ,x) ::= If i € index then return array A , by storing
x at index position i in A
else return
Department error
of Computer Science, RLJIT 11
end Array
Review of Arrays
SINGLE OR ONE DIMENSIONAL ARRAY
• A single or one dimensional array is a linear list consisting of
related elements of same type.
• In memory, all the elements are stored in continuous memory-
location one after the other.
Syntax :
data_type array_name [size];
Example:
int a[3];
char carr[20] ;
float b[3] ;
Initialization of Array :
int a[5] = { 4, 5, 7, Department
3, 9}; of Computer Science, RLJIT
12
Example :
Review of Arrays
Multi-Dimensional Array
(Array of Array) Array having more than one subscript is called Multi-
Dimensional array.
Multi Dimensional Array is also called as Matrix.
Ex: Two dimensional array, Three dimensional array, four dimensional array
etc.
2D array
The Two Dimensional array is used for representing the elements of the array in
the form of the rows and columns and these are used for representing the matrix.
Two Dimensional Array uses the two subscripts for declaring the elements of the
Array
Syntax :
data_type array_name[num_of_rows][num_of_column];
Example:
int a[2][2];
Array initialization syntax: Different ways to initialize two dimensional array
int arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}};
Department of Computer Science, RLJIT 14
Structures
A structure is a user-defined data type.
It is defined as a group of dissimilar or heterogeneous data items, where items
can be of a different data type.
Structure declaration:
Syntax :
struct structure_name
Example:
{ struct student
type1 var1; {
type2 var2; int rno;
int age;
..............................
char name[30];
typeN varN; };
};
Where
struct is a keyword(for structure)
type1, type2, ….typeN are standard data types like int, float, char, double
var1, var2,…varN are member of the structures.
Department of Computer Science, RLJIT 15
structure_name or tagname is any identifier
Structures
Example to define structure for employee in c.
int main()
{
struct Point p1; // The variable p1 is declared like a normal
variable
}
How to initialize structure members?
Structure members cannot be initialized with declaration.
For example the following C program fails in compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
int main()
{
struct Point p1 = {0, 1};
int main()
{
//Struct vehicle v1;
printf(Vehicle No of Wheels : %d",v1.wheels);
printf(Vehicle Name : %s",v1.vname);
printf(Vehicle Color : %s",v1.color);
return(0); Output
} Vehicle No of Wheels : 4
Vehicle Name : Swift Desire
VehicleColor:White
Structures
Array of Structures:
struct student ∙ In this example we have declare a structure variable
{ (i.e s).
char name[20]; ∙ Using this structure variable (i.e s) we can store the
int usn; information of only one student.
int marks; ∙ If we want to store information of more number of
}s; students then we declare structure variable as an
Array.
∙ struct student ∙
∙ { ∙ In this example we have declare a structure
∙ char name[20]; variable as an Array i.e s[10].
∙ int usn; ∙ Using this s[10] we can store the information of
∙ int marks; 10 students.
∙ }s[10]; ∙
– Linked Lists
– Stacks
– Queues
– Trees
– Graphs etc
int main( )
Output
{ value of a = 5
int a = 5;
int *b; value of a = 5
b = &a;
BBB(-5:10)
Number of elements = UB – LB + 1
Number of elements = 10-(-5)+ 1=10+5+1=16
while( low<=high)
{
mid=(low+high)/2;
if (key == a[mid])
return mid;
if (key < a[mid])
high=mid-1;
if (key > a[mid])
low=mid+1;
}
return -1;
} Department of Computer Science, RLJIT 62
Array Operations
Sorting:
Arranging data elements of a data structure in a specified order is
called sorting.
When all the elements of array are arranged in either ascending or
descending order, the operation is called as Sorting.
The Sorting can be done using Insertion, Selection or Bubble sort
techniques.
Consider the unsorted elements as shown below
Note:
• In bubble sort we perform n-1 passes.
• In each pass one element will be placed in correct position.
temp = A[ i ];
A[ i ] = A[ i+1 ];
A[ i + 1] = temp;
}
}
} Department of Computer Science, RLJIT 68
}
Array Operations
Polynomial:
A polynomial is mathematical expression consisting of sum of
terms. A term is made up of coefficient and exponent.
An example of polynomial is
•In other words, the sparse matrix can be defined as the matrix that
has a greater number of zero elements than the non-zero elements.
Array Operations
Sparse Matrix :
A matrix that has very few non-zero elements (or A matrix that has
more number of zero elements)
typedef struct
{
int row ;
int col ;
int val;
}term;