Module 1 DS
Module 1 DS
Module 1
Iliyaz Pasha M
Assistant Professor
De
1 partment 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
Strings:
Basic Terminology
Storing
Operations
Pattern Matching algorithms
Department of Computer Science, RLJIT 2
Data Structures
Data structure is a Specific way to store and organize data in a
computer's memory so that these data can be used efficiently later.
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
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 13
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 14
structure_name or tagname is any identifier
Structures
Example to define structure for employee in c.
struct employee
{
int id;
char name[50];
float salary;
};
∙ 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
BBB(-5:10)
Number of elements = UB – LB + 1
Number of elements = 10-(-5)+ 1=10+5+1=16
return n-1;
} Department of Computer Science, RLJIT 47
Questions???
1. Define Data structure. Give its classification. What are the basic operations
that can be performed on data structure?
2. Define array and its types & explain how array is declared & accessed with
an example.
3. What is structure? How is it different from array? Explain different types of
structure declaration with example and give the difference between
structure and union.
4. Brief about Self Referential Structure.
5. Define Pointers. How to declare and initialize pointers, Explain with
example and give advantage and disadvantages of pointer.
6. Explain dynamic memory allocation functions in detail.
7. List the difference between Static memory allocation and Dynamic memory
allocation.
8. Briefly explain dynamically allocated arrays with example.
9. In brief explain array operations of data structures.