1. Page | 1
Data Structure
M.Sc. Computer Science
I Semester
Ms. Arati Singh
Department of Computer Science
Shri Shankaracharya Mahavidyalaya Junwani Bhila
2. 2
UNIT-II
Strings
Strings are actually one-dimensional array of characters terminated by a null character '0'.
Thus a null-terminated string contains the characters that comprise the string followed by
a null.
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."
char greeting [6] = {'H', 'e', 'l', 'l', 'o', '0'};
If you follow the rule of array initialization, then you can write the above statement as follows
−
char greeting [] = "Hello";
Following is the memory presentation of the above defined string in C/C++ −
Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '0' at the end of the string when it initializes the array. Let us try to
print the above mentioned string −
#include <stdio.h>
int main () {
char greeting [6] = {'H', 'e', 'l', 'l', 'o', '0'};
printf("Greeting message: %sn", greeting );
return 0;
}
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
C supports a wide range of functions that manipulate null-terminated strings −
3. 3
Sr.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
char Data Type in C Programming Language
• char keyword is used to refer character data type.
• Character data type allows a variable to store only one character.
Ex: char ch='a';
• The storage size of character data type is 1(32-bit system). We can store only one
character using character data type.
•
• For example, 'A' can be stored using char datatype. You can't store more than one
character using char data type.
Data Type Size(Byte) Range
char 1 -128 to 127 or 0 to 255
4. 4
char Variable Declaration and Variable Initialization:
Variable Declaration: To declare a variable, you must specify the data type & give the
variable a unique name.
char flag ;
Variable Initialization : To initialize a variable you must assign it a valid value.
flag = 'a' ;
Char Variable Declaration and Initialization in two step:
char flag ;
flag = 'a' ;
5. 5
Program
#include<stdio.h>
void main() {
char flag;
flag = 'a' ;
printf("%c n",flag);
}
Outut
a
Press any key to continue . . .
Char Variable Declaration and Initialization in single step:
char flag = 'a' ;
6. 6
Program
#include <stdio.h>
void main () {
char flag = 'a' ;
printf("%c n",flag);
}
Output
a
Press any key to continue . . .
DataStructuresandAlgorithms-Arrays
Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used
to identify the element.
ArrayRepresentation
Arrays can be declared in various ways in different languages. For illustration, let's take C
array declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C
array declaration.
7. 7
As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
InsertionOperation
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.
Here, we see a practical implementation of insertion operation, where we add data at the end
of the array −
Algorithm
Let Array be a linear unordered array of MAX elements.
Example
Result
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such
that K<=N. Following is the algorithm where ITEM is inserted into the Kth position of LA −
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
8. 8
DeletionOperation
Deletion refers to removing an existing element from the array and re-organizing all elements
of an array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of LA.
• Start
• 2. Set J = K
• 3. Repeat steps 4 and 5 while J < N
• 4. Set LA[J] = LA[J + 1]
• 5. Set J = J+1
• 6. Set N = N-1
• 7. Stop
SearchOperation
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
UpdateOperation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to update an element available at the Kth position of LA.
1. Start
2. Set LA[K-1] = ITEM
3. Stop
9. 9
Multi-dimensionalArraysinC
The simplest form of multidimensional array is the two-dimensional array. A two-
dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-
dimensional integer array of size [x][y], you would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be considered as a table which will have x number of rows and y
number of columns. A two-dimensional array a, which contains three rows and four columns
can be shown as follows −
Thus, every element in the array a is identified by an element name of the form a[ i ][ j ],
where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each
element in 'a'.
InitializingTwo-DimensionalArrays
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The following initialization
is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
10. 10
AccessingTwo-DimensionalArrayElements
An element in a two-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array. For example −
int val = a[2][3];
The above statement will take the 4th element from the 3rd row of the array. You can verify
it in the above figure. Let us check the following program where we have used a nested loop
to handle a two-dimensional array –
1. #include <stdio.h>
2. int main () {
3. /* an array with 5 rows and 2 columns*/
4. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
5. int i, j;
6. /* output each array element's value */
7. for ( i = 0; i < 5; i++ ) {
8. for ( j = 0; j < 2; j++ ) {
9. printf("a[%d][%d] = %dn", i,j, a[i][j] );
10. }
11. }
12. return 0;
13. }
When the above code is compiled and executed, it produces the following result −
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
11. 11
As explained above, you can have arrays with any number of dimensions, although it is likely
that most of the arrays you create will be of one or two dimensions.
Pointers
Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily
with pointers, and other tasks, such as dynamic memory allocation, cannot be performed
without using pointers. So it becomes necessary to learn pointers to become a perfect C
programmer. Let's start learning them in simple and easy steps.
As you know, every variable is a memory location and every memory location has its address
defined which can be accessed using ampersand (&) operator, which denotes an address in
memory. Consider the following example, which prints the address of the variables defined
−
1. #include <stdio.h>
2. int main () {
3. int var1;
4. char var2[10];
5. printf("Address of var1 variable: %xn", &var1 );
6. printf("Address of var2 variable: %xn", &var2 );
7. return 0;
}
When the above code is compiled and executed, it produces the following result −
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
WhatarePointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using
it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer. Take a look at some of the valid pointer declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
12. 12
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise,
is the same, a long hexadecimal number that represents a memory address. The only
difference between pointers of different data types is the data type of the variable or constant
that the pointer points to.
Reference operator (&) and Dereference operator (*)
As discussed, & is called reference operator. It gives you the address of a variable.
Likewise, there is another operator that gets you the value from the address, it is called a
dereference operator (*).
Below example clearly demonstrates the use of pointers, reference operator and dereference
operator.
Pointer to an Array
we can also declare a pointer that can point to whole array instead of only one element of the
array. This pointer is useful when talking about multidimensional arrays.
Syntax:
data_type (*var_name)[size_of_array];
Program
int main()
{
int *p;
int (*ptr)[5];
int arr[5];
p = arr;
ptr = &arr;
printf("p = %p, ptr = %pn", p, ptr);
p++;
ptr++;
printf("p = %p, ptr = %pn", p, ptr);
13. 13
return 0;
}
DataStructure-SortingTechniques
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order. Most common orders are in numerical or lexicographical
order.
ImportantTerms
Some terms are generally coined while discussing sorting techniques, here is a brief
introduction to them −
Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than
the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is
greater than the previous element.
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than
the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is
less than the previous element.
Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the successive element is less
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every
next element is less than or equal to (in case of 3) but not greater than any previous element.
Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the successive element is greater
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every
next element is greater than or equal to (in case of 3) but not less than the previous one.
14. 14
DataStructure-BubbleSortAlgorithm
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are swapped
if they are not in order. This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2) where n is the number of items.
HowBubbleSortWorks?
We take an unsorted array for our example. Bubble sort takes Ο(n2
) time so we're keeping it
short and precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare
33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
The new array should look like this −
Next we compare 33 and 35. We find that both are in already sorted positions.
Then we move to the next two values, 35 and 10.
15. 15
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration,
the array should look like this −
To be precise, we are now showing how an array should look like after each iteration. After
the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
Now we should look into some practical aspects of bubble sort.
Algorithm
We assume list is an array of n elements. We further assume that swap function swaps the
values of the given array elements.
16. 16
1. begin BubbleSort(list)
2. for all elements of list
3. if list[i] > list[i+1]
4. swap(list[i], list[i+1])
5. end if
6. end for
7. return list
8. end BubbleSort
DataStructureandAlgorithmsLinearSearch
Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.
17. 17
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
DataStructureandAlgorithmsBinarySearch
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer. For this algorithm to work properly,
the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the collection.
If a match occurs, then the index of item is returned. If the middle item is greater than the item,
then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is
searched for in the sub-array to the right of the middle item. This process continues on the sub-
array as well until the size of the subarray reduces to zero.
HowBinarySearchWorks?
For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the
process of binary search with a pictorial example. The following is our sorted array and let us
assume that we need to search the location of value 31 using binary search.
First, we shall determine half of the array by using this formula −
mid = low + (high - low) / 2
18. 18
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We
find that the value at location 4 is 27, which is not a match. As the value is greater than 27
and we have a sorted array, so we also know that the target value must be in the upper portion
of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for.
So, the value must be in the lower part from this location.
Hence, we calculate the mid again. This time it is 5.
We compare the value stored at location 5 with our target value. We find that it is a match.
19. 19
We conclude that the target value 31 is stored at location 5.
Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers.
Algorithm
20. 20
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
21. 21
Parallel Array
Parallel Array: Also known as structure an array (SoA), multiple arrays of same size such that
i-th element of each array is closely related and all i-th elements together represent an object
or entity. An example parallel array is two arrays that represent x and y co-ordinates of n
points.
Below is another example where we store first name, last name and heights of 5 people in three
different arrays.
first_name = ['Bones', 'Welma', 'Frank', 'Han', 'Jack']
last_name = ['Smith', 'Seger', 'Mathers', 'Solo', 'Jackles']
height = [169, 158, 201, 183, 172]
This way we could easily store the information and for accessing, the first index of each array
corresponds to the data belonging to the same person.
Application:
Two of the most essential applications performs on an array or a record are searching and
sorting.
▪ Searching: Each index in a parallel array corresponds to the data belonging to the same
entity in a record. Thus, for searching an entity based on a specific value of an attribute,
e.g. we need to find the name of a person having height >200 cms in the above example.
Thus, we search for the index in the height array having value greater than 200. Now,
once we have obtained the index, we print the values of the index in the first_name and
last_name arrays. This way searching becomes an easy task in parallel array.
▪ Sorting: Now, using the same fact that each index corresponds to data items in different
arrays corresponding to the same entity. Thus, we sort all arrays based on the same
criteria. For example, in the above displayed example, we need to sort the people in
increasing order of their respective heights. Thus, when we swap the two heights, we
even swap the corresponding values in other arrays using the same index.
Example:-
Advantages:
▪ They can be used in languages which support only arrays of primitive types and not of
records (or perhaps don’t support records at all).
22. 22
▪ Parallel arrays are simple to understand and use, and are often used where declaring a
record is more trouble than it’s worth.
▪ They can save a substantial amount of space in some cases by avoiding alignment issues.
▪ If the number of items is small, array indices can occupy significantly less space than
full pointers, particularly on architectures with large words.
▪ Sequentially examining a single field of each record in the array is very fast on modern
machines, since this amounts to a linear traversal of a single array, exhibiting ideal
locality of reference and cache behaviour.
Disadvantages:
▪ They have significantly worse locality of reference when visiting the records non-
sequentially and examining multiple fields of each record.
▪ They have little direct language support (the language and its syntax typically express no
relationship between the arrays in the parallel array).
▪ They are expensive to grow or shrink, since each of several arrays must be reallocated.
Multi-level arrays can ameliorate this problem, but impacts performance due to the
additional indirection needed to find the desired elements.
Record
▪ A record is a collection of fields, possibly of different data types, typically in fixed
number and sequence.[5] The fields of a record may also be called members, particularly
in object-oriented programming; fields may also be called elements, though these risk
confusion with the elements of a collection.
For example, a date could be stored as a record containing a numeric year field, a month field
represented as a string, and a numeric day-of-month field. A personnel record might contain
a name, a salary, and a rank. A Circle record might contain a center and a radius—in this
instance, the center itself might be represented as a point record containing x and y-
coordinates.
23. 23
Sparse Matrix
What is Sparse Matrix?
In computer programming, a matrix can be defined with a 2-dimensional array. Any array with
'm' columns and 'n' rows represents a mXn matrix. There may be a situation in which a matrix
contains more number of ZERO values than NON-ZERO values. Such matrix is known as
sparse matrix.
Sparse matrix is a matrix which contains very few non-zero elements.
When a sparse matrix is represented with 2-dimensional array, we waste lot of space to
represent that matrix. For example, consider a matrix of size 100 X 100 containing only 10
non-zero elements. In this matrix, only 10 spaces are filled with non-zero values and remaining
spaces of matrix are filled with zero. That means, totally we allocate 100 X 100 X 2 = 20000
bytes of space to store this integer matrix. And to access these 10 non-zero elements we have
to make scanning for 10000 times.
Sparse Matrix Representations
A sparse matrix can be represented by using TWO representations, those are as follows...
Triplet Representation
In this representation, we consider only non-zero values along with their row and column index
values. In this representation, the 0th row stores total rows, total columns and total non-zero
24. 24
values in the matrix.
For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values. This
matrix can be represented as shown in the image...
In above example matrix, there are only 6 non-zero elements ( those are 9, 8, 4, 2, 5 & 2) and
matrix size is 5 X 6. We represent this matrix as shown in the above image. Here the first row
in the right side table is filled with values 5, 6 & 6 which indicates that it is a sparse matrix
with 5 rows, 6 columns & 6 non-zero values. Second row is filled with 0, 4, & 9 which indicates
the value in the matrix at 0th row, 4th column is 9. In the same way the remaining non-zero
values also follows the similar pattern.
Types of Sparse Matrix
1. Triangular Matrix
2. Tri-diagonal Matrix