Module 1
Module 1
Introduction
Data Structures
Data structure is representation of the logical
relationship existing between individual
elements of data.
Graph applications:-
finding shortest routes, searching, social
network connections, internet routing.
Data Structures operations
Following operations can be performed on the
data structures:
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
Cont..
1. Traversing- It is used to access each data
item exactly once so that it can be processed.
2. Searching- It is used to find out the location
of the data item if it exists in the given
collection of data items.
3. Inserting- It is used to add a new data item
in the given collection of data items.
4. Deleting- It is used to delete an existing
data item from the given collection of data
items.
Cont…
5. Sorting- It is used to arrange the data items
in some order i.e. in ascending or descending
order in case of numerical data and in
dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data
items of two sorted files into single file in the
sorted form.
Review of Arrays
An array is a data structure that can store a
fixed-size sequential collection of elements
of the same data type.
Syntax
datatype arrayName [ arraySize ];
Example:
float salary[15000];
Arrays can be initialized at declaration time:
int age[5]={22,25,30,32,35};
One Dimensional Array
An array which has only one subscript is
known as one dimensional array .
location.
Two Dimensional Array
Declaring a two dimensional array
Initializing Two Dimensional Array
Structures
Structure is a user defined data type that
groups logically related data items of
different data types into a single unit.
Syntax
struct [structure tag]
{
data type variable name;
data type variable name;
data type variable name;
};
Access members of a structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, “abc");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
Difference between Structure and
Union
Why this difference in the size of union and structure variables?
Here, the size of sjob is 40 bytes because
the size of name[32] is 32 bytes
the size of salary is 4 bytes
the size of workerNo is 4 bytes
“calloc” or “contiguous allocation” method in
C is used to dynamically allocate the specified
number of blocks of memory of the specified
type. It initializes each block with a default
value ‘0’.
“free” method in C is used to dynamically de-
allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method
is used, whenever the dynamic memory
allocation takes place. It helps to reduce wastage
of memory by freeing it.
“realloc” or “re-allocation” method in C is used to
dynamically change the memory allocation of a
previously allocated memory. In other words, if
the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be
used to dynamically re-allocate memory. re-
allocation of memory maintains the already
present value and new blocks will be initialized
with default garbage value.
Malloc
“malloc” or “memory allocation” method in C
is used to dynamically allocate a single large
block of memory with the specified size. It
returns a pointer of type void which can be
cast into a pointer of any form. It initializes
each block with default garbage value.
Syntax:
ptr = (cast-type*) malloc(byte-size)
Calloc
“calloc” or “contiguous allocation” method in
C is used to dynamically allocate the specified
number of blocks of memory of the specified
type. It initializes each block with a default
value ‘0’.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
free
“free” method in C is used to dynamically de-
allocate the memory.
Length= UB-LB+1
by one.
Inserting − Adds an element at the given
index.
Deleting − Deletes an element at the given
index.
Searching − Searches an element using the
elements
Traversing
This operation is to traverse through the
elements of an array.
Inserting
Insert operation is to insert one or more data
elements into an array. Based on the
requirement, a new element can be added at
the beginning, end, or any given index of
array.
Example
Deleting
Deletion refers to removing an existing
element from the array and re-organizing all
elements of an array.
Searching
Search for an array element based on its
value or its index.
10
20
30
40
50
Enter the element to be searched
20
The element is present in the array of position
1
Binary Search Program
Sorting
Sorting is the process of rearranging the
elements.
Eg: Bubble Sort
Multidimensional Arrays
Multi-Dimensional array means array of
arrays. A 3D array is a multi-dimensional
array. A 3D array is a collection of 2D arrays.
Syntax:
data_type array_name[block_size][row_size]
[columnsize];
Example
Strings
A finite sequence S of zero or more
characters is called a string. The difference
between a character array and a string is the
string is terminated with a special character ‘\
0’.
Indexing
Concatenation
Length
Substring
A substring is itself a string that is part of a
longer string.
SUBSTRING(string,initial,length)
String : GSSSIETW MYSUR
Position : 2
Length: 9
Indexing
Indexing refers to finding the position where
a string pattern P first appears in a given
string text T.
INDEX(text, pattern)
Strcat(S1,S2)
Length
The number of characters in a string is called
its length.
Length(string)
for the length of a given string.
Eg: Length(‘COMPUTER’)= 8.
String Compare
Disadvantages
1.Time is wasted reading an entire record if most of
the storage consists of inessential blank spaces
2.Certain records may require more space than
available
3.When the correction consists of more or fewer
characters than the original text, changing a
misspelled word requires the entire record to be
changed
Variable-Length Storage with fixed
maximum
Although strings may be stored in fixed
length memory locations as above, there are
advantages in knowing the actual length of
each string.
C program to evaluate a given polynomial by
reading its coefficients in an array.
C Program to add two polynomials