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

Module 1

mc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 1

mc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 119

DATA STRUCTURES AND

APPLICATIONS
(BCS304)
Module 1

By:
Mr.RAGHAVENDRACHAR S,
Associate professor,
Dept of CSE,
KSIT,Bengaluru.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Definition of Data Structure

 A Data Structure is basically a group of data elements


that are put together under one name and defines a
particular way of storing and organizing data in a
computer (computer memory) so that it can be used
efficiently.
Example : int a =10;
int marks[5] = {20, 25, 28, 29, 30};

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Classification of Data Structure

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Primitive Data Structures : The fundamental data types
which are supported by a programming language.
Example : Integer, Float, Character.
 Non primitive Data Structures : Which are created using
primitive Data Structures.
Example : Arrays, Lists, Files.
 Linear Lists : Elements are Stored in Linear or Sequential
Order (One after the other).
Example : Stacks, Queues.
 Non linear Lists : Elements are not stored in Sequential Order.
Example : Graphs, Trees.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Operations of Data Structure

 Creating
 Inserting
 Deleting
 Traversing
 Searching
 Sorting
 Merging

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Review of Structures

 Tagged Structure

 Structure without Tag

 Type defined Structure

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Tagged Structure

 Syntax :

struct tag_name
{
datatype variable 1;
datatype variable 2;
.
.
};

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Example :

struct student
{
char name[10];
int roll_number;
float average_marks;
};

struct student cse, ise ;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Structure without tag

 Syntax :

struct
{
datatype variable 1;
datatype variable 2;
.
.
}variable1, variable 2, variable 3, ….. variable n;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Example :

struct
{
char name[10];
int roll_number;
float average_marks;
}cse, ise ;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Type-defined Structure

 Syntax :

typedef struct
{
datatype variable 1;
datatype variable 2;
.
.
}type_Id;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Example :

typedef struct
{
char name[10];
int roll_number;
float average_marks;
}STUDENT;

STUDENT cse, ise;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Structure Initialization

struct student
{
char name[10];
int roll_number;
float average_marks;
}cse = {“Raghu”, 42, 40};

or
struct student cse = {“Raghu”, 42, 40};

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Write a C program to access student information by creating
structure student with the following data fields.

➢ student name
➢ roll_number
➢ average_marks

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 Write a C Program with an appropriate structure definition and
variable declaration to read and display information about 5
employees using nested structure consider the following fields
like Ename, EmpId, DOJ (Date, Month, Year) and Salary
(Basic, DA, HRA).

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Unions

 A Union is a special data type available in C that allows to


store different data types in the same memory location.

 A Union is same as that of the structure, but in structure all


structure members uses different memory locations, but in
union all union members uses same memory location only one
member can contain a value at any given time.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Syntax :

union union_name
{
data type variable_name1;
data type variable_name2;
.
.
};

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Structure v/s Union

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Pointers

 Steps to be followed while using pointers

➢ Declare a data variable -> Example : int a;

➢ Declare a pointer variable -> Example : int *p;

➢ Initialize a pointer variable -> Example : p = &a;

➢ Access a data using pointer variable ->


Example : printf(“%d”, *p);

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
NOTE

& Address of
* Value at Address

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 C Program to add two numbers using pointers

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Dangling Pointer

 A Pointer variable which does not contain a valid address


is called dangling pointer.
 Example : Assume all the following pointer variables are
local.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
NULL Pointer

 A NULL Pointer is defined as a special pointer value that


points nowhere in the memory.
 If it is too early in the code to assign a value to the pointer,
then it is better to assign NULL (i.e \0) to the pointer.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Pointers and functions
( Pass by Reference)

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Arrays and Pointers

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 C Program to read N array elements and print the
same by using pointers.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 C Program to read find sum of n numbers using
pointers.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Self Referential Structure
typedef struct
{
char data;
struct list *link;
}list;

list item1, item2, item3;

item1.data =‘a’;
item2.data =‘b’;
item3.data = ‘c’;

item1.link = &item2;
item2.link=&item3;
item1.link = item2.link = item3.link = NULL;
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Static allocation v/s Dynamic allocation
Static allocation Dynamic allocation
1. Memory is allocated during 1. Memory is allocated during
compilation time. execution time.

2. The size of the memory to be 2. Memory can be allocated when it is


allocated is fixed during compilation required and memory can be
time and can not be altered during deallocated when it is not required.
execution time.
3. Execution is faster, since memory is 3. Execution is slower, since memory
allocated and data manipulation is has to be allocated during run time,
done on these allocated memory data manipulation is done only after
locations. allocation of the memory.
4. Memory is allocated either in stack 4. Memory is allocated only in heap
area or data area. area.
5. Example : Arrays 5. Example: Dynamic arrays, Linked
lists, Trees.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Dynamic Memory Allocation Functions

➢ malloc (size)

➢ calloc (n, size)

➢realloc (ptr, size)

➢free (ptr)

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
malloc (size)

 The function allows the program to allocate memory explicitly


as and when required and the exact amount needed during
execution.

 This function allocates a block of memory.

 The size of the block is number of bytes specified in the


parameter.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 On successful allocation, the function returns the address of
first byte of allocated memory. Since the address is returned,
the return type is a void pointer. By type casting appropriately
we can use it to store integer, float etc.

 If specified size of the memory is not available, the condition is


called “Overflow of memory”. In such a case, the function
returns NULL.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Syntax :
#include <stdlib.h>
………………….
………………….
ptr = (datatype * ) malloc (size) ;

if (ptr == NULL)
{
printf(“In sufficient memory \n”);
exit(0);
}
………………….
Where
 ptr is a pointer variable of type datatype.
 datatype can be any of the basic datatype or user defined datatype.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 C program to show the usage of malloc() function.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
calloc (n, size)

 This function is used to allocate multiple blocks of memory.


Here, calloc stands for contiguous allocation of multiple
blocks and is mainly used to allocate memory for arrays.

 The number of blocks is determined by the first parameter n.

 The size of each block is equal to the number of bytes specified


in the parameter i.e., size.

 Thus total number of bytes allocated is n*size and all bytes will
be initialized to 0.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Syntax :
#include <stdlib.h>
………………….
………………….
ptr = (datatype * ) calloc (n,size) ;

if (ptr == NULL)
{
printf(“In sufficient memory \n”);
exit(0);
}
………………….
Where
 ptr is a pointer variable of type datatype.
 datatype can be any of the basic datatype or user defined datatype.
 n is the number of blocks to be allocated.
 size is the number of bytes in each block.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 C program to show the usage of calloc() function.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
realloc (ptr, size)
 Before using this function the memory should have been allocated using
malloc() or calloc().
 Sometimes the allocated memory may not be sufficient and we may require
additional memory space.
 Sometimes the allocated memory may be much larger and we want to
reduce the size of the allocated memory.
 In both situations the size of the allocated memory can be changed using
realloc() and process is called reallocation of memory.
 realloc() changes the size of the block by extending or deleting the memory
at the end of block.
 If the existing memory can be extended ptr value will not be changed.
 If the memory can not be changed this function allocates a completely new
block and copies the contents of existing memory in to new block and then
deletes the old memory block.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Syntax :

#include <stdlib.h>
………………….
………………….
ptr = (datatype * ) realloc (ptr,size) ;
………………….
………………….
………………….
Where
 ptr is a pointer to a block of previously allocated memory either using malloc() or
calloc().
 datatype can be any of the basic datatype or user defined datatype.
 size is the new size of the block.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 C program to show the usage of realloc() function.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
free (ptr)

 The function is used to de allocate ( or free) the allocated block


of memory which is allocated by using malloc(), calloc(),
realloc().

 It is the responsibility of a programmer to de allocate memory


whenever it is not required by the program and initialize ptr to
NULL.
 Syntax :
#include <stdlib.h>
………………….
free (ptr);
ptr = NULL;
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 C program to implement two dimensional array
dynamically.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Multidimensional Array
Two Dimensional Array

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 The Address of any element A[i , j] is calculated as

column major order :


LOC (A[i , j] ) = Base(A) + W [ m (j – 1) + (i - 1) ]

Row major order :


LOC (A[i , j] ) = Base(A) + W [ n (i – 1) + (j - 1) ]

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
General Multidimensional Arrays

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
SPARSE MATRICES

 Sparse Matrix is two dimensional array of size m X n in which


most of the elements are zero.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Sparse Matrix as Array of Triplets

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Sparse Matrix Representation

# define MAX_TERMS 101

typedef struct {
int col;
int row;
int value;
} term ;

term a[MAX_TERMS] ;
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Transpose of sparse Matrix

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Fast Transpose

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Polynomials

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Polynomial Representation

# define MAX_TERMS 100

typedef struct {
float coef;
int expon;
} polynomial ;

polynomial a[MAX_TERMS] ;

int avail = 0;

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Array Representation of Two Polynomials

Where : start A -> index of first term of A(x)


startB -> index of first term of B(x)
finish A -> index of last term of A(x)
finish B -> index of last term of B(x)
Avail -> index of next free location in Array term
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Polynomial Addition

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Pattern Matching Algorithms

 Algorithm 1 : Pattern Matching By Checking End Indices


First

 Algorithm 2: Knuth, Morris and Patt ( KMP) Pattern


Matching Algorithm.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Pattern Matching By Checking End Indices First
 STR -> Main String

 PAT -> Pattern String

 lasts -> Points to last character of STR

 lastp -> Points to last character of PAT

 start = 0 ( Initially points to first character of STR)

 endmatch = lastp (endmatch intially points to a character of


STR, which is at a position equal to length of PAT, starting
from first character of STR)
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Steps
 a) Compare a last character of PAT with character of STR at a position
equal to length of PAT i.e.
STR[endmatch] == PAT[lastp]

 b) If character in above step matches,


compare STR[start] and PAT[j], where j is pointing to first character of
PAT
i) If match, continue the matching second character and so on
ii) If no match goto step C

 c) Increment start to point to next character in STR, increment endmatch ,


then compare , STR[endmatch] == PAT[lastp]

 d) Repeat Step b and c until end of STR, if PAT in STR , display position
of first match of PAT
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 Let STR is “ a b a b b a a b a a” and PAT is “a a b”
perform pattern matching by checking end indices first.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 1 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 2:

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 3:

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 4:

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 5:

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 6:

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Knuth, Morris and Patt ( KMP) Pattern Matching
Algorithm.

 Proper Prefix : All the characters in a string with one or more


cut off the end are called proper prefix.
Example : For String KSIT
Proper Prefixes are 1. KSI
2. KS
3. K

Note : But whole string is not proper prefix of itself

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Proper Suffix : All the characters in a string with one or more
cut off the beginning are called proper suffix.
Example : For String KSIT
Proper Suffixes are 1. SIT
2. IT
3. T

Note : But whole string is not proper suffix of itself

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Search pattern “ a b c d a b c y” in the text (main string)
“ a b c x a b c d a b x a b c d a b c d a b c y”
using KMP algorithm.

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
Mr.Raghavendrachar.S , Associate Professor,
Dept of CSE, KSIT.
 Step 1 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 2 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 3 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 4 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.
 Step 5 :

Mr.Raghavendrachar.S , Associate Professor,


Dept of CSE, KSIT.

You might also like