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

Ds Module 1 Merged

Uploaded by

shadow.range03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Ds Module 1 Merged

Uploaded by

shadow.range03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 123

DATASTRUCTURE AND APPLICATIONS

Module -1
Introduction: Data Structures, Classifications (Primitive & Non Primitive), Data structure
Operations, Review of Arrays, Structures, Self-Referential Structures, and Unions. Pointers and
Dynamic Memory Allocation Functions. Representation of Linear Arrays in Memory,
Dynamically allocated arrays,
Array Operations: Traversing, inserting, deleting, searching, and sorting. Multidimensional
Arrays, Polynomials and Sparse Matrices.
Strings: Basic Terminology, Storing, Operations and Pattern Matching algorithms.
Programming Examples.
Introduction
1.1 Data Structures
 “A data structure is a method of storing and organizing the data in a computer so that it
can be used efficiently”.
 Data Structure is a way of collecting and organizing data in such a way that we can perform
operations on these data in an effective way.
 Data Structures is about rendering data elements in terms of some relationship, for better
organization and storage.
 If the data contains a single value, then it can be represented using primitive data types.
 If the data contains set of values, then it can be represented using non-primitive data types.

Need for Data Structures


 The computers are electronics data processing machines.
 In order to solve a particular problem we need to know:
1. How to represent data in a computer.
2. How to access them.
3. What are the steps to be performed in order to get the needed output.
 These tasks can be achieved with the knowledge of Data structures & Algorithms.

The Study of data structure includes

1. Defining operations that can be performed on data.


2. Representing data in the memory.
3. Determining the amount of memory required to store the data.
4. Determining the amount of time needed to process the data.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG PAGE1


DATASTRUCTURE AND APPLICATIONS

1.2 Classification of Data Structures


The data structures can be classified as shown below:
Integer
Floating point
Primitive Character
Double
Pointers
Data structures
Arrays
Stacks
Linear Queues
Linked lists
Non primitive
Trees
Non linear
Graphs

Figure 1.1 shows the classification of data structures.


 Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions. They have different representations on different computers. Integers, floating point
numbers, character constants, string constants and pointers come under this category.

 Non-primitive data structures are more complicated data structures and are derived from
primitive data structures. They emphasize on grouping same or different data items with
relationship between each data item. Arrays, lists and files come under this category.
 A data structure is said to be linear if its elements form a sequence or a linear list. The linear
data structures like an array, stacks, queues and linked lists organize data in linear order.

 There are basically four ways of representing such linear structure in memory.
1. Arrays: An array is a collection of similar type of items (elements) stored sequentially
(continuously) one after the other in memory.
2. Stack: A stack is an ordered list in which insertions and deletions are made at one end called
the top.
3. Queue: A queue is an ordered list in which insertions and deletions take place at different
ends.”
4. Linked list: Linked list is a linear data structure that consists of a sequence of elements where
each element comprises of two items - the data and a reference (link) to the next node

 A data structure is said to be non-linear if the data are not arranged in sequence or linear. The
insertion and deletion of data is not possible in linear fashion. i.e.,elements form a hierarchical
classification where, data items appear at various levels.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 1


DATASTRUCTURE AND APPLICATIONS

 Trees: Tree is a non-linear data structure which organizes data in hierarchical fashion and
the tree structure follows a recursive pattern of organizing and storing data.
 Graph: It is basically a collection of vertices (also called nodes) and edges that connect these
vertices.

1.3 Data Structures Operations


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 the location of the data item with a given key value or finding
the locations of all data which satisfy one or more conditions.
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.
5. Sorting: It is used to arrange the data items in some logical order. e.g., 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.

1.4 Review of Arrays


1.4.1 Arrays
An Array is a special and powerful data structure and it is used to store, process and print
large amounts of data.
 “An array is a collection of similar type of items (elements) stored sequentially (continuously)
one after the other in memory”.

Ex: 1. int A[5] ; // Array of 5 Integers

A[0] 10
A[1] 20
A[2] 30
A[3] 40
A[4] 50

 The Elements in the Array A can be accessed using the common name A, but with different index.
 The Element „10‟ is called 0th Element and it can be accessed using the Subscript 0 (called Index „0‟)
along with name of the array „A‟.
 An „Index‟ is also called as Subscript ([ ]). It is used to indicate the position of an element in the
Array.

Basic Properties of the Arrays


1. All the elements in an array should be of the same data type.
2. The elements are stored continuously in the memory. (For example, in the array char B[5] , if the first
address is 1000 then the data is stored contiguously in the addresses 1000, 1001, 1002 and so on).

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 2


DATASTRUCTURE AND APPLICATIONS

3. The Subscript (index) of first item is always zero.


4. Each element of an array is accessed using the name of the Array, but with different subscript.
5. The Index of the Array is always an Integer number can‟t be float.
Ex: a [1] or a [5].
a [1.5] is an Error.

1.4.2 Classification of Arrays


1. Single Dimensional Array
2. Multi-Dimensional Array

1. Single Dimensional Array


Definition:
Single dimensional array (One-dimensional array) is a linear list consisting of related data
items of same data type and in the memory, all the data items are stored contiguously in
memory locations one after the other.
Or
An array with one index is called as Single dimensional array (One-dimensional array)

Declaration of Single dimensional arrays


 As we declare the variables before they are used in a program, an array must also be declared and
defined before it is used using following syntax.
 Syntax
data_type array_name[size];
Where,
data_type: data type can be int, float, char etc.
array_name: name of the array which is a valid C variable.
size: it is the number of elements in the array.
Complete declaration ends with Semicolon.
Ex: int Marks[5];
Declares Marks as an array consisting of 5 elements of integer data type.

Ex: int Marks[5];

1000 35 Marks[0]
data type array_name 1002 45 Marks[1]
1004 65 Marks[2]
1006 55 Marks[3]
75
char name[5]; 1008 Marks[4]

Memory location Array name

 5 memory locations are reserved. sizeof(int) is 2 bytes, 2*5=10 bytes are reserved.
 5 memory locations are reserved. sizeof(char) is 1 bytes 1*5=5 bytes are reserved.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 3


DATASTRUCTURE AND APPLICATIONS

Initialization of Single dimensional arrays


 Once an array is declared it must be initialized with some values using Initialization.
“Process of assigning the values to the individual elements of an array is called as Initialization
of an array”.

Syntax data_type array_name[size]={v1,v2,-----,vn};

data_type: it can be int, float, char etc.


array_name: it is the name of the array.
size: it is the number of elements in the array.
v1,v2,-----,vn are the values and should be enclosed within „{„ and „}‟ separated by commas.

Ex: 1. int a[5] = {10, 20, 30, 40, 50};


 The compiler allocates 5 memory locations and these locations are initialized with the integer values
in the order specified.

a[0] 10
a[1] 20
a[2] 30
a[3] 40
a[4] 50

 Address Calculation in single (one) Dimension Arrays:


If the memory address of list[i] needs to be computed, then the size of the int would get by
sizeof (int), then memory address of list[i] is as follows:

list[i] = α + i * sizeof(int)

Where, α is base address.

 For example, an array of 10 integer variables, with indices 0 through 9, may be stored as 10 words at
memory addresses 2000, 2004, 2008, ……2036, so that the element with index i has the address 2000
+ i× 4.[hereα is base address whose value is 2000 and sizeof (int) is4]
 For i=0,1,2…..(size of int is 4 bytes(32bit machine))

1.4.3 Representation of Linear Arrays in Memory


A linear array is a list of homogeneous data element such that

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 4


DATASTRUCTURE AND APPLICATIONS

a. The elements of the array are referenced respectively by an index set consisting of n
consecutive numbers.
b. The elements of the array are respectively in successive memory locations.
The number n of elements is called the length or size of the array. The length or the numbers of
elements of the array can be obtained from the index set by the formula

When LB = 0,
Length = UB – LB + 1
When LB = 1,
Length = UB
Where,
UB is the largest index called the Upper Bound
LB is the smallest index, called the Lower Bound

1.4.4 Representation of linear arrays in memory


 Let LA be a linear array in the memory of the computer. The memory of the computer is simply a
sequence of address location as shown below:

LOC (LA [K]) = address of the element LA [K] of the array LA


 The elements of LA are stored in successive memory cells.
 The computer does not keep track of the address of every element of LA, but needs to keep track only
the address of the first element of LA denoted by,
 Base (LA) and called the base address of LA.
 Using the base address of LA, the computer calculates the address of any element of LAby the
formula
LOC (LA[K]) = Base(LA) + w(K – lower bound)
 Where, w is the number of words per memory cell for the array LA.

1.5 Array Operations


1.5.1 Traversing
 Let array A be a collection of data elements stored in the memory of the computer. Suppose if the
contents of the each elements of array A needs to be printed or to count the numbers of elements of
array A it can be accomplished by Traversing.
 Traversing is a accessing and processing each element in the array exactly once.

 If you want to read n data items from the keyboard, the following statement can be used:
for(i=0;i<5;i++)
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 5
DATASTRUCTURE AND APPLICATIONS

{
scanf(“%d”,&a[i]);
}
 If you want to print „n‟ data items from the keyboard, the following statement can be used:
for(i=0;i<5;i++)
{
printf(“%d\n”, a[i]);
}

1.5.2 Inserting
 Let A be a collection of data elements stored in the memory of the computer. Inserting refers to the
operation of adding another element to the array A.
 Inserting an element at the “end” of the linear array can be easily done provided the memory space
allocated for the array is large enough to accommodate the additional element.
 Inserting an element in the middle of the array, then on average, half of the elements must be moved
downwards to new locations to accommodate the new element and keep the order of the other
elements.
Algorithm:
1. Start
2. Read pos, elem.
3. Create space for item to insert at position
for(i=n-1;i>=pos;i- -)
{
A[i+1] = A[i];
}
4. Insert item at the specified position
A[pos] = elem;
5. Update number of elements in the array
n=n+1;
6. Stop

Example: Let us consider an array


Int A[5]={10,20,30,40,50};
A [0] A [1] A [2] A [3] A [4]
10 20 30 40 50
Let us consider an example of insertion of element 100 at position 3
i..e ITEM=100 and pos=3 and n=5
Iteration 1: i = 4, a[5] = a[4]

A [0] A [1] A [2] A [3] A [4] A[5]


10 20 30 40 50 50

Iteration 2: i = 3, a[4] = a[3]


Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 6
DATASTRUCTURE AND APPLICATIONS

A [0] A [1] A [2] A [3] A [4] A[5]


10 20 30 40 40 50

Iteration 3: i = 2, i>=pos –condition failed.


A[3]=100 // insert element to the pos=3
A [0] A [1] A [2] A [3] A [4] A[5]
10 20 30 100 40 50

Iteration 4: Update number of elements in the array n=6

1.5.3 Deletion
Deleting refers to the operation of removing one element from the array A with specified position.

Algorithm:
1. Start
2. Read pos.
3. Move the elements towards left
for(i=pos ; i< n-1; i++)
{
A[i] = A[i+1];
}
4. Display deleted element at the specified position
elem = a[pos];
5. Decrement the number of elements in the array
n=n-1;
6. Stop

Example: Let us consider an array, int A[5]={10,20,30,100,40,50};


i.e. A[0] A[1] A[2] A [3] A [4] A[5]
10 20 30 100 40 50

Let us consider an example of deleting an element 100 at position 3


i.e. ITEM=100 and K=3 and N=5
Iteration 1: i = 3, a[3] = a[4]

A [0] A [1] A [2] A [3] A [4] A [5]


10 20 30 40 40 50

Iteration 2: i = 4, a[4] = a[5]


A [0] A [1] A [2] A [3] A [4] A [5]
10 20 30 40 50 50
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 7
DATASTRUCTURE AND APPLICATIONS

Iteration 3: i = 5, i<4 condition failed


Iteration 4: decrement the number of elements in the array n=5

1.6 Two dimensional arrays (Multi-dimensional arrays)


 Arrays which are specified with 2 subscripts (2 set of square brackets [ ][ ]) are called 2-
dimensional arrays.
 Arrays with two or more dimensions are called Multi-dimensional arrays.
 In 2-Dimensional Array, the first index indicates the „row size‟( the number of rows) and second
index indicates the „column size‟( the number of columns).
Ex: int a[10][10]; //Two-dimensional array
int b[3][4][5]; //Three-dimensional array

Declaration of Two-dimensional arrays


 As we declare the variables before they are used in a program, an array must also be declared before it
is used using the following syntax.
Syntax:
data_type array_name [row_size][col_size];
data_type: It can be int, float, char etc.
array_name: It is the name of the array.
row_size: It is the number of rows in the array.
col_size: It is the number of columns in the array.

 The size used during declaration of the array is useful to reserve the specified memory locations.

Ex: int a [2][4]; a[0][0] a[0][1] a[0][2] a[0][3]


col_size Col.0 Col.1 Col.2 Col.3
Row 0
Data type row_size
Row 1
Array name a[1][0] a[1][1] a[1][2] a[1][3]

 The array „a‟ is a 2-dimensional array with 2 rows and 4 columns. This declaration informs the
compiler to reserve 8 locations (2*4=8 locations, 2*8=16 bytes in total) continuously one after the
other.

Initialization of 2-dimensional arrays


 As we initialize a variable to the required value, we can initialize the individual elements of the array
during initialization.
Syntax
data_type array_name[row_size][col_size]={
{a1,a2,-----,an},
{b1,b2,-----,bn},
……………...,
{z1,z2,-----,zn}
};

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 8


DATASTRUCTURE AND APPLICATIONS

data_type: It can be int, float, char etc.


array_name: It is the name of the array.
row_size: It is the number of rows in the array.
col_size: It is the number of columns in the array.
a1 to an are the values assigned to 1st row, b1 to bn are the values assigned to 2nd row and so on.
Ex: 1. int a[4][3]= {{11,22,33}, {44,55,66},{77,88,99},{10,20,30} };
The array has 4 rows and 3 columns.
Columns

0 1 2
0 11 22 33
1 44 55 66
Rows 2 77 88 99
3 10 20 30

1.6.2 Storage representation of 2-dimensional arrays


The elements in a 2-dimensional array can be stored using: 1. Row major order
2. Column major order
1. Row major order: the elements are stored row by row one row at a time.
Ex: int a[4][3] = {{11,22,33},{44,55,66},{77,88,99}};

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2]


11 22 33 44 55 66 77 88 99

2000 2002 2004 2006 2008 2010 2012 2014 2016


| row 0 || row 1 || row 2 |

2. Column major order: the elements are stored column by column one column at a time.
Ex: int a[4][3] = {{11,22,33},{44,55,66},{77,88,99}};

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2]


11 44 77 22 55 88 33 66 99

2000 2002 2004 2006 2008 2010 2012 2014 2016


| col 0 || col 1 || col 2 |

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 9


DATASTRUCTURE AND APPLICATIONS

1.7 Basic Algorithms


1.7.1 Searching Algorithms
 The process of identifying or finding a particular record or element is called searching. This can be
implemented using array. Here we have two searching techniques.
1. Linear Search: linear search or sequential search is a method for finding an element within a list. It
sequentially checks each element of the list until a match is found or the whole list has been searched.
 Linear search is usually very simple to implement, and is practical when the list has only a few elements,
or when performing a single search in an unordered list.
 A simple approach is to do linear search:
 Start from the leftmost element of arr[] and one by one compare j with each element of arr[].
 If j matches with an element, return the index number.
 If j doesn‟t match with any of elements, return -1 or element not found in an arr[].

Example: C Program to search the „key‟ element in an array using linear search algorithm.
#include <stdio.h>
void main()
{ Output:
int array[100], key, i, n; Enter number of elements in
printf("Enter number of elements in array\n"); array:
5
scanf("%d", &n);
Enter elements of array
printf("Enter elements of array\n"); 100
for (i = 0; i < n; i++) 25
scanf("%d", &array [ i ] ); 35
30
printf("Enter a number to search\n"); 56
scanf("%d", &key); Enter a number to search:
30
30 is present at location 4
for (i = 0; i < n; i++)
{
if (array[i] == key)
{
printf("%d is present at location %d.\n", key, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", key);
}

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 10


DATASTRUCTURE AND APPLICATIONS

2. Binary Search:
 Binary search is a fast searching algorithm. This search algorithm works on the principle of divide and
conquers.
 Binary search works on sorted arrays. Binary search begins by comparing the middle element of the array
with the target value. If the target value matches the middle element, its position in the array is returned.
If the target value is less than the middle element, the search continues in the lower half of the array. If
the target value is greater than the middle element, the search continues in the upper half of the array.

How Binary Search Works?


 For a binary search to work, it is mandatory for the target array to be sorted. 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
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.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 11


DATASTRUCTURE AND APPLICATIONS

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

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

Example: C Program to search key elements in array using binary search algorithms.
#include<stdio.h>
void main()
{
int n, i, arr[50], key, first, last, middle;
Output:
printf("Enter total number of elements :");
Enter number of elements
scanf("%d",&n);
in array:
printf("Enter array elements:\n");
5
for (i=0; i<n; i++)
Enter elements of array
{
10
scanf("%d",&arr[i]);
25
}
35
printf("Enter a number to find :");
50
scanf("%d", &key);
65
first = 0;
Enter a number to search:
last = n-1;
65
while (first <= last)
65 is present at location 5
{ middle = (first+last)/2;
if ( arr[middle] == key)
{
printf("%d found at location %d\n", key, middle+1);
break;
}
else if ( arr[middle] < key)
{

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 12


DATASTRUCTURE AND APPLICATIONS

first = middle + 1;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
printf("Not found! %d is not present in the list.",key);
}
}

1.7.2 Sorting Algorithm


1. Bubble Sort.
 Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
 Bubble sort algorithm starts by comparing the first two elements of an array and swapping if
necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is
greater than second then, you need to swap the elements but, if the first element is smaller than
second, you mustn't swap the element.
 If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times
to get required result. But, for better performance, in second step, last and second last elements are
not compared because; the proper element is automatically placed at last after first step. Similarly, in
third step, last and second last and second last and third last elements are not compared and so on.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 13


DATASTRUCTURE AND APPLICATIONS

Example: C Program to sort the given array elements using Bubble sort Algorithm.
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n); Output:
Enter the size of array:
printf("Enter the array elements: ");
5
for(i=0;i<n;++i) Enter elements of array
30
scanf("%d",&a[i]);
25
for(i=1;i<n;++i) 35
10
for(j=0;j<(n-i);++j)
56
if(a[j]>a[j+1]) Sorted elements:
10
{
25
temp=a[j]; 30
35
a[j]=a[j+1];
56
a[j+1]=temp;
}
printf("\nArray after sorting: ");
for(i=0;i<n;++i)
printf("%d ",a[i]);
}

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 14


DATASTRUCTURE AND APPLICATIONS

3.1 STRINGS
 “A string is a sequence of characters enclosed within double quotes”.
or
 “String is an array of characters and terminated by NULL character which is denoted by „\0‟.

 A string is stored as a sequence of characters in an array terminated by „\0‟ (NULL character).


Ex: Consider the String “DAVANGERE”.
 This string is stored in the form of an array as shown below:

D A V A N G E R E \0 Null character
0 1 2 3 4 5 6 7 8 9

3.1.1 Declaring String variables


 A string is declared like an array of characters.

Syntax: char string_name[size/length];

Where,
char: data type used to declare the strings or characters.
string_name: It specifies the name of the given string.
size: The size or maximum length (number of characters including „\0‟) of the string is specified in square
brackets.

 Length of the String: The „length‟ is the number of characters stored in the string up to but not
including the null character.
Example
1. char name[21];
 Size of the string is 21, means that it can store up to 20 characters plus one null character.

2. char str[10];
 Size of the string is 10, means that it can store up to 10 characters plus one null character.

3.3.3 Initializing the Strings


 We can initialize an array of characters (Strings) in the declaration itself.
Examples:
1. char a[9]={„C‟, „O‟, „M‟, „P‟, „U‟, „T‟, „E‟, „R‟, „\0‟};
 The compiler allocates 9 memory locations ranging from 0 to 8 and these locations are initialized with
the characters in the order specified.

a C O M P U T E R \0
0 1 2 3 4 5 6 7 8

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 15


DATASTRUCTURE AND APPLICATIONS

2. char b[ ]={„C‟, „O‟, „M‟, „P‟, „U‟, „T‟, „E‟, „R‟, „\0‟};
 For this declaration, the compiler will set the array size to the total number of initial values. i.e. 9. The
characters will be stored in these memory locations in the order specified as shown below:

b C O M P U T E R \0
0 1 2 3 4 5 6 7 8

3. char b[ ]= “COMPUTER”;
 Here, the string length is 8 bytes. But string size is 9 bytes. So, the compiler reserves 8+1 memory
locations and these locations are initialized with the characters in the order specified. The string is
terminated by „\0‟ by the compiler.

b C O M P U T E R \0
0 1 2 3 4 5 6 7 8

3.4 String Input / Output Functions


3.4.1. Token-Oriented I/O functions
 The Input/Output operations performed by scanf() and printf() functions are called token-oriented
Input/Output.
Reading a String using scanf( )
 It is possible to read a string using “scanf()”.
 The conversion specification for reading a string using scanf() is “%s”.
 The scanf() function stops reading characters when it finds the first white space character ( spaces,
tabs and new line characters).
Printing a String using printf( )
 The „printf()‟ function prints the given string (all the characters but not the null character).
 The printf() conversion specification for a string variable is “%s”.

Example: char name[20];


printf(“Enter the name:\n”);
scanf(“%s”, name);
printf(“The entered name is:%s”, name);

3.4.2. Line-Oriented I/O functions


 The Input/Output operation performed by gets() and puts() functions are called line-oriented
Input/Output.
 This type of I/O functions processes entire line (string with white spaces). Hence these are called line-
oriented I/O.
Reading a String using gets( )
 gets() function is used to read a sequence of characters (string) with spaces in between.
 The „gets()‟ function allows us to read an „entire line‟ of input including whitespace characters.
Syntax
gets(string);

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 16


DATASTRUCTURE AND APPLICATIONS

Printing a String using puts( )


 „puts()‟ function is used for printing the given strings.
 Whenever we want to display a sequence of characters stored in memory locations on the screen, then
puts() function can be used.
Syntax
puts(string);

Example: char name[20]; Output:


printf(“Enter the name:\n”); Enter the name:
gets(name); Abdul Kalam
printf(“The entered name is:\n”); Entered name is:
puts(name); Abdul Kalam

3.5 String Manipulation Functions


 The standard library „string.h‟ contains many functions for the string manipulation.

Si.No String Functions Description of each function


1. strlen(str) Returns length of the string str
2. strcpy(dest,src) Copies the source string src to destination string dest
3. strncpy(dest,src,n) Copies n characters of the source string src to destination
string dest
4. strcat(s1,s2) Append string s2 to string s1
5. strncat(s1,s2) Append first n characters of string s2 to string s1
6. strcmp(s1,s2) Compare two strings s1 and s2
7. strncmp(s1,s2,n) Compare n characters of two strings s1 and s2
8. strrev(string) Reverse the given string

3.6 String Operations


3.6.1 Substring: Accessing a substring from a given string requires three pieces of information:
(1) The name of the string or the string itself.
(2) The position of the first character of the substring in the given string.
(3) The length of the substring or the position of the last character of the substring.
Syntax: SUBSTRING (string, initial, length)
The syntax denotes the substring of a string S beginning in a position K and having a length L.
Example: SUBSTRING ('TO BE OR NOT TO BE‟, 4, 7) = 'BE OR N‟
SUBSTRING ('THE END', 4, 4) = ' END'
3.6.2 Indexing: Indexing also called pattern matching, refers to finding the position where a string
pattern P first appears in a given string text T. This operation is called INDEX.
Syntax: INDEX (text, pattern)
If the pattern P does not appears in the text T, then INDEX is assigned the value 0.
The arguments “text” and “pattern” can be either string constant or string variable.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 17


DATASTRUCTURE AND APPLICATIONS

3.7 Pattern Matching


 The process of searching for a pattern string in a given text string is called pattern matching.
 strstr(string,pat) : this function is used to check pattern in given text.

 Example of pattern matching is shown in below diagram.

Knuth, Morris, Pratt string Matching Algorithm:

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 18


DATASTRUCTURE AND APPLICATIONS

Analysis: The while loop is iterated until the end of either the string or the pattern is reached. Since I
is never decreased, the lines that increase i cannot be executed more than m=strlen(string) times. The
resetting of j to failure [j-1]+1 decreases the value of j. so, this can‟t be done more times than j is
incremented by the statement j++ as otherwise, j falls off the pattern. Each time the statement j++ is
executed, i is also incremented. So, j can‟t be incremented more than m times. No statement of code is
executed more than m times.
return ((j == lenp) ? (i - lenp) : -1 )
This statement checks to see whether or not we found the pattern. If we didn‟t find the pattern, the
pattern index j is not equal to the length of the pattern and we return -1. If we found the pattern, then
the starting position is i  the length of the pattern.

Algorithm 2: (Pattern Matching)


P and T are strings with lengths R and S, and are stored as arrays with one character per
element. This algorithm finds the INDEX of P in T.
1.[Initialize.]
Set K= 1 and MAX= S - R + 1
2.Repeat Steps 3 to 5
while K ≤ MAX
3. Repeat for L = 1 to R [Tests each character of P]
If P[L] ≠ T[K + L – l], then:
Go to Step 5
[End of inner loop.]
4.[Success.]
Set INDEX = K, and Exit
5. Set K= K + 1
[End of Step 2 outer loop]
6.[Failure.]
Set INDEX = 0
7. Exit

Observation of algorithms
P is an r-character string and T is an s-character string. Algorithm contains two loops, one inside the
other. The outer loop runs through each successive R-character substring WK = T[K] T[K + 1] ...
T[K+R-l] of T. The inner loop compares P with WK, character by character. If any character does
not match, then control transfers to Step 5, which increases K and then leads to the next substring of
T. If all the R characters of P do match those of some WK then P appears in T and K is the INDEX
of P in T. If the outer loop completes all of its cycles, then P does not appear in T and so INDEX =
0.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 19


DATASTRUCTURE AND APPLICATIONS

4.1 Structures
Structure is a user defined data type that can hold data items of same/different data
types. All data items grouped are logically related & can be accessed by using variables.

Declaration:
Syntax:
struct tag-name
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
};

 In this declaration, struct is a required keyword, tag-name is a name of the structure defined.
 The individual members can be ordinary variables, pointers, arrays or other structures. The member
names within a particular structure must be distinct from one another.

Ex: struct person


{
char name[10];
int age;
float salary;
};

 The above example declares a structure called person that has three fields:
name = a name that is a character array
age = an integer value representing the age of the person
salary = a float value representing the salary of the individual

Ex:
Struct student
{
char name[10]; 10 bytes
int roll_no; 4 bytes
float marks; 8 bytes
}; 22 bytes

 To allocate the memory for the structure, we have to declare the variable as shown below:
Struct student s1,s2; [ size of variables s1,s2 is 22bytes each]
Two ways to declare variables:
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 20
DATASTRUCTURE AND APPLICATIONS

Initialization or Assigning values to fields


 Initialization is the process of Assigning values to structure variables (structure members).
 To assign values to the fields, use .(dot operator) as the structure member operator. This operator is
used to select a particular member of the structure.
Ex: Method-1
struct person
{
char name[10];
int age;
float salary;
}p;
strcpy(p.name,“james”);
p.age = 10;
p.salary = 35000;
Method-2
struct student Struct student
{ {
char name[10]; char name[10];
introll_no;
int age;
floatmarks;
float marks; };
}s1={“virat”,19,25}; Struct student s1={“virat”,19,25};

Accessing structure members


 To access members of structures, we specify the variables followed by the dot operator then followed
by the name of the member.

Reading:
printf(“Enter name of the student\n”);
scanf(“%s”,s.name);
printf(“Enter marks of the student\n”);
scanf(“%f”,&s.marks);
Writing:
printf(“name of the student is %s\n”,s.name);
printf(“marks of the student is %f\n”,s.marks);

4.2 Structure within a structure:


 There is possibility to embed a structure within a structure. There are 2 ways to embed structure.
 1.The structures are defined separately and a variable of structure type is declared inside the definition
of another structure. The accessing of the variable of a structure type that are nested inside another
structure can be done in the same way as accessing other member of that structure.
Example: The following example shows two structures, where both the structure are defined
separately.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 21


DATASTRUCTURE AND APPLICATIONS

struct
struct
{
{ char name[10];
int month; int age;
int day; float salary;
int year; date dob;
}date; } humanBeing;
humanBeing person1;
A person born on February 11, 1944, would have the values for the date struct set as:
person1.dob.month = 2;
person1.dob.day = 11;
person1.dob.year = 1944;

2. The complete definition of a structure is placed inside the definition of another structure.
Example:
typedefstruct
{
char name[10];
int age;
float salary;
struct
{
int month;
int day;
int year;
} date;
} humanBeing;

4.3 Self-Referential Structures


A self-referential structure is the one in which one or more of its components is a
pointer to itself. Self-referential structures usually require dynamic storage management routines
(malloc and free) to explicitly obtain and release memory.
Consider as an example:
struct
{
char data;
struct list *link ;
} list;
 Each instance of the structure list will have two components data and link.
data: is a single character,
link: link is a pointer to a list structure. The value of link is either the address in memory of an
instance of list or the null pointer.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 22


DATASTRUCTURE AND APPLICATIONS

Consider these statements, which create three structures and assign values to their respective fields:
list item1, item2, item3;
item1.data = 'a';
item2.data = 'b';
item3.data = 'c';
item1.link = item2.1ink = item3.link = NULL;

Structure variables item1, item2 and item3 each contain the data items a, b, and c respectively, and
the null pointer. These structures can be attached together by replacing the null link field in item 2
with one that points to item 3 and by replacing the null link field in item 1 with one that points to
item 2.
item1.link = &item2;
item2.1ink = &item3;

4.4 Unions
A union is a collection of data of similar data types or dissimilar data types. A union
declaration is similar to a structure, but the fields of a union must share their memory space. This
means that only one field of the union is "active" at any given time.
Syntax:
union tag-name
{
data_type member 1;
data_type member 2;
………………………
………………………
data_type member n;
};
Example:
unionstudent
{
char name[10];
intage;;
float salary;
};
 The major difference between a union and a structure is that unlike structure members which are
stored in separate memory locations; all the members of union must share the same memory space.
This means that only one field of the union is "active" at any given time.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 23


DATASTRUCTURE AND APPLICATIONS

Example: C program to show how structure behaves


void main()
#include <stdio.h> {
struct student struct student s1;
{ s1.marks=29;
int marks; s1.grade= „A‟;
char grade; s1.percentage=99.5;
printf(" Marks= %d \n", s1.marks);
float percentage;
printf(" Grade= %c \n", s1.grade);
}; printf("Percentage = %c \n", s1.percentage);
OUTPUT: }
Marks=29
Grade= A
Percentage =99.5
Example: C program to show how union behaves
#include <stdio.h>
union student void main()
{ {
union student s1;
int marks;
char grade; s1.marks=29;
float percentage; printf(" Marks= %d \n", s1.marks);
};
s1.grade=‟A‟;
printf(" Grade= %c \n", s1.grade);
OUTPUT:
s1.percentage=99.5;
Marks=29 printf("Percentage = %c \n", s1.percentage);
Grade= A }
Percentage =99.5

Differences between structure and union in C:

C Structure C Union
keyword struct is used to define a structure keyword union is used to define a union
Structure allocates storage space for all its Union allocates one common storage space for
members separately. all its members. Union finds that which of its
member needs high storage space over other
members and allocates that much space.
Structure occupies larger memory space. Union occupies lower memory space over
structure.
We can access all members of structure at a time. We can access only one member of union at a
time.
Address of each member will be in ascending Address is same for all union members.
order (different address).
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 24
DATASTRUCTURE AND APPLICATIONS

5. Pointers and Dynamic Memory Allocation Functions


5.1 Pointers
Pointer is the important feature available in C.
As we store data/information in memory, computer stores data in its memory.
1. Computer memory is divided into number of cells called memory locations.Each
locationholds 1byte of data.
2. Each location is associated with address. Address of memory location ranges from 0 to
65535.
3. We can‟t change these addresses assigned by the system & hence these are constant. But we
can only use them to store the data.
These addresses are called pointer constants.
Data
Address
0 10
1 20
2 30
3 40
4 50 Memory
5 … locations
... …
65534 …
65535 …

Definition
“A pointer is a variable which contains the address of another variable as its value”.

Declaring a Pointer Variable


Syntax
data_type *pointer_variable_name;
where,
data_type: It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
pointer_variable_name: It is the name of the pointer variable.

Example:
1. int *ptr; // declares a pointer variable ptr of integer type.
2. float *temp; // declares a pointer variable temp of floating type.

Operators used with Pointers:


The two basic operators used with pointers are:
i. The Address of operator (&): By using the address of (&) operator, we can determine the
address of the variable.
ii. The Indirection operator (*): It gives the value stored at a particular address.
Example:
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 25
DATASTRUCTURE AND APPLICATIONS

int a=3;
int *ptr;
ptr=&a;
ptr a
Memory layout:
65530 3
Address: 65530
 „ptr = &a‟ copies the address of „a‟ to the pointer variable „ptr‟.

Example Program: Write a C program to print value and address of the variable using
pointers.
#include<stdio.h>
#include<conio.h>
void main () Output:
{ The address of a=65530 and value of a=20
int a=20, *ptr1;
clrscr ();
ptr1 = &a; //ptr1 is a pointer to variable a
printf(“The address of a=%d and value of a=%d\n”,ptr1,*ptr1);
getch();
}
ptr1 a
Memory layout:
65530 20
Address: 65530
Initializing a Pointer Variable
 We can initialize the pointer variables by assigning the address of other variable to them.
However these variables must be declared in the program.

Syntax
data_type *pointer_variable_name = address_of_variable;
where,
data_type:. It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
pointer_variable_name: It is the name of the pointer variable.
address_of_variable: It is the address of another variable.

Example:
1. int a;
int *ptr;
ptr=&a;
or
int a;
int *ptr=&a;
Both are equivalent.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 26


DATASTRUCTURE AND APPLICATIONS

5.2 Dynamic Memory Allocation Methods


 Memory can be reserved for the variables either during compilation time or during
execution time (run time).
 Based on this memory can be allocated for variables using two different techniques:
1. Static allocation
2. Dynamic allocation

Static Allocation
 If memory space is allocated for variables during compilation time, then it is called „Static
Memory allocation‟.
 Size of memory space is „fixed‟; it can‟t be altered during execution time.
Example: int a [10];
 During compilation, the compiler will allocate 10 memory locations for the array variable „a‟.
Inserting less than 10 elements leads to underutilization of allocated space and more than 10
elements cannot be inserted.

Dynamic Allocation
 “Dynamic memory allocation is the process of allocating memory space during the
execution time (Run time).”
 The various predefined memory management functions that are used to allocate or deallocate
memory are:
1. malloc( )
2. calloc( )
3. realloc( )
4. free( )

5.3 Different Dynamic Memory allocation Functions:


1. malloc( ): Allocating a Block of Memory
 This function reserves a block of memory of specified size and returns a pointer of data type to
that memory block.
 If there is insufficient memory to make the allocation, then it returns a NULL value.

Syntax:
ptr = (data_type *) malloc (size);
where,
ptr: ptr is a pointer variable of type int, float, char, double etc.
data_type: It can be any of the basic data type or user defined data type.
size: size is the number of bytes to be reserved for a block.

Example:
1. ptr = (int *) malloc(10);
 Allocates a block of Memory of 10 bytes.

2. ptr = (int *) malloc(20);


 Allocates a block of Memory of 20 bytes.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 27


DATASTRUCTURE AND APPLICATIONS

2. calloc( ): Allocating multiple Blocks of Memory


 This function allocates space for multiple Blocks, each of the same size and initializes all of its
bytes to zero (0).
 If there is insufficient memory to make the allocation, then it returns a NULL value.

Syntax:
ptr = (data_type *) calloc (n,size);
where,
ptr: ptr is a pointer variable of type int, float, char, double etc.
data_type: It can be any of the basic data type or user defined data type.
n: n is the number of blocks to be allocated.
size: size is the number of bytes in each block.

Example:
1. ptr = (int *) calloc(10, 2);
 Allocates 10 blocks of Memory each of 2 bytes.

2. ptr = (int *) calloc(5, 4);


 Allocates 5 blocks of Memory each of 4 bytes.

3. realloc( ): Reallocating already allocated block of Memory by malloc( ) or calloc( )


 This function reallocates memory space by modifying already allocated memory.
 The function relloc() resizes memory previously allocated by either malloc() or calloc(), which
means, the size of the memory changes by extending or deleting the allocated memory.
 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.

Syntax
ptr = (data_type *)realloc (ptr, new_size);

where,
ptr: it is a pointer to a block of previously allocated memory either using malloc( ) or calloc( ).
data_type: It can be any of the basic data type or user defined data type.
new_size: it is the new size of the block.

Example:
char *str;
str = (char *) malloc(10); // malloc function allocates 10 memory blocks
strcpy(str, “Computer”);
str = (char *) realloc (str, 40); //realloc function allocates new memory blocks from 10 to 40
strcpy(str, “Computer Science and Engineering”);

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 28


DATASTRUCTURE AND APPLICATIONS

4. free( ): Freeing the memory allocated using malloc( ), calloc( ) or realloc( )


 This function is used to free the previously allocated memory using malloc( ), calloc( ) or
realloc( ).
Syntax
free(ptr);

Ex:int *ptr;
ptr = (int *) malloc(100*sizeof(int));
free(ptr);

Difference between Static Memory Allocation and Dynamic Memory allocation in C:


Static memory allocation Dynamic memory allocation
In static memory allocation, memory is In dynamic memory allocation, memory is
allocated while writing the C program. allocated while executing the program.
Compile time allocation. Run time allocation.
Memory size can‟t be modified while Memory size can be modified while
execution. execution.
Example: Array Example: Linked list

Difference between malloc() and calloc() Functions in C:


malloc() calloc()
It allocates only single block of memory. It allocates multiple blocks of memory.
int *ptr; int *ptr;
ptr = malloc( 20 * sizeof(int)); ptr = calloc( 20, 20 *sizeof(int) );
For the above, 20*4 bytes of memory are For the above, 20blocks of memory will be
allocated in one block.Total = 80 bytes allocated and each contains20*4 bytes of
memory.Total = 1600 bytes.
malloc() doesn‟t initializes the allocated calloc() initializes the allocated memory to
memory. It contains garbage values zero.

5.4 Polynomials
 “A polynomial is a sum of terms, where each term has a form axe, where x is the variable, a is
the coefficient and e is the exponent.”
Example polynomials are:
A(x) =3x20 + 2x5 + 4
B(x) =x4 + 10x3 + 3x2 +1
 The largest (or leading) exponent of a polynomial is called its degree. Coefficients that are zero are
not displayed. The term with exponent equal to zero does not show the variable since x raised to a
power of zero is 1.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 29


DATASTRUCTURE AND APPLICATIONS

Polynomial operations
1. Representation
2. Addition
3. Multiplication

5.4.1 Polynomial Representation


Consider the two polynomials
A(x) = 2xl000+ 1
B(x) = x4 + 10x3 + 3x2 + 1

 The above figure shows how these polynomials are stored in the array terms.
 The index of the first term of A and B is given by startA and startB, while finishA and finishB give
the index of the last term of A and B.
 The index of the next free location in the array is given by avail.
 For above example, startA=0, finishA=1, startB=2, finishB=5, & avail=6.

5.4.2 Polynomial Addition


 C function is written that adds two polynomials, A and B to obtain D =A + B.
 To produce D (x), padd( ) is used to add A (x) and B (x) term by term. Starting at position avail,
attach( ) which places the terms of D into the array, terms.
 If there is not enough space in terms to accommodate D, an error message is printed to the standard
error device & exits the program with an error condition.

Function to add two polynomials:


Void padd (intstartA, intfinishA, intstartB, intfinishB, int *startD,int *finishD)
{
/* add A(x) and B(x) to obtain D(x) */

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 30


DATASTRUCTURE AND APPLICATIONS

float coefficient;
*startD = avail;
while (startA <= finishA && startB <= finishB)

switch(COMPARE(terms[startA].expon, terms[startB].expon))
{
case -1: /* a expon< b expon */
attach (terms [startB].coef, terms[startB].expon);
startB++;
break;
case 0: /* equal exponents */
coefficient = terms[startA].coef + terms[startB].coef;
if (coefficient)
attach (coefficient, terms[startA].expon);
startA++;
startB++;
break;
case 1: /* a expon> b expon */
attach (terms [startA].coef, terms[startA].expon);
startA++;
}
/* add in remaining terms of A(x) */
for(; startA <= finishA; startA++)
attach (terms[startA].coef, terms[startA].expon);

/* add in remaining terms of B(x) */


for( ; startB <= finishB; startB++)
attach (terms[startB].coef, terms[startB].expon);

*finishD = avail-i;
}
Example: A(x) = 2xl000+ 2x2 +1 and B(x) = x4 + 10x3 + 3x2 + 1 find D=A+B
Addition of 2 polynomial D = 2xl000+ x4 + 10x3 + 5x2 +2
Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 31
DATASTRUCTURE AND APPLICATIONS

5.5 Sparse Matrices


 A matrix which contains many zero entries or very few non-zero entries is called as sparse
matrix.
 A matrix contains m rows and n columns of elements as illustrated in below figures. In this figure,
the elements are numbers. The first matrix has five rows and three columns and the second has six
rows and six columns. We write m x n (read "m by n") to designate a matrix with m rows and n
columns. The total number of elements in such a matrix is mn. If m equals n, the matrix is square.
 In the figure B, matrix contains only 8 non-zero elements out of 36elements and that is sparse
matrix.

Note: A sparse matrix can be represented in 1-Dimension, 2- Dimension and 3- Dimensional array.
When a sparse matrix is represented as a two-dimensional array as shown in Figure B, more space is
wasted.
Example: Consider the space requirements necessary to store a 1000 x 1000 matrix that has only
2000 non-zero elements. The corresponding two-dimensional array requires space for 1,000,000
elements. The better choice is by using a representation in which only the nonzero elements are
stored.

5.5.1 Sparse Matrix Representation


 An element within a matrix can characterize by using the triplet<row,col,value>.This means that,
an array of triples is used to represent a sparse matrix.
 Organize the triples so that the row indices are in ascending order.
 In order to terminate the operations, we must know the number of rows and columns, and the
number of nonzero elements in the matrix.
Example 1:
 Below figures shows the sparse matrix representation of a matrix using triplet<row,col,value>
 The below figure shows the representation of matrix in the array “a” a[0].row contains the number of
rows, a[0].col contains the number of columns and a[0].value contains the total number of nonzero
entries.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 32


DATASTRUCTURE AND APPLICATIONS

 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. The triples are ordered
by row and within rows by columns.
Row Col Value

Sparse matrix stored as triplet.


 0th index of array a contains number of rows, number of columns and number of values.
Example 2: For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values.
This matrix canbe represented as shown in the figure below:

5.5.2 Transposing a Matrix


 To transpose a matrix, interchange the rows and columns. This means that each element a[i][j]
in the original matrix becomes element a[j][i] in the transpose matrix.

a. Spare matrix stored as triplet and b. transpose of spare matrix stored as triplet

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 33


DATASTRUCTURE AND APPLICATIONS

QUESTION BANK

1. Define Data structures. Classify the data structures.


2. List & explain the operations of data structures.
3. What is array? How arrays are declared and initialized in C.
4. Define structure? Explain how structures are declared and initialized.(explain various
operations that can be performed on structures.)
5. Write a note on structure with in structures with an example.
6. What are self-referential structures? Explain with example.
7. Write a C program with an appropriate structure definition & variable declaration to read &
display information about 5 employees using nested structures. Consider the following fields
like Ename,Empid,DOJ(Date,Month,Year) & Salary(Basic,DA,HRA).
8. Define union. Explain how unions are declared and initialized.
9. Differentiate between unions & structures.
10. Define pointer. What are the advantages & Disadvantages of using pointers?
11. Define dynamic memory allocation. List and explain the functions supported in C for
dynamic memory allocation with examples.
12. Explain the representation of multidimensional arrays.
13. Explain the operations of array with example.(i.e. insertion, deletion and display)
14. Write a C function to sort integers using selection sort algorithm& Bubble sort algorithm.
15. What is a polynomial? What is the degree of polynomial? Write a function to add two
polynomials.
16. Consider 2 polynomials A(x)=2x1000+1 and B(x)=x4+10x3+3x2+1 with a diagram show how
these polynomials are stored in 1-D array. Also give its C representation.
17. Define sparse matrix. Explain declaration of sparse matrix with example.
18. Give ADT of sparse matrix & show with a suitable example sparse matrix representation
storing as triples. Give a sample transpose function to transpose sparse matrix.
19. Express the given sparse matrix as triplets & find its transpose

10 0 0 25 0
0 23 0 0 45
0 0 0 0 32
42 0 0 31 0
A=
0 0 0 0 0
0 0 30 0 0

20. Write the Knuth Morris Pratt pattern matching algorithm & apply the same to search the
pattern „abcdabcy‟ in the text „abcxabcdabxabcdabcdabcy‟.

Prof. SHRIKANT PUJAR, Dept. of CS&E,JIT-DVG 34


DATA STRUCTURES AND APPLICATIONS
1. RECURSION:
 “The process in which a function calls itself again and again is called as Recursion”.
 A function is recursive if a statement in the body of the function calls itself. Recursion is the
process of defining something in terms of itself. For a computer language to be recursive, a
function must be able to call itself.

1.1 Factorial of a number:


 let us consider the function fact() shown below, which computers the factorial of an integer.

main()
{ int fact(int n)
int n,res; {
printf (“Enter a positive integer value: "); if (n == 0)
return 1;
scanf (“%d”, &n);
else
res = fact(n); return n * fact (n-1);
printf ("\n Factorial of %d =%5d\n", n, res); }
}
 A non-recursive or iterative version for finding the factorial is as follows:
main()
{
int n,res=1;
printf (“Enter a positive integer value: ");
scanf (“%d”, &n);
for (i=1; i<=n; i++)
{
res = res * i;
}
printf ("\n Factorial of %d =%5d\n", n, res);
}
 The operation of the non-recursive version is clear as it uses a loop starting at 1 and ending at the
target value and progressively multiplies each number by the moving product. When a function calls
itself, new local variables and parameters are allocated storage on the stack and the function code is
executed with these new variables from the start. A recursive call does not make a new copy of the
function.
 Only the arguments and variables are new. As each recursive call returns, the old local variables and
parameters are removed from the stack and execution resumes at the point of the function call inside
the function.
 When writing recursive functions, there must be an exit condition somewhere to force the function to
return without the recursive call being executed. If there is no exit condition, the recursive function
will loop forever until you run out of stack space and indicate error about lack of memory, or stack
overflow.

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS
Differences between recursion and iteration:

1.2 Greatest Common Divisor (GCD): The GCD of two given numbers is the largest integer that
divides both of them. GCD is defined only for positive integers not for negative integers and floating
numbers.
#include<stdio.h>
int gcd(int a,int b);
void main()
{ int gcd(int a,int b)
int a,b,res; {
printf(“Enter two numbers”); if (n ==0)
scanf(“%d%d”,&m,&n); return m;
res=gcd(m,n); return gcd(n,m%n);
printf(“GCD of %d and %d is %d”,m,n,res); }
}

1.3 Fibonacci Sequence: The Fibonacci numbers are a series of numbers such that each number is
the sum of the previous two numbers.
 A Fibonacci sequence starts with the integers 0 and 1. Successive elements in this sequence are
obtained by summing the preceding two elements in the sequence.
 For example, third number in the sequence is 0 + 1 = 1, fourth number is 1 + 1= 2, fifth number is 1
+ 2 = 3 and so on. The sequence of Fibonacci integers is given below:

0 1 1 2 3 5 8 13 21 . . . . . . . . .

 A recursive definition for the Fibonacci sequence of integers may be defined as follows:
Fib (n) = n if n = 0 or n = 1
Fib (n) = fib (n-1) + fib (n-2) for n >=2
Prof. Shrikant Pujar, Dept. of CS&E 2
DATA STRUCTURES AND APPLICATIONS
We will now use the definition to compute
fib(5): fib(5) = fib(4) + fib(3)
=fib(3) + fib(2) + fib(3)
=fib(2) + fib(1) + fib(2) + fib(3)
=fib(1) + fib(0) + fib(1) + fib(2) + fib(3)
=1 + 0 + 1 + fib(1) + fib(0) + fib(3)
=1 + 0 + 1 + 1 + 0 + fib(2) + fib(1)
=1 + 0 + 1 + 1 + 0 + fib(1) + fib(0) + fib(1)
=1 + 0 + 1 + 1 + 0 + 1 + 0 + 1 = 5
fib(2) is computed 3 times, and fib(3),is computed 2 times in the above calculations. The values of
fib(2) or fib(3) are saved and reused whenever needed

 A recursive function to compute the Fibonacci number in the nth position is given below:
main()
fib (int n)
{
int n,res; {
printf (“Enter a positive integer value: "); if (n==0 | | n==1)
scanf (“%d”, &n); return n;
printf (“ fib(%d) is %d”, n, fib(n)); return fib(n-1) + fib(n-2);
} }

1.4 The Towers of Hanoi


 In the game of Towers of Hanoi, there are three towers labeled 1, 2, and 3. The game starts with n
disks on tower A. For simplicity, let n is 3. The disks are numbered from 1 to 3, and without loss of
generality we may assume that the diameter of each disk is the same as its number. That is, disk 1
has diameter 1 (in some unit of measure), disk 2 has diameter 2, and disk 3 has diameter 3. All three
disks start on tower A in the order 1, 2, 3.
 The objective of the game is to move all the disks in tower 1 to entire tower 3 using tower 2.
That is, at no time can a larger disk be placed on a smaller disk.
 The RULES to be followed in moving the disks from tower 1 tower 3 using tower 2 are as follows:
• Only one disk can be moved at a time.
• Only the top disc on any tower can be moved to any other tower.
• A larger disk cannot be placed on a smaller disk.
Example 1: Towers of Hanoi problem for n = 3

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS
1. Move top disk from peg A to peg C.
2. Move top disk from peg A to peg B.
3. Move top disk from peg C to peg B.
4. Move top disk from peg A to peg C.
5. Move top disk from peg B to peg A.
6. Move top disk from peg B to peg C.
7. Move top disk from peg A to peg C.
 The program that uses recursion to produce a list of moves that shows how to accomplish the task of
transferring the n disks from tower 1 to tower 3 is as follows.
#include<stdio.h>
void towers(int n, char source, char temp, char dest)
{ void main()
if(n==0) {
return; int n;
towers(n-1, source, dest, temp); printf("Enter the number of disks:");
printf("move disk %d from peg %c to peg %c\n",n,source,dest); scanf("%d",&n);
towers(n-1,temp,source,dest); towers(n,'A','C','B');
} }

1.5 Ackerman’s Function:


 The Ackermann function is a function with two arguments each of which can be assigned any
nonnegative integer: 0, 1, 2, ....

Example: Find the value of A (1,2) using Ackermann Functions.

#include<stdio.h> int A(int m,int n,)


main() {
{ int m,n; if(m = = 0)
printf("Enter the value for m & n : "); return(n+1);
scanf("%d%d",&m,&n); else if ( m>0 && n = = 0)
return A(m-1,1);
res= A(m,n)
else
rintf("The value is : %d\n", res); return A(m-1,A(m,n-1));
} }

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS
2. QUEUES:
 “A queue is an ordered list in which insertions (additions, pushes) and deletions (removals and
pops) take place at different ends.” The end at which new elements are added is called the rear
end, and that from which old elements are deleted is called the front end.
 Ex: people waiting in a line at ticket counter in theatre. The first person in queue is the first person to
take ticket. Whenever new person comes he joins at end of the queue.
 If the elements are inserted A, B, C, D and E in this order, then A is the first element deleted from
the queue. Since the first element inserted into a queue is the first element removed, queues are also
known as First-In-First-Out (FIFO) lists.
f=front end r=rear end (when queue is empty f=0 & r=-1)

ADT Queue.
1. Queue CreateQ(maxQueueSize) ::=
#define MAX_QUEUE_SIZE 100
typedef struct
{
int ele;
} element;

element queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = 0;
2. Boolean IsEmptyQ(queue) ::= f=r+1
3. Boolean IsFullQ(queue) ::= r =N-1 (r == MAX_QUEUE_SIZE-1)
4. void addq(ele) ::= void addq(element ele)
{
if (r == MAX_QUEUE_SIZE-1)
Prof. Shrikant Pujar, Dept. of CS&E 5
DATA STRUCTURES AND APPLICATIONS
queueFull();
queue [++rear] = item;
}
5. element deleteq( ) ::= element deleteq()
{
if (r==-1)
return queueEmpty( );
return queue[++front];
}

6. queueFull( ) ::= void queueFull()


{
fprintf(stderr, "Queue is full, cannot add element");
exit(EXIT_FAILURE);

The queueFull function which prints an error message and terminates execution

2.1 Type of queues.


1. Linear Queue
2. Circular queue
3. Double ended queue
4. Priority queue

2.1.1 Linear Queue


 Linear Queue is a linear data structure, where elements are inserted from one end elements are
deleted from another end. insertion of element is from rare end and deletion is from front end.
Array Representation of Queue
 In Array implementation FRONT pointer initialized with 0 and REAR initialized with -1.Consider
the implementation: - If there are 5 items in a Queue.

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS
Note: In case of empty queue, front is one position ahead of rear : f = r + 1;.This is the queue
underflow condition. The queue is full when r =N-1.This is the queue overflow condition.

Operations/Implementation of queue using arrays:


1. Insert into queue: inserting new item at the rare end.
Step 1: Before inserting the item into queue, first we have to check is there any memory space
present in queue are not.
if( rare = = queue_size -1)
{
Printf(“Queue is Full”);
}
Step 2: if the above condition gets failed then we can insert the new item into queue, before
inserting an item we have to increment rare by one. Initially front=0 and rare =-1.
rare = rare + 1;
Now the item can be inserted at rare position.
Q[rare] = item;

2. Delete from queue: deleting an element from front end.


Step 1: Before deleting the item from queue, first we have to check is there any items present in
queue are not if queue is empty no item to delete. Front = 0 and rare = -1.
if( front > rare )
{
Printf(“Queue is Empty”);
}
Step 2: if the above condition gets failed then we can delete the item from queue by incrementing
front by one.
front = front+1;

3. Display Queue items: displaying the content of queue from front end to rare end.
for( i= front; i <= rare; i++)
{
Printf(“%d\t”, q[i]); // A B C D E
}

C Program to implement Queue operations.


#include<stdio.h>
# define MAX 5
int Q[MAX], front=0, rear=-1,i,ele,ch;

void main()
{
while(1)
{
printf("press 1 for insertion\n");
Prof. Shrikant Pujar, Dept. of CS&E 7
DATA STRUCTURES AND APPLICATIONS
printf("press 2 for deletion\n");
printf("press 3 for display\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insertQ();
break;
case 2: deleteQ();
break;
case 3: displayQ();
break;
default: printf(invalid choice\n");
}
}
}
void insertQ()
{
if(rear == MAX-1)
{
printf("\n Linear Queue is full");
}
printf("\n Enter element to be inserted: ");
scanf("%d", & ele);
rare = rare + 1;
q[rear] = ele;
}
void deleteQ()
{
if( front>rear) //OR front=rear +1
{
printf("\n\n Queue is Empty..");
}
printf("\n Deleted element from Queue is %d", Q[front]);
front = front + 1;
}
void displayQ()
{
if(front >rear)
{
printf("\n\n\t Queue is Empty");
}
printf("\n Elements in Queue are: ");
for(i = front; i < =rear; i++)
{
printf("%d\t", Q[i]);
}
}

Prof. Shrikant Pujar, Dept. of CS&E 8


DATA STRUCTURES AND APPLICATIONS
Drawback of Linear Queue:
 When item enters and deleted from the queue, the queue gradually shifts to the right as shown in
figure.

 In this above situation, when we try to insert another item, which shows that the queue is full. This
means that the rear index equals to MAX_QUEUE_SIZE -1. But even if the space is available at the
front end, rear insertion cannot be done.

2.1.2 Circular queue


 A circular queue is linear data structure that contains a collection of data and rare end of queue
is followed by front end of queue. Addition of data at the end of the queue and removal of data at
the beginning of the queue.
 Circular queues have a fixed size. Circular queue follows FIFO principle. Queue items are added at
the rear end and the items are deleted at front end of the circular queue.

Operations of Circular queue:


1. insert(): This function is used to insert an element into the circular queue. In a circular queue, the
new element is always inserted at Rear position.
Step 1: Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)).
Step 2: If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1
&& front != 0) if it is true then set rear=0 and insert element.

2. delete(): This function is used to delete an element from the circular queue. In a circular queue,
the element is always deleted from front position.
Step 1: Check whether queue is Empty means check (front==-1). If it is empty then display Queue is
empty. If queue is not empty then step 2.
Step 2: Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is
true then set front=0 and return the element.

Example:
 To add an element, increment rear one position clockwise and insert at the new position. Here the
MAX_QUEUE_SIZE is 8 and if all 8 elements are added into queue and that can be represented in
below figure (a).
rare = (rear +1) % MAX_QUEUE_SIZE
 To delete an element, increment front one position clockwise. The element A is deleted from
queue and if we perform 6 deletions from the queue of Figure (b) in this fashion, then queue
becomes empty and that front =rear.
front = (front+1)% MAX_QUEUE_SIZE;

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS
 If the element I is added into the queue as in figure (c), then rear needs to increment by 1 and the
value of rear is 8. Since queue is circular, the next position should be 0 instead of 8.
rare = (rear +1) % MAX_QUEUE_SIZE

Addition causes the increment in REAR.


It means that when REAR reaches N-1 position then Increment in REAR causes REAR to reach at
first position that is 0.
rear = ( rear + 1) % N;
Deletion causes the increment in FRONT.
It means that when FRONT reaches the N-1 position, then increment in FRONT, causes FRONT to
reach at first position that is 0.
front = ( front + 1) % N;

C Program to implement Circular Queue operations.


#include<stdio.h>
#define MAXSIZE 5
int rear=-1,front=0, item, count=0,ch,i,j;
char q[MAXSIZE],item;
void main()
{
while(1){
printf(" press 1 for insert\n");
printf(" press 2 for delete\n");
printf(" press 3 for display\n");
printf(" press 4 for exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{ case 1: insert();
break;
case 2: del();
break;
case 3:display();
break;

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS
case 4:exit(0);
default: printf("invalid choice\n");
}
}
}
void insert()
{
if(count==MAXSIZE)
{
printf("queue is full\n");
}
else
{ printf("enter the element to be inserted into the queue\n");
scanf(" %c",&item);
rear=(rear+1)%MAXSIZE;
q[rear]=item;
count++;
}
}
void del()
{
if(count==0)
{
printf("the queue is empty\n");
}
else
{ item=q[front];
front=(front+1)%MAXSIZE;
count - -;
printf("the deleted element is %c\n",item);
}
}
void display()
{
if(count==0)
{
printf("\nthe queue is empty\n");
}
else
{ printf("the queue elements are\n");
j=count;
for(i=front;j!=0;j--)
{
printf("%c\t",q[i]);
i=(i+1)%MAXSIZE;
}
}
}

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS
circular queues using dynamic arrays.
A dynamically allocated array is used to hold the queue elements. Let capacity be the number of
positions in the array queue.
To add an element to a full queue, first increase the size of this array using a function realloc. As
with dynamically allocated stacks, array doubling is used.
Consider the full queue of figure (a). This figure shows a queue with seven elements in an array
whose capacity is 8. A circular queue is flatten out the array as in Figure (b).

Figure (c) shows the array after array doubling by realloc().

To get a proper circular queue configuration, slide the elements in the right segment (i.e., elements A
and B) to the right end of the array as in figure (d)

To obtain the configuration as shown in figure (e), follow the steps


1) Create a new array newQueue of twice the capacity.
2) Copy the second segment (i.e., the elements queue [front +1] through queue [capacity-1]) to
positions in newQueue beginning at 0.
3) Copy the first segment (i.e., the elements queue [0] through queue [rear]) to positions in
newQueue beginning at capacity – front – 1.

Program gives the code to add to a circular queue using a dynamically allocated array.

Prof. Shrikant Pujar, Dept. of CS&E 12


DATA STRUCTURES AND APPLICATIONS
void addq( element item)
{
rear = (rear +1) % capacity;
if(front == rear)
queueFull( ); /* double capacity */
queue[rear] = item;
}
Program obtains the configuration of figure (e) and gives the code for queueFull. The function
copy (a,b,c) copies elements from locations a through b-1 to locations beginning at c.
void queueFull( )
{ /* allocate an array with twice the capacity */
element *newQueue;
MALLOC(newQueue, 2 * capacity * sizeof(* queue)); /* copy from queue to newQueue */
int start = ( front + ) % capacity;
if ( start < 2) /* no wrap around */
copy( queue+start, queue+start+capacity-1,newQueue);
else
{ /* queue wrap around */
copy(queue, queue+capacity, newQueue);
copy(queue, queue+rear+1, newQueue+capacity-start);
}
front = 2*capacity – 1; /* switch to newQueue*/
rear = capacity – 2;
capacity * =2; free(queue);
queue= newQueue; }

2.1.3 Double ended queue (deque)


“Double Ended Queue is special type of data structure in which the insertion at both front and
rear positions and can deletion from both front and rear positions”.

Operations on deque:
1. Insert an item from front end
2. Insert an item from rare end
3. Delete an item from front end
4. Delete an item from rear end
5. Display the content of queue

Prof. Shrikant Pujar, Dept. of CS&E 13


DATA STRUCTURES AND APPLICATIONS
1. Insert an item from front end:
Step 1: Queue empty – when queue is empty, an item can be inserted at the front end first by
incremented rare by 1 and then insert an item.
if( front==0 && rare==-1 ) Before insert:
{
rare = rare +1;
q[rare]=item; After insert:
}
Step 2: if some item are present in queue, we can insert an item by decreasing the front index f by 1.
if (front !=0) 0 1 2 3 4
{
C D
front = front -1; Before insert:
q[front] = item; f r
} After insert: B C D
f r
2. Insert an item from rare end:
Step 1: Before inserting the item into queue, first we have to check is there any memory space
present in queue are not.
if( rare = = queue_size -1)
{
Printf(“Queue is Full”);
}
Step 2: if the above condition gets failed then we can insert the new item into queue, before
inserting an item we have to increment rare by one. Initially front=0 and rare =-1.
rare = rare + 1;
Now the item can be inserted at rare position.
Q[rare] = item;

3. Deleting an element from front end:


Step 1: Before deleting the item from queue, first we have to check is there any items present in
queue are not if queue is empty no item to delete. Front = 0 and rare = -1.
if( front > rare )
{
Printf(“Queue is Empty”);
}
Step 2: if the above condition gets failed then we can delete the item from queue by incrementing
front by one.
front = front+1;

4. Delete an item from rear end: if some item are present in queue, we can delete an item by
decreasing the front index r by 1.

Prof. Shrikant Pujar, Dept. of CS&E 14


DATA STRUCTURES AND APPLICATIONS
if (front > rear) 0 1 2 3 4
return -1; //before delete
else B C D
item = q[rare] f r
rare = rare -1; //After delete: B C

f r

Example: C Program to demonstrate Doubly ended queue.


#include<stdio.h>
#define N 5
int dq[N], front=0, rear=-1, count=0;

void insertrear() //insert element at the rear end


{
int key;
if (count==N)
printf("overflow\n");
else
{
printf("enter the key to be inserted\n");
scanf("%d",&key);
rear=(rear+1)%N;
dq[rear]=key;
count++;
}
}

void insertfront() //insert element at the front


{
int key;
if (count==N)
printf("overflow\n");
else
{
printf("enter the key elemet\n");
scanf("%d",&key);
if (front==0)
front=N-1;
else
front=front-1;
dq[front]=key;
count++;
}
}
void deletefront() //delete element from the front
{
if (count==0)
printf("underflow\n");
else

Prof. Shrikant Pujar, Dept. of CS&E 15


DATA STRUCTURES AND APPLICATIONS
{
printf("element deleted is%d",dq[front]);
front=(front+1)%N;
count--;
}
}

void deleterear()
{
if (count==0)
printf("underflow\n");
else
{
printf("%d is rear value\n",rear);
printf("element deleted is %d",dq[rear]);
if (rear==0)
rear=N-1;
else
rear=rear-1;
count--;
}
}

void display()
{
int i,k;
if (count==0)
printf("empty queue\n");
else
{
k=front;
for(i=0;i<count;i++)
{
printf("%d\t",dq[k]);
k=(k+1)%N;
}
}
}

Deque is a variation of queue data structure, pronounced “deck”, which stands for doubleended queue.
In a deque values can be inserted at either the front or the back, A collection of peas in a straw is a
good example.

Queues and deques are used in a number of ways in computer applications. A printer, for example, can
only print one job at a time. During the time it is printing there may be many different requests for other
output to be printed. To handle this printer will maintain a queue of pending print tasks. Since you want
the results to be produced in the order that they are received, a queue is the appropriate data structure.

Prof. Shrikant Pujar, Dept. of CS&E 16


DATA STRUCTURES AND APPLICATIONS
2.1.4 Priority Queue: A queue in which we are able to insert items or remove items from any position
based on some priority is called as priority queue. In priority queue each element has been assigned a
priority and such that the order in which elements are deleted and processed comes from the following
rules:
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with same priority are processed according to the order in which they were added to the
queue.

Types of priority queue:


1. Ascending Order: in ascending order priority queue elements can be inserted in any order, but while
deleting an element from queue only the smallest element is removed first.
2. Descending Order: in descending priority also elements can be inserted in any order, but while
deleting an element from queue only the largest element is removed first.

Implementation of a Priority Queue


1. Insert_rare(): which inserts the item at the end of the queue, insert an item such that that items
are arranged in ascending order.
While (j>=0 && item<q[j]) //insert an item base on priority
{ q[j+1] = q[j]; // move the item at q[j] to next position
j--;
}
q[j+1] = item; // insert an item at appropriate position

2. Delete_front(): which returns the smallest item from the queue front.
3. Display (): which displays the contents of queue.

#include <stdio.h>
#define MAX 5
int pri_que[MAX];
int front=0, rear=-1,i,j;
void insert_by_priority(int data) /* Function to insert value into priority queue */
{
if (rear== MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if (rear == -1))
{
rear++;
pri_que[rear] = data;
}
else

Prof. Shrikant Pujar, Dept. of CS&E 17


DATA STRUCTURES AND APPLICATIONS
check(data);
rear++;
}
void check(int data) //* Function to check priority and place element */
{
for (i = 0; i <= rear; i++)
{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}
void delete_by_priority(int data) /* Function to delete an element from queue */
{
if (rear==-1)
{
printf("\nQueue is empty no elements to delete");
return;
}
printf( “The element deleted is %d”,pri_que[front]);
front++;
}

void display_pqueue() /* Function to display queue elements */


{
if (rear == -1)
{
printf("\nQueue is empty");
return;
}
for (i=front; i <= rear; i++)
{
printf(" %d ", pri_que[i]);
}
}

Prof. Shrikant Pujar, Dept. of CS&E 18


DATA STRUCTURES AND APPLICATIONS
2.3 Applications of Queue
1. It is used to schedule the jobs to be processed by the CPU.
2. When multiple users send print jobs to a printer, each printing job is kept in the printing queue.
Then
the printer prints those jobs according to first in first out (FIFO) basis.
3. Breadth first search uses a queue data structure to find an element from a graph.

2.4 Multiple stacks


 A sequential representation of a single stack using array is simple since only the top of the stack
needs to be maintained and kept track. A linear structure like array can be used to represent multiple
stacks. If multiple stacks are to be implemented, the array can be approximately divided into equal
sized segments, each segment denoting a stack. The top and bottom of each stack are to be kept track
of to manage insertions and deletions into the individual stacks.
 Consider a set of N stacks to be implemented using an array. The array can be divided into N equal
sized segments. Say if the array can hold 20 elements, and 4 stacks are to be implemented then each
individual stack hold 5 elements.

 If i denotes an individual stack ,to establish multiple stacks, an array of top(tos[ i])and bottom
pointers (b[i])are maintained to keep track of the top and bottom of every stack.
 Every stack„s bottom and top pointer is set to B[i]=tos[i]=(size/n)*i-1 which enables dividing the
stack to be divided into equal sized segments.

Overflow in any stack


tos[i]=b[i+1] // top pointer of one stack points to the bottom position of the following stack

Underflow in any stack


tos[i]=b[i] //top and bottom pointer of a stack in the same position

Implementation of multiple stacks using array


#define memsize 20 //size of array
#define maxstack 4 //number of stacks
int s[memsize],tos[maxstack],b[maxstack],n;
int main()
{
int i;

Prof. Shrikant Pujar, Dept. of CS&E 19


DATA STRUCTURES AND APPLICATIONS
scanf(“%d”,&n); //number of stacks
for(i=0;i<n;++)
tos[i]=b[i]=(memsize/n)*i-1;
b[n]=memsize-1; // use switch case to call the different operations push, pop and display
}
void push()
{
int ele; scanf(“%d”,&i); //stack number on which operation is to be done
if (tos[i]==b[i+1])
{
printf(“stack %d is full”, i);
return;
}
printf(“enter the value to be inserted\n”);
scanf(“%d”,&ele);
s[++tos[i]]=ele;
}
void pop()
{
printf(“enter the stack number\n”);
scanf(“%d”,&i);
if (tos[i]==b[i])
{
printf(“empty stack\n”);
return;
}
printf(“deleted element is %d”,s[tos[i]--];
}
void disp()
{
printf(“enter the stack number\n”);
scanf(“%d”,&i);
if (tos[i]==b[i])
{
printf(“empty stack\n”);
return;
}
printf(“contents are \n”);
for(j=b[i]+1;j<=tos[i];j++)
printf(“%d”,s[j]);
}

Prof. Shrikant Pujar, Dept. of CS&E 20


DATA STRUCTURES AND APPLICATIONS
2.5 A Mazing Problem.
 A Maze problem is finding a food (Maze) for a rat from entrance to exit (source to destination).
 A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e.,
maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1].
 Start from source and has to reach the destination. rat can move in all the 8 directions:

 In the maze matrix, 1 means the block is a dead end and 0 means the block can be used in the path
from source to destination.
Example:

Prof. Shrikant Pujar, Dept. of CS&E 21


DATA STRUCTURES AND APPLICATIONS

ASSIGNMENT QUESTIONS

1. Define stack. Write ADT for stack. Implement the operations push, pop & display using arrays &
implement Stack Full() & Stack Empty() functions.
2. Write an algorithm to implement a stack using dynamic array whose initial capacity is 1 and
array doubling is used to increase the stack‟s capacity whenever an element is added to a full
stack. Implement the operations push, pop & display.
3. What is expression? List and explain different types of expressions.
4. Write an algorithm and C function for (1) converting infix expression to postfix expression (2)
Evaluate a postfix expression.
5. Convert the following infix expression to postfix expression. And trace for the given data
1. ((a/(b-c+d))*(e-a)*c) where a=6,b=3,c=1,d=2,e=4.
2. (a+b)*d+e/(f+a*d)+c
3. ((6+(3-2)*4) 5+7)
4. A$B$C*D
6. Evaluate the following postfix expression And trace for the given data
1. ab/c-de*+ac* where a=6,b=3,c=1,d=2,e=4.
2. abc+*de/- where a=5,b=6,c=2,d=12,e=4.
7. Define recursion. Write the recursive program for:
(1) factorial of a number (2) tower of Hanoi.
8. Write an algorithm for tower of Hanoi. Trace the same with an example.
9. Write an algorithm for Ackerman‟s function. Evaluate A(1,2) using Ackerman‟s function.
10. Define queues. Implement QInsert and Qdelete functions for queues using arrays.
11. List the disadvantages of linear queue and explain how is it solved in circular queue. Give the
algorithm to implement a circular queue with suitable example.
12. What is double ended queue? Implement the operations of double ended queue.
13. What is priority queue? Explain the operations of priority queue.
14. Explain with example multiple stacks. Implement the program for multiple stacks.
15. Define Maze problem. Explain with example.

Prof. Shrikant Pujar, Dept. of CS&E 22


DATA STRUCTURES AND APPLICATIONS

Module -2
Stacks and Queues: Definition, Stack Operations, Array Representation of Stacks,
Stacks using Dynamic Arrays, Stack Applications: Polish notation, Infix to postfix conversion,
evaluation of postfix expression, Recursion - Factorial, GCD, Fibonacci Sequence, Tower of
Hanoi, Ackerman's function. Queues: Definition, Array Representation, Queue Operations,
Circular Queues, Circular queues using Dynamic arrays, Dequeues, Priority Queues, A Mazing
Problem. Multiple Stacks and Queues. Programming Examples.
.

Stacks and Queues


1. STACKS:
Definition
“A stack is an ordered list in which insertions (pushes) and deletions (pops) are made at one end
called the top.”

Given a stack S= (a0, ... ,an-1), where a0 is the bottom element, an-1 is the top element, and
ai is on top of element ai-1, 0 < i < n.

As shown in above figure, the elements are added in the stack in the order A, B, C, D, E, then
E is the first element that is deleted from the stack and the last element is deleted from stack is A.
Figure illustrates this sequence of operations.

Since the last element inserted into a stack is the first element removed, a stack is also known
as a Last-In-First-Out (LIFO) list.

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS

1.1 STACK OPERATIONS:


The two basic operations associated with stacks are:
1.Push(): is the term used to insert an element into a stack.
2.Pop(): is the term used to delete an element from a stack.
3.Display(): prints the contents of stack if not empty

1.Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_SIZE 100 /* maximum stack size*/
typedef struct
{
int item; /* other fields */
} element; ADT for stack
element stack[MAX_STACK_SIZE]; (Abstract Data Type)
int top = -1;
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;

Prof. Shrikant Pujar, Dept. of CS&E 2


DATA STRUCTURES AND APPLICATIONS

1.1.1 Push( )
Function push checks whether stack is full. If it is, it calls stackFull( ), which prints an error message
and terminates execution. When the stack is not full, increment top and assign item to stack [top].
void push()
{
if (top >= MAX_STACK_SIZE-1)
{
stackFull();
}
else
{
printf(“enter the element to be pushed on to stack\n”);
scanf(“%d”,&item);

top= top+1;
stack[top] = item;
}
}

1.1.2 Pop( )
Deleting an element from the stack is called pop operation. The element is deleted only from the top
of the stack and only one element is deleted at a time
void pop ( )
{
if (top == -1)
{
return stackEmpty();
}
else
{
item= stack[top];
top= top-1;

printf(“deleted element is %d\n”,item);


}
}

1.1.3 Display( )
void Display( )
{

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS

if (top == -1)
{
return stackEmpty();
}
else
{
printf(“elements on stack are\n”);
for(i=0;i>=top;i++)
{
printf(“%d\n”,stack[i]);
}
} // end of else
}

1.1.4 stackFull( )-The stackFull which prints an error message and terminates execution.

void stackFull()
{
fprintf(stderr, "Stack is full, cannot add element");
exit(0);
}

1.1.5 stackEmpty( )-The stackEmpty which prints an error message.

void stackEmpty
{
fprintf(stderr, "Stack is Empty cannot delete/display elements\n ");
exit(0);
}

1.2 STACKS USING DYNAMIC ARRAYS:


The array is used to implement stack, but the bound (MAX_STACK_SIZE) should be known during
compile time. The size of bound is impossible to alter during compilation hence this can be
overcome by using dynamically allocated array for the elements and then increasing the size of array
as needed.

1.2.1 Stack Operations using dynamic arrays:


1. Stack CreateS( )::=
typedef struct
{

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS

int item;
} element;
element *stack;
MALLOC(stack, sizeof(*stack));
int capacity= 1;
int top= -1;
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= capacity-1;
4. push()
Here the MAX_STACK_SIZE is replaced with capacity
void push(element item)
{
if (top >= capacity-1)
{
stackFull();
}
else
{
printf(“enter the element to be pushed on to stack\n”);
scanf(“%d”,&item);

top= top+1;
stack[top] = item;
}
}

5. pop( ) In this function, no changes are made.


void pop ( )
{
if (top == -1)
{
return stackEmpty();
}
else
{
item= stack[top];
top= top-1;

printf(“deleted element is %d\n”,item);

Prof. Shrikant Pujar, Dept. of CS&E 5


DATA STRUCTURES AND APPLICATIONS

}
}
6. stackFull( )
The new code shown below, attempts to increase the capacity of the array stack so that new element
can be added into the stack. Before increasing the capacity of an array, decide what the new capacity
should be. In array doubling, array capacity is doubled whenever it becomes necessary to increase
the capacity of an array.

void stackFull()
{
REALLOC (stack, 2*capacity*sizeof(*stack));
capacity *= 2;
}

1.3 APPLICATIONS OF STACK:


1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to convert an infix expression into postfix/prefix form.
3. Stack is used to evaluate a postfix expression.
4. Used In recursion, to solve the problems like,Tower of Hanoi,Factorial of a given number,GCD
of two numbers,Fibbacci sequence & Ackermans function etc
5. Used in other applications like a) To check whether a string is palindrome or not. b) To check
whether given expression is valid or not.
6. During a function call the return address and arguments are pushed onto a stack and on return
they are popped off.

2. EXPRESSIONS:
It is sequence of operators and operands that reduces to a single value after evaluation is called an
expression.
X=a/b–c+d*e–a*c
above expression contains operators (+, –, /, *) operands (a, b, c, d, e).
1.4.1 Types of Expressions
Prefix Expression or Polish notation
Infix Expression
Postfix Expression or Reverse Polish notation

Infix Expression:
In this expression, the binary operator is placed in-between the operand. The expression can be
parenthesized or un- parenthesized.

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS

Example: A + B
Here, A & B are operands and + is operand
Prefix or Polish Expression:
In this expression, the operator appears before its operand.
Example: + A B
Here, A & B are operands and + is operand
Postfix or Reverse Polish Expression:
In this expression, the operator appears after its operand.
Example: A B +
Here, A & B are operands and + is operand

PRECEDENCE OF THE OPERATORS:


OPERATOR PRECEDENCE VALUE
$ or ↑ or ^ Highest 4
*, / Next highest 3
+, - Lowest 2
# Lowermost 1
In C, there is a precedence hierarchy that determines the order in which operators are evaluated.
Below figure contains the precedence hierarchy for C.

Prof. Shrikant Pujar, Dept. of CS&E 7


DATA STRUCTURES AND APPLICATIONS

 The operators are arranged from highest precedence to lowest. Operators with highest
precedence are evaluated first.
 The associativity column indicates how to evaluate operators with the same precedence.
 For example, the multiplicative operators have left-to-right associativity. This means that the
expression a * b / c % d / e is equivalent to ( ( ( ( a * b ) / c ) % d ) / e )
 Parentheses are used to override precedence, and expressions are always evaluated from the
innermost parenthesized expression first.

3. Conversion from infix to Postfix Expression:


Procedure to convert from infix expression to postfix expression is as follows: (algorithm)
1. Push # to the operator stack to indicate stack is empty.
2. Scan the infix expression from left to right.
3. If the scanned symbol is an character (a-z,A-Z) then place directly in the postfix stack(postfix expr).
4. If the scanned symbol is a „(‟(opening brace or left parenthesis), push it onto the operator stack.
5. If the scanned symbol is an operator, then go on removing all the operators from the stack and
place them in the postfix stack(expression), if and only if the precedence of the operator on the top
of the stack is greater than or equal to the precedence of the current operator(current input symbol)
6. Else push the current operator (current input symbol) onto the operator stack.
7. If the symbol scanned is a „)‟ (closing brace right parenthesis), then go on popping all the items
from the stack and place them in the postfix expression till we get the matching left parenthesis.
8. If you have reached end of input then pop all the elements from the operator stack to postfix stack.
(Postfix expression).

Function/Algorithm to Convert Infix Expression to Postfix Expression.


void infix_postfix(char infix[], char postfix[])
{
int top; //points to top of stack
int j; // index to postfix expression
int i; // index to access infix expression
char s[30]; //storage for stack
char symbol; // holds scanned char from infix expression
top = -1; // stack is empty
s[++top] = '#'; //initialize stack to #
j = 0; // output is empty
for(i=0; i < strlen(infix); i++)
{ symbol = infix[i]; //scan next symbol
while(F(s[top]) > G(symbol)) //if stack precedence is greater
{
postfix[j++] = s[top--]; // pop and place into postfix
}

Prof. Shrikant Pujar, Dept. of CS&E 8


DATA STRUCTURES AND APPLICATIONS

if(F(s[top]) != G(symbol))
s[++top] = symbol; //push the input symbol
else
top--; //discard „(„ from stack
}
while(s[top] != '#')
{
postfix[j++] = s[top--]; //pop and place in postfix
}
postfix[j] = '\0'; //terminated by null
}

Example 1: Convert infix Expression to Postfix using stack ( A + ( B – C ) * D )

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS

Note: Refer class notes for examples and steps in conversion (detailed).

4. EVALUATION OF POSTFIX EXPRESSION:


1. Scan the postfix expression from left to right.
2. If the scanned symbol is an operand, then push it onto the stack.
3. If the scanned symbol is an operator, pop two operands from the stack.
The first operand popped is operand2 and the second operand popped is operand1.
4. Perform the desired operation and push the result onto stack
5. Repeat above steps till the end of the expression reached.
Function/Algorithm to Convert Infix Expression to Postfix Expression.
While end of input is not reached do
{
Symbol = nextchar()
If ( symbol is an operand)
Push(symbol, top, s);
Else
Op2 = pop (top, s);
Op1 = pop (top, s);

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS

Res = op1 op op2;


Push (res, top, s)
}

Example: Evaluate the following postfix Expression.


ABC-D*+E$F+
A=6, B=3, C=2, D=5, E=1, F=7

Example 2: Evaluate the following postfix Expression.


ABC+*CBA-+* where A=1, B =2, C=3

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS

Example 3: Evaluate the following postfix Expression.


AB+C-BA+C$- where A=1, B =2, C=3

ASSIGNMENT QUESTIONS
1. Define stack. Implement the operations push, pop functions.
2. What is expression? List and explain different types of expressions.
3. Write an algorithm or C function for (1) converting infix expression to postfix expression (2)
Evaluate a postfix expression.
4. Convert the following infix expression to postfix expression.
1. ((a/(b-c+d))*(e-f)*g)
2. (a+b)*d+e/(f+g*h)+i
3. A+(B*C-(D/E$F)*G)*H
4. ((A+(B-C)*D)$E+F)
5. Evaluate the following postfix expression And trace for the given data
1. ab/c-de*+ac*+ where a=6,b=3,c=1,d=2,e=4.
2. abc+*de/- where a=5,b=6,c=2,d=12,e=4.

Prof. Shrikant Pujar, Dept. of CS&E 12


DATA STRUCTURES AND APPLICATIONS

MODULE -3
Linked Lists: Definition, Representation of linked lists in Memory, Memory allocation; Garbage
Collection. Linked list operations: Traversing, Searching, Insertion, and Deletion. Doubly Linked
lists, Circular linked lists, and header linked lists. Linked Stacks and Queues. Applications of
Linked lists – Polynomials, Sparse matrix representation. Programming Examples

3.1 Linked List: “A Linked list is a linear collection of data elements called nodes, where each
node is divided into two parts: the first part called info field, contains the information of the element
and second part called link field or next pointer field, contains the address of the next node in the
list”.
Node: Info-field Link-field

Example: first
10 20 30 40 \0

100 200 300 400


Where,
 first is a variable, contains the address of the first node.
 This list contains 4 nodes and each node consists of two fields, info and link.
 The info field of each node contains the data. 10, 20, 30 and 40.
 The numbers 100,200,300 and 400 represent the address of each node in memory.
 The link field of the last node contains \0 (NULL) indicates it is last node.

Arrays–drawbacks:
(1)The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. (Static in nature)
(2) Inserting and a new element in an array of elements is expensive, because room has to be
created for the new elements and to create room existing elements have to shifted. Same holds good
for deletion also.

Advantages of linked list:


1. Efficient memory utilization: The memory of a linked list is not pre-allocated. Memory can be
allocated whenever required. And it is de-allocated when it is no longer required.
2. Insertion and deletion operations are easier and efficient: Linked list provide flexibility in
inserting a data item at a specified position and deletion of a data item from the given position.
3. Extensive manipulation: We can perform any number of complex manipulations without any
prior idea of the memory space available. (i.e. in stacks and queues we sometimes get overflow
conditions. Here no such problem arises.)
4. Arbitrary memory locations: Here the memory locations need not be consecutive. They may
be any arbitrary values. But even then the accessing of these items is easier as each data item
contains within itself the address to the next data item.

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS
Disadvantages of linked lists
 They have a tendency to use more memory due to pointers requiring extra storage space.
 Nodes in a linked list must be read in order from the beginning as linked lists are inherently
sequentially accessed.(cannot be randomly accessed)
 Nodes are stored in contiguously, greatly increasing the time required to access individual
elements within the list.
 Difficulties arise in linked lists when it comes to reverse traversing.

3.2 Representation of linked lists in Memory


 List requires 2 linear arrays- INFO and LINK – such that INFO[K] and LINK [K] contain,
information part and next pointer field of a node of LIST.
 LSIT also requires a variable name – START – which contains the location of the beginning of
the list.
 NULL –which indicates the end of the list.

Prof. Shrikant Pujar, Dept. of CS&E 2


DATA STRUCTURES AND APPLICATIONS
3.3 Memory allocation; Garbage Collection
 The maintenance of linked lists in memory assumes the possibilities of inserting new nodes into
the lists and hence requires some mechanism which provides unused memory space for new
nodes. With the linked list in memory, a special list is maintained which consist of unused
memory cells. This list which has its own pointer is called the list of available spaces or the free
storage list.
 When we use parallel array then for the use of unused memory cells in array we will also be
linked together to form a linked list using avail as its list pointer variable. Such structure will be
denoted by
LIST (INFO, LINK, START, AVAIL)

 Garbage Collection: Whenever a node is deleted, some memory space becomes reusable. This
memory space should be available for future use. One way to do this is called „Garbage
Collection‟.
 The operating system of computer may periodically collect all the deleted space into the free
space list. Any technique which does this collection is called garbage collection.
 It takes place in two steps.
1. First computer runs through all lists, tagging those cells which are currently in use.
2. Then computer runs through the memory collecting the untagged space on to free storage list.
 The garbage collection may take place when there is only some minimum amount of space or no
space at all left in the Free storage list or when CPU is idle and has time to do the collection.

Over flow & under flow


 Sometimes new data are to be inserted into a data structure but there is no available space i.e the
free storage list is empty. This situation is usually called overflow. The over flow may handle by
printing the message of over flow.
 Under flow refers to the situation where one wants to delete data from a data structure that is
empty. This situation is usually called underflow. The over flow may handle by printing the
message of under flow.

3.4 Types of Linked List:


 There are mainly 4 different types of linked lists.
1. Singly Linked List (SLL)
2. Circular Singly Linked List (CSLL)
3. Doubly Linked List (DLL)
4. Circular Doubly Linked List (CDLL)
5. Header Linked List (HLL)

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS
3.4.1 Singly Linked List (SLL): It is a collection of zero or more nodes where each node has two
or more fields but only one link field which contains the address of next node.

Example: first
10 20 30 40 \0

100 200 300 400


Where,
 first is a variable, contains the address of the first node.
 This list contains 4 nodes and each node consists of two fields, info and link.
 The info field of each node contains the data. 10, 20, 30 and 40.
 The numbers 100,200,300 and 400 represent the address of each node in memory.
 The link field of the last node contains \0 (NULL) indicates it is last node.

Representing a node of Linked List:

Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE first; // first- self-referential structure variable
First = NULL; // Empty list by name first is created with NULL value

Create a node:
X = (data_type *) malloc (size);
 Malloc(): it is used to allocate memory explicitly as and when required and exact amount of
memory space needed during execution.
X = (NODE) malloc (sizeof(struct node)); // allocate the memory space

NODE getnode()
{
NODE X;
X = (NODE) malloc (sizeof(struct node));
if ( X = = NULL) //nodes don‟t exist
{
Printf(“out of memory”);
}
return X; // allocation successful
}

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS
Operations on Singly Lined List:
1. Inserting a node into list : a) insert front b) insert rear
2. Deleting a node from list: a) delete front b) delete rear
3. Searching data in a list
4. Display the content of list

3.4.1.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: copy the address of the first node which is stored in the variable first into link field of
temp. temp -> link = first;
temp first
50 10 20 30 40 \0

Step 4: temp is inserted and then temp becomes the first node.
return temp;
first

50 10 20 30 40 \0

C Function to insert an item at the front end of the list.

NODE insert_front (int item, NODE first )


{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> link = first; // insert new node at front
return temp; // return the new first node
}

3.4.1.2 Inserting a node at rear:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy item 50 into info field and NULL to link. temp
temp -> info = item; 50 \0
temp -> link = NULL;

Prof. Shrikant Pujar, Dept. of CS&E 5


DATA STRUCTURES AND APPLICATIONS
Step 3: If list is empty, the above node can be returned as the first node of the list.
if (first = = NULL) return temp;

Step 4: if the list is existing, then we have to insert temp at the end of list. To insert at the end,
we have to find the address of the last node.
cur = first;
while( cur -> link != NULL)
{
cur = cur ->link;
}
After executing the above code , we will get the address of last node in a list.
First cur temp

10 20 30 40 \0 50 \0

Step 5: copy the address of the temp node to cur -> link, NULL is replaced by temp address.
cur -> link = temp;
first cur temp
10 20 30 40 50 \0

Step 6: first contains the address of the first node of list. So we have to return first.
return first;
first

10 20 30 40 50 \0

C Function to insert an item at the rear end of the list.


NODE insert_rear ( int item, NODE first)
{
NODE temp, cur;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> link = NULL; // insert NULL to link
if (first = = NULL)
return temp;
cur = first;
while( cur -> link != NULL) //checking last node of list
{
cur = cur ->link;
}
cur -> link = temp; // insert new node at end
return first; // return the first node
}

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS
3.4.1.3 Delete a node from the front end:
Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}
Step 2: if list is existing, first contains the address of the first node. We have to take pointer
variable temp and store the address of first node.
temp = first;
first temp

10 20 30 40 50 \0

Step 3: update the pointer temp so that it contains address of the second node.
temp = temp -> link;
first temp
10 20 30 40 50 \0

Step 4: now, first points to first node and temp points to second node of the list. De-allocate the
memory of the first node.
free( first);

Step 5: once first node is deleted, and then second node temp is the first node. So, return temp as
first node. return temp;
first
20 30 40 50 \0

C Function to delete an item from front end of the list.

NODE delete_front ( NODE first)


{
NODE temp;
if ( first = = NULL)
{
printf(“List is empty”);
}
temp = first;
temp = temp -> link;
printf(“item delted =%d”, first -> info);
free (first);
return temp;
}

Prof. Shrikant Pujar, Dept. of CS&E 7


DATA STRUCTURES AND APPLICATIONS
3.4.1.4 Delete a node from the rear end:
Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if the list is existing, then we have to delete the last node of list. To delete at the raer end,
we have to find the address of the last node by using variable cur and prev.
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}

After executing the above code , we will get the address of last node and prev get the address of
last but one node in a list.
First prev cur

10 20 30 40 50 \0

Step 3: delete the last node pointed by cur.


free ( cur );
first prev cur
10 20 30 40 50 \0

Step 4: once last node is deleted, and then prev node should be the last node. So, copy NULL to
link field of prev node.
prev -> link = NULL;
first prev

10 20 30 40 \0

Step 5: finally return the address of the first node..


return first;
first

10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 8


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE first)


{
NODE cur,prev;
if ( first = = NULL)
{
printf(“List is empty”);
}
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf(“item delted =%d”, cur -> info);
free (cur);
return first;
}

3.4.1.5 Searching for key item in the list:


 Here we have to search key by comparing info field of each node in a list.
 if item found, then return pointer to the node.
 if item not found, return NULL.

C Function to Search for key item in the list.


NODE search ( int key, NODE first)
{
NODE cur;

if (first = = NULL) // search for empty list


return NULL;

/* compare one after the other */


cur = first;
while( cur ! = NULL)
{
if ( key = = cur -> info)
return cur; //if key found
cur = cur ->link; // point cur to next node
}
return NULL; // key not found
}

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS
3.4.1.6 Display the items in the list
C Function to display the contents of linked list.

Void display ( NODE first)


{
NODE cur;
if ( first = = NULL)
{
printf(“List is empty”);
return;
}

printf(“the contents of linked list”);


cur = first; //holds address of first node
while( cur != NULL)
{
Printf(“%d”, cur ->info); //display info field of node
cur = cur ->link; //points to the next node
}
printf(“\n”);
}

3.4.1.7 Concatenate two lists:


 Concatenation of two lists means combining the second list at the end of the first list.
Step 1: concatenation of two lists is possible only if two lists exist. If one of the lists is empty,
return the address of the first node of the non-empty list.
Step 2: if two lists are exist, obtain the address of the last node of first list. Pointer cur contains
the address of the last node of the first list.
cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link;
}
First cur second

10 20 30 \0 40 50 \0

Step 3: now, attach address of the first node of the second list to cur node.
cur -> link = second;
First cur second
10 20 30 40 50 \0

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS
C Function to concatenate two list.

NODE concate ( NODE first, NODE second)


{
NODE cur;

if (first = = NULL)
return second;
if (second = = NULL)
return first;

cur = first;
while( cur ->link ! = NULL)
{
cur = cur ->link; // point cur to next node
}
Cur -> link =second;
return first;
}

3.4.1.8 Reverse a list without creating new nodes.

C Function to reverse list.

NODE reverse ( NODE first )


{
NODE cur,temp;
cur = NULL; //initial reversed list

while (first ! = NULL)


{
temp = first -> link; //obtain address of second node
first-> link = cur; // attach first node of list to be reversed
// at the beginning of partially reversed list
cur = first; // point cur to point to newly
// partially reversed list
first = temp;
}
return cur; //contains address of reversed list
}

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS
3.4.2 Circular Singly Linked List (CSLL)
 A circular singly linked list is a singly linked list where the link field of last node of the list
contains address of the first node.
first

10 20 30 40

Representing a node of Circular Linked List:


Struct node
{
int info; //info-integer field contains information
struct node *link; //link- holds address of next node
};
typedef struct node *NODE; //giving alternative name to strcut node to NODE
NODE last; // last- self-referential structure variable
last = NULL; // Empty list by name last is created with NULL value
if (last = = NULL)
{
printf(“list is empty\n”);
}

Operations on Circular Singly Lined List:


1. Inserting a node into list : a) insert front b) insert rear
2. Deleting a node from list: a) delete front b) delete rear
3. Display the content of list

3.4.2.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 40 into info field of temp. temp


temp -> info = item; 40

Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
temp first last

40 10 20 30

Prof. Shrikant Pujar, Dept. of CS&E 12


DATA STRUCTURES AND APPLICATIONS
Step 4: make temp as the first node by copying temp into link field of last node.
last->link = temp;
first last

40 10 20 30

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
if(last != NULL)
temp -> link = last ->link; //insert at front
else
last = temp;
last -> link = temp; // link last node to first node
return last; // return the last node
}

3.4.2.2 Inserting a node at rear:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: copy the address of the first node(i.e last -> link ) into link field of temp.
if(last != NULL)
temp -> link = last ->link;
else
temp = last;
first last temp

10 20 30 50

Step 4: make temp as the last node by copying temp into link field of last node.
last->link = temp;
first temp

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 13


DATA STRUCTURES AND APPLICATIONS
C Function to insert an item at the rear end of the list.
NODE insert_rear (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
if(last != NULL)
temp -> link = last ->link; //insert at front
else
last = temp ;
last -> link = temp; // link last node to first node
return temp; // make new node as last node
}

3.4.2.3 Delete a node from the front end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if list is existing, obtain the address of the first node.


first = last -> link;
first last

10 20 30 50

Step 3: Make second node as first node.


last-> link = first -> link;
first last

10 20 30 50

Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);

first last

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 14


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from front end of the list.

NODE delete_front ( NODE last)


{
NODE first;
if ( last = = NULL)
{
printf(“List is empty”);
}
first = last ->link; //obtain node to be deleted
last ->link = first->link; // store new first node in link of last
printf(“item deleted =%d”, first->info);
free(first); // delete old node
return last; // return address of last node
}

3.4.2.3 Delete a node from rear end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}
Step 2: if list is existing, obtain the address of the first node.
prev = last -> link;
first last

10 20 30 50

Step 3: find the address of the last but one node.


While(prev -> link ! = last)
{ prev = prev -> link;
}
first prev last

10 20 30 50

Step 4: Make the last but one node as the last node and De-allocate the memory of the last node.
prev -> link = last ->link;
free( last);
first last

10 20 30 50

Prof. Shrikant Pujar, Dept. of CS&E 15


DATA STRUCTURES AND APPLICATIONS
C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE last)


{
NODE prev;
if ( last = = NULL)
{
printf(“List is empty”);
}
prev = last ->link; //obtain the address of prev node
while(prev -> link ! = last)
{
prev = prev -> link;
}
printf(“item deleted =%d”, last->info);
free(last); // delete old last node
return prev; // return address of new last node
}

3.4.3 Doubly linked list: A doubly linked list is a linear collection of nodes where each node is
divided into three parts:
Info – contains information to be stored.
llink – contains the address of the left or previous node. llink info rlink
rlink - contains the address of the right or next node.

Example:
first

\0 10 20 30 \0

3.4.3.1 Insert node at front


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

Step 2: insert into existing link.


temp-> rlink = first;
first -> llink = temp;
temp first

\0 40 10 20 30 \0

Prof. Shrikant Pujar, Dept. of CS&E 16


DATA STRUCTURES AND APPLICATIONS
Step 3: temp is inserted and then temp becomes the first node.
return temp;
first

\0 40 10 20 30 \0

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE first )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
temp ->rlink = first; //insert at front of existing list
first -> llink = temp;
return temp; //return the address of new first node
}

3.4.3.2 Insert node at the rear


Step 1: create a node using getnode() function, copy the item 40 into info field and assign
NULL to llink & rlink.
temp = getnode (); temp
temp -> info = item;
\0 40 \0
temp -> llink = temp -> rlink = NULL;

Step 2: To insert the node at rear end, first find the address of the last node.
cur = first;
while(cur -> rlink ! = NULL)
{
cur = cur -> rlink;
}
first cur temp

\0 10 20 30 \0 \0 40 \0
Step 3: Insert node at the end.
cur -> rlink = temp;
temp -> llink = cur;
first cur temp

\0 10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 17


DATA STRUCTURES AND APPLICATIONS
Step 4: return address of the first node.

return first;
first

\0 10 20 30 40 \0

C Function to insert an item at the rear end of the list.


NODE insert_rear (int item, NODE first )
{
NODE temp,cur;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
temp -> llink = temp-> rlink =NULL;
if(first == NULL)
return temp; //insert node for first time
cur = first;
while(cur -> rlink ! = NULL) //find address of last node
{
cur = cur -> rlink;
}
cur -> rlink = temp; //insert node at the end
temp -> llink = cur;
return first; //return the address of first node
}

3.4.2.3 Delete a node from the front end:


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2: if list is existing, obtain the address of the second node.


second = first -> rlink;
first second

\0 10 20 30 \0

Step 3: Make second node as first node.


second->l link =NULL;
first second

\0 10 \0 20 30 \0

Prof. Shrikant Pujar, Dept. of CS&E 18


DATA STRUCTURES AND APPLICATIONS
Step 4: Now remove the first node. De-allocate the memory of the first node.
free( first);
first first

\0 10 \0 20 30 \0

C Function to delete an item from front end of the list.

NODE delete_front ( NODE first)


{
NODE second;
if ( first = = NULL)
{
printf(“List is empty”);
}
second = first -> rlink; //get address of second node
second -> llink =NULL; // make second as first node
printf(“item deleted =%d”, first->info);
free(first); // delete first node
return second;
}

3.4.3.4 Delete a node at the rear


Step 1: if the list is empty, it is not possible to delete a node from list.
if ( first = = NULL)
{
printf(“List is empty”);
}

Step 2:if list is existing then find the address of last node and last but one node.
cur = first, prev = NULL;
while(cur -> rlink ! = NULL)
{ prev = cur;
cur = cur -> rlink;
}
first prev cur

\0 10 20 30 40 \0

Step 3:the last node pointed by cur can be deleted.


free(cur);

first prev cur

\0 10 20 30 40 \0

Prof. Shrikant Pujar, Dept. of CS&E 19


DATA STRUCTURES AND APPLICATIONS
Step 4: The node pointed by prev becomes the last node.
prev -> rlink = NULL;
first

\0 10 20 30 \0

C Function to delete an item from rear end of the list.

NODE delete_rear ( NODE first)


{
NODE cur,prev;
if ( first = = NULL)
{
printf(“List is empty”);
}
cur = first;
prev= NULL;
while( cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf(“item delted =%d”, cur -> info);
free (cur);
prev -> rlink = NULL;
return first;
}

3.4.4 Header Linked List


“A header node in a linked list is special node whose link field always contains address of the first
node of the list and info field contains the number of nodes in list”.
head
10 30 40 50 \0

 The info field of header node does not contain any information.
 The first node starts from link field of header node i.e the node containing the info field 10 is the
first node.
head

4 10 30 40 50 \0

 The info field of the header node contains the number of nodes in the list.

Prof. Shrikant Pujar, Dept. of CS&E 20


DATA STRUCTURES AND APPLICATIONS
Advantages of Header node:
1. Simplifies Insertion and deletion operations.
2. Avoid the usage of various cases such as “if only one node is present what to do”.
3. Designing of program will be very simple.
4. Circular lists with header node are frequently used instead of ordinary linked list.

3.4.4.1 Inserting a node at front:


Step 1: create a node using getnode() function. temp
temp = getnode ();

Step 2: copy the item 50 into info field of temp. temp


temp -> info = item; 50

Step 3: Obtain the address of the first node which is stored in the head link field of head.
first = head -> link;
head first
10 20 30 40 \0

Step 4: temp node is inserted between the head and first node.
head -> link = temp;
temp temp -> link = first;
50

head first
10 20 30 40 \0

Step 5: return the address of header node.


return head;
head

50 10 20 30 40 \0

C Function to insert an item at the front end of the list.


NODE insert_front (int item, NODE head )
{
NODE temp;
temp = getnode(); //obtain the new temp node
temp -> info = item; //insert item to temp info
first = head -> link;
head -> link = temp; // insert new node at front
temp -> link = first;
return head; // return the header node
}

Prof. Shrikant Pujar, Dept. of CS&E 21


DATA STRUCTURES AND APPLICATIONS
3.5 POLYNOMIALS
Polynomial Representation:
representation of Polynomials in a linked list.
Example: a = 3X14 + 2X8 + 1 and b=8X14 – 3X10 + 10X6

2. Adding polynomials

Prof. Shrikant Pujar, Dept. of CS&E 22


DATA STRUCTURES AND APPLICATIONS
C Function for addition of two polynomials.

3.6 Sparse Matrix Representation


 The sparse matrix can be more efficiently represented using linked list.
 Header node is represented by 3 fields.

Next
Down right

 Items/elements in matrix are represented by 5 fields.

row col val


Down right

Prof. Shrikant Pujar, Dept. of CS&E 23


DATA STRUCTURES AND APPLICATIONS
Representation of sparse matrix using linked list:

Prof. Shrikant Pujar, Dept. of CS&E 24


DATA STRUCTURES AND APPLICATIONS

MODULE - 4
Trees: Terminology, Binary Trees, Properties of Binary trees, Array and linked Representation of
Binary Trees, Binary Tree Traversals - Inorder, postorder, preorder; Additional Binary tree
operations. Threaded binary trees, Binary Search Trees – Definition, Insertion, Deletion, Traversal,
Searching, Application of Trees-Evaluation of Expression, Programming Examples

Trees
“Tree is a non-linear data structure which organizes data in hierarchical fashion and the tree
structure follows a recursive pattern of organizing and storing data”.
 Every individual element is called as Node. Node in a tree data structure, stores the actual data of
that particular element and link to next element in hierarchical structure.
 If there are N number of nodes in a tree structure, then there can be a maximum of N-1 number of
links.
Example:

4.1 Trees Terminology


1. Root: In a tree data structure, the first node is called as Root Node. Every tree must have root
node. We can say that root node is the origin of tree data structure. In any tree, there must be only
one root node. We never have multiple root nodes in a tree. Ex: ‘A’ in the above tree
2. Edge: In a tree data structure, the connecting link between any two nodes is called as EDGE. In a
tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. Ex: Line between
two nodes.
3. Parent: In a tree data structure, the node which is predecessor of any node is called as PARENT
NODE. In simple words, the node which has branch from it to any other node is called as parent
node. Parent node can also be defined as "The node which has child / children".
Ex: A,B,C & D are parent nodes
4. Child: In a tree data structure, the node which is descendant of any node is called as CHILD
Node. In simple words, the node which has a link from its parent node is called as child node. In a
tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are
child nodes. Ex: B & C are children of A, G & F are children of C and E child of B.
5. Siblings: In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In
simple words, the nodes with same parent are called as Sibling nodes. Ex: B & C are siblings, D, E,
F and G are siblings, I & H are siblings.

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS
6. Leaf: In a tree data structure, the node which does not have a child is called as LEAF Node. In
simple words, a leaf is a node with no child. In a tree data structure, the leaf nodes are also called as
External Nodes. External node is also a node with no child. In a tree, leaf node is also called as
'Terminal' node. Ex: H,I,E,F and G are leaf nodes

7. Internal Nodes: In a tree data structure, the node which has atleast one child is called as
INTERNAL Node. In simple words, an internal node is a node with atleast one child. In a tree data
structure, nodes other than leaf nodes are called as Internal Nodes. The root node is also said to be
Internal Node if the tree has more than one node. Internal nodes are also called as 'Non-Terminal'
nodes. Ex: B,C & D.

8. Degree of a node: In a tree data structure, the total number of children of a node is called as
DEGREE of that Node. In simple words, the Degree of a node is total number of children it has. The
highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'. Ex: Degree of B
is 2, A is 2 and of F is 0

9. Level of a node: In a tree data structure, the root node is


said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1
will be at Level 2 and so on... In simple words, in a tree
each step from top to bottom is called as a Level and the
Level count starts with '0' and incremented by one at each
level (Step).

10. Height
In a tree data structure, the total number of edges from leaf
node to a particular node in the longest path is called as
HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf
nodes is '0'.

11. Depth: In a tree data structure, the total number of


edges from root node to a particular node is called as
DEPTH of that Node. In a tree, the total number of
edges from root node to a leaf node in the longest path
is said to be Depth of the tree. In simple words, the
highest depth of any leaf node in a tree is said to be
depth of that tree. In a tree, depth of the root node is
'0'.

Prof. Shrikant Pujar, Dept. of CS&E 2


DATA STRUCTURES AND APPLICATIONS
12. Path:
In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as
PATH between the two Nodes. Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.

13. Sub Tree: In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.

The ancestors of a node are all the nodes along the path from the root to that node. Ex: ancestor of j
is B & A A forest is a set of n 0 disjoint trees. The notion of a forest is very close to that of a tree
because if we remove the root of a tree we get a forest. For example, in figure 1 if we remove A we
get a forest with three trees.

4.2 General Tree Representations


A general Tree Structure can be represented with the following three methods.
1. List Representation
2. Left Child - Right Sibling Representation
3. Degree two Representation (Binary Tree Representation)
Consider the following tree...

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS
1. List Representation
 A tree can be represented using list as shown below:
1. The information in the root node comes first.
2. It is immediately followed by a list of subtrees of that node.
3. It is recursively repeated for each subtree,
 The above tree example can be represented using List representation as follows...

 In this representation, we use two types of nodes, one for representing the node with data and
another for representing only references.
 We start with a node with data from root node in the tree. Then it is linked to an internal node
through a reference node and is linked to any other node directly. This process repeats for all the
nodes in the tree.

2. Left Child - Right Sibling Representation

3. Degree two Representation (Binary Tree Representation)

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS
4.3 Binary Tree
 Binary tree is a special type of tree data structure in which every node can have either 0 children or
1 child or 2 children but not more than 2 children. One is known as left child and the other is known
as right child.
Example:

4.3.1 Types of Binary Trees


1. Strictly Binary Tree
 A strictly Binary Tree is a binary tree, where every node should have exactly two children or none.
That means every internal node must have exactly two children.
 Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree.

2. Complete Binary Tree


 A binary tree in which every internal node has exactly two children and all leaf nodes are at same
level is called Complete Binary Tree. Complete binary tree is also called as Perfect Binary Tree.
 In complete binary tree all the nodes must have exactly two children and at every level of complete
binary tree there must be 2level number of nodes. For example at level 2 there must be 22 = 4 nodes
and at level 3 there must be 23 = 8 nodes.

3. Almost complete Binary Tree


 It is complete binary tree but completeness property is not followed in last level. In the above tree
absence of leaf nodes L, M, N, O and P indicates its almost complete binary tree.

Prof. Shrikant Pujar, Dept. of CS&E 5


DATA STRUCTURES AND APPLICATIONS

Binary Tree Representations


 A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...

1. Array Representation
 In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a
binary tree. Consider the above example of binary tree and it is represented as follows...

 To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n+1 - 1.
Advantages of array representation:
 Faster access
 Easy for implementation
 Good for complete binary trees
Disadvantages:
 Wastes memory for skewed trees
 Implementation of operations requires rearranging (shifting) of array elements.

2. Linked List Representation


 The linked notation uses a doubly linked list to represent a binary tree. In a double linked list, every
node consists of three fields. First field for storing left child address, second for storing actual data
and third for storing right child address. In this linked list representation, a node has the following
structure...

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS
The above example of binary tree represented using Linked list representation is shown as follows...

4.4 Binary Tree Traversals


 Tree traversal is a method of visiting the nodes of a tree in a particular order. The tree nodes are
visited exactly once and displayed as they are visited. Displaying (or) visiting order of nodes in a
binary tree is called as Binary Tree Traversal.
 There are three types of binary tree traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal

4.4.1 In - Order Traversal ( left Child - root – right Child )


 In In-Order traversal, the root node is visited between left child and right child. In this traversal, the
left child node is visited first, then the root node is visited and later we go for visiting right child
node. This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.
void inorder(NODE root)
{
inorder (root->llink);
printf("%d\t",root->data);
inorder (root->rlink);
}

Prof. Shrikant Pujar, Dept. of CS&E 7


DATA STRUCTURES AND APPLICATIONS
4.4.2 Pre - Order Traversal ( root - leftChild - rightChild )
 In Pre-Order traversal, the root node is visited before left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order
traversal is applicable for every root node of all subtrees in the tree.
void preorder(NODE root)
{
printf("%d\t",root->data);
preorder(root->llink);
preorder(root->rlink);
}

4.4.3 Post - Order Traversal ( leftChild - rightChild - root )


 In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left
child node is visited first, then its right child and then its root node. This is recursively performed
until the right most node is visited.
void postorder(NODE root)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->data);
}

Prof. Shrikant Pujar, Dept. of CS&E 8


DATA STRUCTURES AND APPLICATIONS
Level Order Traversal
 Level order traversal is a method of traversing the nodes of a tree level by level as in breadth first
traversal.
 Level order traversal uses queue thus avoiding stack space usage.
 Here the nodes are numbered starting with the root on level zero
continuing with nodes on level 1,2,3…..
 The nodes at any level is numbered from left to right
 Visiting the nodes using the ordering of levels is called level order
traversal
 Queue uses FIFO principle
 The level order traversal of the above tree is F B G A D I C E H

Additional Binary tree Operations:


1. Copying a Tree: Copy one binary tree to other tree.
NODE copy(NODE root)
{ NODE temp;
if(root = = NULL) //tree does not exist
return NULL;
temp =getnode(); // create a new node
temp -> info = root->info; // copy the information
temp-> lptr = copy(root->lptr); //copy appropriate links
temp-> rptr = copy(root->rptr);
return temp; //return the address of the new node
}

2. Test for Equality: Check whether two trees are equal or not.
int equal (NODE r1, NODE r2)

{
if(r1 = = NULL && r2 = = NULL) return 1; // two trees are equal

if(r1 = = NULL && r2 ! = NULL) return 0; // two trees are not equal

if(r1 ! = NULL && r2 = = NULL) return 0; // two trees are not equal

if(r1 ->info == r2 ->info) return 1; // two trees are equal

if(r1 ->info != r2 ->info) return 0; // two trees are not equal

//recursively check leftsubtree and right subtree of both the trees


return equal(r1->llink, r2->llink) && equal(r1->rlink, r2->rlink);
}

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS
4.5 Threaded Binary Tree
 A binary tree is represented using array representation or linked list representation. When a binary
tree is represented using linked list representation, if any node is not having a child we use NULL
pointer in that position.
 In any binary tree linked list representation, there are more number of NULL pointer than actual
pointers. Generally, in any binary tree linked list representation, if there are 2N number of reference
fields, then N+1 number of reference fields are filled with NULL ( N+1 are NULL out of 2N ). This
NULL pointer does not play any role except indicating there is no link (no child).
 A. J. Perlis and C. Thornton have proposed new binary tree called "Threaded Binary Tree", which
make use of NULL pointer to improve its traversal processes. In threaded binary tree, NULL
pointers are replaced by references to other nodes in the tree, called threads.
 Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked
list representation) points to its in-order predecessor, and all right child pointers that are NULL (in
Linked list representation) points to its in-order successor. If there is no in-order predecessor or in-
order successor, then it points to root node.
 Consider the following binary tree with header node...

 Right in threaded binary tree: if the right link of a node is NULL and if it is replaced by the
address of the inorder successor.

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS
Lift in threaded binary tree: if the left link of a node is NULL and if it is replaced by the address
of the inorder successor.

In-order Threaded binary tree: left and right links are replaced by the address of the inorder
successor.

4.6 Binary Search Tree


 Binary Search Tree is a binary tree in which every node contains only smaller values in its left
subtree and only larger values in its right subtree.
 The left subtree of a node contains only nodes with keys lesser than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s key.
 The left and right subtree each must also be a binary search tree.
Example:

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS
Operations on a Binary Search Tree
The following operations are performed on a binary search tree...
 Insertion
 Search
 Deletion
 Traversal

4.6.1 Insertion Operation in BST


In binary search tree, new node is always inserted as a leaf node. The insertion operation is
performed as follows...
Step 1: Create a newNode with given value and set its left and right to NULL.
Step 2: Check whether tree is Empty.
Step 3: If the tree is Empty, then set set root to newNode.
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the
node (here it is root node).
Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is
larger than the node, then move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (e.i., reach to NULL).
Step 7: After reaching a leaf node, then isert the newNode as left child if newNode is smaller or
equal to that leaf else insert it as right child.

NODE insert(int item,NODE root)


{
NODE temp,cur,prev;
int i;
temp=getnode();
temp->data=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL) return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
cur=(temp->data<cur->data)?cur->llink: cur->rlink;
}
if(temp->data<prev->data)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}

Prof. Shrikant Pujar, Dept. of CS&E 12


DATA STRUCTURES AND APPLICATIONS
4.6.2 Search Operation in BST
In a binary search tree, the search operation is performed as follows...
Step 1: Read the search element from the user
Step 2: Compare, the search element with the value of root node in the tree.
Step 3: If both are matching, then display "Given node found!!!" and terminate the function
Step 4: If both are not matching, then check whether search element is smaller or larger than that
node value.
Step 5: If search element is smaller, then continue the search process in left subtree.
Step 6: If search element is larger, then continue the search process in right subtree.
Step 7: Repeat the same until we find exact element or we completed with a leaf node.
Step 8: If we reach the node with search value, then display "Element is found" and terminate the
function.
Step 9: If we reach a leaf node and it is also not matching, then display "Element not found" and
terminate the function.

NODE search(NODE root) /*for search function*/


{
int item,i=0;
NODE cur;
printf("enter the element to be searched\n");
scanf("%d",&item);
cur=root;
while(cur!=NULL)
{
if(item==cur->data)
{
i++;
printf("found key %d in tree\n",cur->data);
}
if(item<cur->data)
cur=cur->llink;
else
cur=cur->rlink;
}
if(i==0)
printf("key not found\n");
return root;
}

Prof. Shrikant Pujar, Dept. of CS&E 13


DATA STRUCTURES AND APPLICATIONS
4.6.3 Deletion Operation in BST
Deleting a node from Binary search tree has following three cases...
Case 1: Deleting a leaf node We use the following steps to delete a leaf node from BST...
Step 1: Find the node to be deleted using search operation
Step 2: Delete the node using free function (If it is a leaf) and terminate the function.

Case 2: Deleting a node with one child


We use the following steps to delete a node with one child from BST...
Step 1: Find the node to be deleted using search operation
Step 2: If it has only one child, then create a link between its parent and child nodes.
Step 3: Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from BST...
Step 1: Find the node to be deleted using search operation
Step 2: If it has two children, then find the largest node in its left subtree (OR) the smallest node in
its right subtree.
Step 3: Swap both deleting node and node which found in above step.
Step 4: Then, check whether deleting node came to case 1 or case 2 else goto steps 2
Step 5: If it comes to case 1, then delete using case 1 logic.
Step 6: If it comes to case 2, then delete using case 2 logic.
Step 7: Repeat the same process until node is deleted from the tree.
4.6.4 Traversal
Traversal of BST is same as Binary tree traversal i.e inorde, preorder and postorder traversals.

4.7 Expression tree: A Binary Expression Tree is a special kind of binary tree in which:
1. Any leaf is an operand.
2. The root and internal nodes and operators
3. The subtrees represent sub expressions with root of the subtree as an operator.

 Levels Indicate Precedence


 The levels of the nodes in the tree indicate their relative precedence of evaluation (we do not
need parentheses to indicate precedence).
 Operations at higher levels of the tree are evaluated later than those below them.
 The operation at the root is always the last operation performed.

Prof. Shrikant Pujar, Dept. of CS&E 14


DATA STRUCTURES AND APPLICATIONS

MODULE - 5
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation Of Graphs,
Elementary Graph operations, Traversal methods: Breadth First Search and Depth First Search.
Sorting and Searching: Insertion Sort, Radix sort, Address Calculation Sort.
Hashing: Hash Table organizations, Hashing Functions, Static and Dynamic Hashing.
Files and Their Organization: Data Hierarchy, File Attributes Text Files and Binary Files, Basic
File Operations, File Organizations and Indexing.

5.1 Graphs - Terminology and Representation


 A graph is an abstract data structure that is used to implement the mathematical concept of graphs. It
is basically a collection of vertices (also called nodes) and edges that connect these vertices.
 A graph is often viewed as a generalization of the tree structure, where instead of having a purely
parent-to-child relationship between tree nodes, any kind of complex relationship can exist.

Definitions: Graph, Vertices, Edges


 Graph is defined as a pair of two sets G = (V, E)
1. V = a set of vertices
2. E = a set of edges
 Edges: Edge is an arc or line joining two vertices.
 Vertices: Vertices also called nodes, represented by circle. Denote vertices with labels.
Example:
V = {A,B,C,D,E}
E = {(A,B),(A,C),(A,D),(B,D),(C,D),(B,E),(D,E)}

 Examples of Graph applications:


 Cities with distances between Roads
 Network and shortest routes
 Social networks
 Electric circuits, projects planning and many more...

5.2 Graph Classifications


There are several common kinds of graphs
1. Directed or undirected
2. Multigraphs
3. Complete graph
4. Weighted or unweighted

Prof. Shrikant Pujar, Dept. of CS&E 1


DATA STRUCTURES AND APPLICATIONS
1. Digraph: A graph whose edges are directed (i.e have a direction).
 Edge drawn as arrow.
 Edge can only be traversed in direction of arrow.
 Example: E = {(A,B), (B,C), (C,E), (E,D), (D,B), (E,F)}

Undirected Graph: no implied direction on edge between nodes.


 In diagrams, edges have no direction (ie there are no arrows)
 Can traverse edges in either directions

2. Multigraph: A graph with self-loops or parallel edges(multiple edge) between any two vertices is
called a multigraph.

3. Complete graph: A graph is said to be a complete graph, if there exists an edge between every
pair of vertices.

4. Weighted graph: A graph in which a number is assigned to each edge in graph is called weighted
graph. Weight typically shows cost or distance of traversing.
Example: weights are distances between cities.

Unweighted graph: A graph in which a number is not assigned to edge in graph is called
unweighted. Edges simply show connections.

Prof. Shrikant Pujar, Dept. of CS&E 2


DATA STRUCTURES AND APPLICATIONS
 Path: sequence of vertices in which each pair of successive vertices is connected by an edge.
 Cycle: a path that starts and ends on the same vertex.
 Simple path: a path that does not cross itself.
 That is, no vertex is repeated (except first and last)
 Simple paths cannot contain cycles
 Length of a path: Number of edges in the path.

5.3 Matrix and Adjacency List Representation of Graphs:


Two common data structures for representing graphs are:
1. Adjacency matrix
2. Adjacency lists

5.3.1 Adjacency matrix


 An adjacency matrix is used to represent which nodes are adjacent to one another. By definition, two
nodes are said to be adjacent if there is an edge connecting them.
 In a directed graph G, if node v is adjacent to node u, then there is definitely an edge from u to v.
That is, if v is adjacent to u, we can get from u to v by traversing one edge. For any graph G having
n nodes, the adjacency matrix will have the dimension of n * n.
 In an adjacency matrix, the rows and columns are labeled by graph vertices. An entry aij in the
adjacency matrix will contain 1, if vertices vi and vj are adjacent to each other. However, if the
nodes are not adjacent, aij will be set to zero.
 An adjacency matrix contains only 0s and 1s, it is called a bit matrix or a Boolean matrix. The
entries in the matrix depend on the ordering of the nodes in G. Therefore, a change in the order of
nodes will result in a different adjacency matrix.
 Example: directed and undirected graphs with adjacency matrix.

Prof. Shrikant Pujar, Dept. of CS&E 3


DATA STRUCTURES AND APPLICATIONS
5.3.2 Adjacency list:
 An adjacency list is a way in which graphs can be represented in the computer’s memory. This
structure consists of a list of all nodes in G. Furthermore, every node is in turn linked to its own list
that contains the names of all other nodes that are adjacent to it.
 The key advantages of using an adjacency list are:
 It is easy to follow and clearly shows the adjacent nodes of a particular node.
 It is often used for storing graphs that have a small-to-moderate number of edges. That is, an
adjacency list is preferred for representing sparse graphs in the computer’s memory; otherwise,
an adjacency matrix is a good choice.
 Adding new nodes in G is easy and straightforward when G is represented using an adjacency
list. Adding new nodes in an adjacency matrix is a difficult task, as the size of the matrix needs
to be changed and existing nodes may have to be reordered. Each node has a list of adjacent
nodes.
Example: directed and undirected graphs with adjacency list.

 Adjacent list and Adjacent Matrix representation of weighted graph.

Prof. Shrikant Pujar, Dept. of CS&E 4


DATA STRUCTURES AND APPLICATIONS
5.4 Basic Operations on Graphs
Following are basic primary operations of a Graph −
 Add Vertex − Adds a vertex to the graph.
 Delete Vertex – Deletes a vertex from the graph
 Add Edge − Adds an edge between the two vertices of the graph.
 Delete Edge – Deletes an edge from the graph
 Display Vertex − Displays a vertex of the graph.

5.5 Traversal methods:


5.5.1. Breadth First Search:
 Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all
the neighbouring nodes. Then for each of those nearest nodes, the algorithm explores their
unexplored neighbour nodes, and so on, until it finds the goal.
 That is, we start examining the node A and then all the neighbours of A are examined. In the next
step, we examine the neighbours of neighbours of A, so on and so forth. This means that we need to
track the neighbours of the node and guarantee that every node in the graph is processed and no node
is processed more than once. This is accomplished by using a queue that will hold the nodes that are
waiting for further processing.
 Algorithm of BFS:

Example: Traverse the following graph by BFS and print all the vertices reachable from start vertex
a.

Prof. Shrikant Pujar, Dept. of CS&E 5


DATA STRUCTURES AND APPLICATIONS

5.5.2. Depth First Search:


 Depth-first search begins at a starting node A which becomes the current node. Then, it examines
each node N along a path P which begins at A. That is, we process a neighbour of A, then a
neighbour of neighbour of A, and so on.
 During the execution of the algorithm, if we reach a path that has a node N that has already been
processed, then we backtrack to the current node. Otherwise, the unvisited (unprocessed) node
becomes the current node. The algorithm proceeds like this until we reach a dead-end (end of path
P). On reaching the deadend, we backtrack to find another path P. The algorithm terminates when
backtracking leads back to the starting node A.
 In this algorithm, edges that lead to a new vertex are called discovery edges and edges that lead to
an already visited vertex are called back edges. Observe that this algorithm is similar to the in-order
traversal of a binary tree. Its implementation is similar to that of the breadth first search algorithm
but here we use a stack instead of a queue.
 Algorithm of DFS:

Prof. Shrikant Pujar, Dept. of CS&E 6


DATA STRUCTURES AND APPLICATIONS
Example: Traverse the following graph by DFS and print all the vertices reachable from start vertex
a.

The vertices reachable from start vertex a are b, d, f, g, e.

5.6 Sorting
 Sorting means arranging the elements of an array so that they are placed in some relevant order
which may be either ascending or descending.

5.6.1 Insertion sort


 Insertion sort is a very simple sorting algorithm in which the sorted array (or list) is built one
element at a time. We all are familiar with this technique of sorting, as we usually use it for ordering
a deck of cards while playing bridge. The main idea behind insertion sort is that it inserts each item
into its proper place in the final list.
 To save memory, most implementations of the insertion sort algorithm work by moving the current
data element past the already sorted values and repeatedly interchanging it with the preceding value
until it is in its correct place.
 Insertion sort is less efficient as compared to other more advanced algorithms such as quick sort,
heap sort, and merge sort.

Prof. Shrikant Pujar, Dept. of CS&E 7


DATA STRUCTURES AND APPLICATIONS
Technique:
1. The array of values to be sorted is divided into two sets. One that stores sorted values and
another that contains unsorted values.
2. The sorting algorithm will proceed until there are elements in the unsorted set.
3. Suppose there are n elements in the array. Initially, the element with index 0 (assuming LB = 0)
is in the sorted set. Rest of the elements are in the unsorted set.
4. The first element of the unsorted partition has array index 1 (if LB = 0).
5. During each iteration of the algorithm, the first element in the unsorted set is picked up and
inserted into the correct position in the sorted set.

Example: Sort the following list using insertion sort – 77, 33, 44, 11, 88, 22, 66, 55

ALGORITHM INSERTION-SORT (ARR, N)


Step 1: Repeat Steps 2 to 5 for K = 1 to N–1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SETJ=J-1 [END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP [END OF LOOP]
Step 6: EXIT

To insert an element A[K] in a sorted list A[0], A[1], ..., A[K–1], we need to compare A[K] with
A[K–1], then with A[K–2], A[K–3], and so on until we meet an element A[J] such that A[J] <=
A[K]. In order to insert A[K] in its correct position, we need to move elements A[K– 1], A[K–2], ...,
A[J] by one position and then A[K] is inserted at the (J+1)th location.
Prof. Shrikant Pujar, Dept. of CS&E 8
DATA STRUCTURES AND APPLICATIONS
5.6.2 Radix Sort
 Radix sort is a linear sorting algorithm for integers and uses the concept of sorting names in
alphabetical order.
 When we have a list of sorted names, the radix is 26 (or 26 buckets) because there are 26 letters in
the English alphabet. So radix sort is also known as bucket sort. Observe that words are first sorted
according to the first letter o/f the name. That is, 26 classes are used to arrange the names, where the
first class stores the names that begin with A, the second class contains the names with B, and so on.
 During the second pass, names are grouped according to the second letter. After the second pass,
names are sorted on the first two letters. This process is continued till the nth pass, where n is the
length of the name with maximum number of letters.
 After every pass, all the names are collected in order of buckets. That is, first pick up the names in
the first bucket that contains the names beginning with A. In the second pass, collect the name from
the second bucket, and so on.
 When radix sort is used on integers, sorting is done on each of the digits in the number. The sorting
procedure proceeds by sorting the least significant to the most significant digit. While sorting the
numbers, we have ten buckets, each for one digit (0, 1, 2, …, 9) and the number of passes will
depend on the length of the number having maximum number of digits.

Algorithm for RadixSort (ARR, N)


Step 1: Find the largest number in ARR as LARGE
Step 2: [INITIALIZE] SET NOP = Number of digits in LARGE
Step 3: SET PASS = 0
Step 4: Repeat Step 5 while PASS <= NOP-1
Step 5: SET= I=0 and INITIALIZE buckets
Step 6: Repeat Steps 7 to 9 while I<N-1
Step 7: SET DIGIT = digit at PASSth place in A[I]
Step 8: Add A[I] to the bucket numbered DIGIT
Step 9: INCREMENT bucket count for bucket numbered DIGIT [END OF LOOP]
Step 10: Collect the numbers in the bucket [END OF LOOP]
Step 11: END

Example: Sort the following list using Radix sort – 348, 143, 361, 423, 538, 128, 321, 543,366

Prof. Shrikant Pujar, Dept. of CS&E 9


DATA STRUCTURES AND APPLICATIONS

Sorted numbers are- 128, 143, 321, 348, 361, 366, 423, 538, 543.
 Difference between Graph and Tree

Prof. Shrikant Pujar, Dept. of CS&E 10


DATA STRUCTURES AND APPLICATIONS
5.7 Hashing
 Hashing is the process of mapping large amount of data item to smaller table with the help
of hashing function.
 Hashing is also known as Hashing Algorithm or Message Digest Function.
 It is a technique to convert a range of key values into a range of indexes of an array.
 It is used to facilitate the next level searching method when compared with the linear or binary
search.
 Hashing allows updating and retrieving any data entry in a constant time.
 Constant time means the operation does not depend on the size of the data.
 Hashing is used with a database to enable items to be retrieved more quickly.
 It is used in the encryption and decryption of digital signatures.

5.7.1 Hash Function:


 A fixed process converts a key to a hash key is known as a Hash Function.
 This function takes a key and maps it to a value of a certain length which is called a Hash
value or Hash.
 Hash value represents the original string of characters, but it is normally smaller than the
original.
 It transfers the digital signature and then both hash value and signature are sent to the receiver.
Receiver uses the same hash function to generate the hash value and then compares it to that
received with the message.
 If the hash values are same, the message is transmitted without errors.

Types of hash function


There are various types of hash function which are used to place the data in a hash table,
1. Division method: In this the hash function is dependent upon the remainder of a division.
H(key)=record% table size.
For example:-if the record 52,68,99,84 is to be placed in a hash table and let us take the table size is
10.
Then: h(key)=record% table size.
2=52%10
8=68%10
9=99%10
4=84%10

2. Mid square method: In this method firstly key is squared and then mid part of the result is taken
as the index.
H(key) = Key2 = mid(key)
For example: consider that if we want to place a record of 3101 and the size of table is 1000. So
3101*3101=9616201 i.e. h (3101) = 162 (middle 3 digit).

Prof. Shrikant Pujar, Dept. of CS&E 11


DATA STRUCTURES AND APPLICATIONS
3. Digit folding method: In this method the key is divided into separate parts and by using some
simple operations these parts are combined to produce a hash key.
H(k) = k1 + k2 + k3 + k4+ . . . . . . +kn
For example: consider a record of 12465512 then it will be divided into parts i.e. 124, 655, 12. After
dividing the parts combine these parts by adding it.
H(key) = 124 + 655 + 12 = 791

5.7.2 Hash Table


 Hash table or hash map is a data structure used to store key-value pairs.
 It is a collection of items stored to make it easy to find them later.
 It uses a hash function to compute an index into an array of buckets or slots from which the
desired value can be found.
 It is an array of list where each list is known as bucket.
 It contains value based on the key.
 Hash table is used to implement the map interface and extends Dictionary class.

Collision
 It is a situation in which the hash function returns the same hash key for more than one
record, it is called as collision. Sometimes when we are going to resolve the collision it may
lead to a overflow condition and this overflow and collision condition makes the poor hash
function.
Collision resolution technique
 If there is a problem of collision occurs then it can be handled by apply some technique. These
techniques are called as collision resolution techniques.

1) Chaining: It is a method in which additional field with data i.e. chain is introduced. A chain is
maintained at the home bucket. In this when a collision occurs then a linked list is maintained for
colliding data.

Example: Let us consider a hash table of size 10 and we apply a hash function of H(key)=key %
size of table. Let us take the keys to be inserted are 31,33,77,61. In the above diagram we can see at
same bucket 1 there are two records which are maintained by linked list or we can say by chaining
method.
Prof. Shrikant Pujar, Dept. of CS&E 12
DATA STRUCTURES AND APPLICATIONS
2) Linear probing: It is very easy and simple method to resolve or to handle the collision. In this
collision can be solved by placing the second record linearly down, whenever the empty place is
found. In this method there is a problem of clustering which means at some place block of a data is
formed in a hash table.
Example: Let us consider a hash table of size 10 and hash function is defined as H(key)=key %
table size. Consider that following keys are to be inserted that are 56,64,36,71.

 In this diagram we can see that 56 and 36 need to be placed at same bucket but by linear probing
technique the records linearly placed downward if place is empty i.e. it can be seen 36 is placed
at index 7.

5.7.3 Static and Dynamic Hashing:


Static Hashing: This is the process of mapping large amounts of data into a table whose size is fixed
during compilation time.

Dynamic Hashing: This is the process of mapping large amounts of data into a table whose size is
assigned during run time.

5.8 Files and Their Organization:


Data Hierarchy:
 The systematic organization of data refers to data hierarchy. It involves various data items such as,
data fields, records, files and database.

File Attributes:
 File attributes are settings associated with computer files that grant or deny certain rights to how a
user or the operating system can access that file. For example, IBM compatible computers running
MS-DOS or Microsoft Windows have capabilities of having read, archive, system, and hidden
attributes.
 Read-only - Allows a file to be read, but nothing can be written to the file or changed.
 Archive - Tells Windows Backup to backup the file.
 System - System file.
 Hidden - File is not shown when doing a regular directory from DOS.
Prof. Shrikant Pujar, Dept. of CS&E 13
DATA STRUCTURES AND APPLICATIONS
In operating systems like Linux, there are three main file attributes: read (r), write (w), execute (x).
 Read - Designated as an "r"; allows a file to be read, but nothing can be written to or changed in
the file.
 Write - Designated as a "w"; allows a file to be written to and changed.
 Execute - Designated as an "x"; allows a file to be executed by users or the operating system.

Text Files and Binary Files:


 A text file stores data in the form of alphabets, digits and other special symbols by storing their
ASCII values and are in a human readable format. For example, any file with a .txt, .c, etc
extension.
 a binary file contains a sequence or a collection of bytes which are not in a human readable
format. For example, files with .exe, .mp3, etc extension.

Basic File Operations:


1) Read Operation: Meant To Read the information which is Stored into the Files.
2) Write Operation: For inserting some new Contents into a File.
3) Rename or Change the Name of File.
4) Copy the File from one Location to another.
5) Sorting or Arrange the Contents of File.
6) Move or Cut the File from One Place to Another.
7) Delete a File
8) Execute Means to Run Means File Display Output.

File Organizations and Indexing:


There are three types of organizing the file:
1. Sequential access file organization
2. Direct access file organization
3. Indexed sequential access file organization

1. Sequential access file organization


 Storing and sorting in contiguous block within files on tape or disk is called as sequential access file
organization.
 In sequential access file organization, all records are stored in a sequential order. The records are
arranged in the ascending or descending order of a key field.
 Sequential file search starts from the beginning of the file and the records can be added at the end of
the file.
 In sequential file, it is not possible to add a record in the middle of the file without rewriting the file.

Advantages of sequential file


 It is simple to program and easy to design.
 Sequential file is best use if storage space.

Prof. Shrikant Pujar, Dept. of CS&E 14


DATA STRUCTURES AND APPLICATIONS
Disadvantages of sequential file
 Sequential file is time consuming process.
 It has high data redundancy.
 Random searching is not possible.

2. Direct access file organization


 Direct access file is also known as random access or relative file organization.
 In direct access file, all records are stored in direct access storage device (DASD), such as hard disk.
The records are randomly placed throughout the file.
 The records does not need to be in sequence because they are updated directly and rewritten back in
the same location.
 This file organization is useful for immediate access to large amount of information. It is used in
accessing large databases.
 It is also called as hashing.

Advantages of direct access file organization


 Direct access file helps in online transaction processing system (OLTP) like online railway
reservation system.
 In direct access file, sorting of the records are not required.
 It accesses the desired records immediately.
 It updates several files quickly.
 It has better control over record allocation.

Disadvantages of direct access file organization


 Direct access file does not provide backup facility.
 It is expensive.
 It has less storage space as compared to sequential file.

3. Indexed sequential access file organization


 Indexed sequential access file combines both sequential file and direct access file organization.
 In indexed sequential access file, records are stored randomly on a direct access device such as
magnetic disk by a primary key.
 This file have multiple keys. These keys can be alphanumeric in which the records are ordered is
called primary key.
 The data can be access either sequentially or randomly using the index. The index is stored in a file
and read into memory when the file is opened.

Advantages of Indexed sequential access file organization


 In indexed sequential access file, sequential file and random file access is possible.
 It accesses the records very fast if the index table is properly organized.
 The records can be inserted in the middle of the file.
 It provides quick access for sequential and direct processing.

Prof. Shrikant Pujar, Dept. of CS&E 15


DATA STRUCTURES AND APPLICATIONS
Disadvantages of Indexed sequential access file organization
 Indexed sequential access file requires unique keys and periodic reorganization.
 Indexed sequential access file takes longer time to search the index for the data access or
retrieval.
 It requires more storage space.
 It is expensive because it requires special software.
 It is less efficient in the use of storage space as compared to other file organizations.

QUESTION BANK

1. Define the following terminologies with examples.


i. Graph ii. Complete graph iii. Multigraph
iv. Directed & undirected graph v. weighted and unweighted graph
2. Give the adjacent matrix and adjacency list representation of following graphs.

3. Write an algorithm for DFS and BFS graph traversals.


4. Apply insertion sort technique for the following elements: 77, 33, 44, 11, 88, 22, 66, 55.
5. Explain hashing and collision. What are methods used to resolve collision.
6. What are the basic operations that can be performed on a file? List the methods used for file
organization (any two).
7. Write difference between graph and tree.
8. What is collision? Explain Linear probing with an example.
9. Write an algorithm for insertion sort and sort the list: 50, 30, 10, 70, 40, 20, 60.
10. Explain the radix sort with example.
11. Define hashing and Explain Hashing Functions.

Prof. Shrikant Pujar, Dept. of CS&E 16

You might also like