Module 1
Module 1
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
Creating
Inserting
Deleting
Traversing
Searching
Sorting
Merging
Tagged Structure
Syntax :
struct tag_name
{
datatype variable 1;
datatype variable 2;
.
.
};
struct student
{
char name[10];
int roll_number;
float average_marks;
};
Syntax :
struct
{
datatype variable 1;
datatype variable 2;
.
.
}variable1, variable 2, variable 3, ….. variable n;
struct
{
char name[10];
int roll_number;
float average_marks;
}cse, ise ;
Syntax :
typedef struct
{
datatype variable 1;
datatype variable 2;
.
.
}type_Id;
typedef struct
{
char name[10];
int roll_number;
float average_marks;
}STUDENT;
struct student
{
char name[10];
int roll_number;
float average_marks;
}cse = {“Raghu”, 42, 40};
or
struct student cse = {“Raghu”, 42, 40};
➢ student name
➢ roll_number
➢ average_marks
union union_name
{
data type variable_name1;
data type variable_name2;
.
.
};
& Address of
* Value at Address
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.
➢ malloc (size)
➢free (ptr)
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.
Thus total number of bytes allocated is n*size and all bytes will
be initialized to 0.
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.
#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.
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
typedef struct {
float coef;
int expon;
} polynomial ;
polynomial a[MAX_TERMS] ;
int avail = 0;
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.