Module 1
Module 1
MODULE 1
Function call
ptr=(data_type *)malloc(size);
calloc is a function for dynamic memory allocation in C language stdlib.h header malloc is a function for dynamic memory allocation in C language stdlib.h header
file that allocates a specific number of bytes and initializes them to zero. file that allocates a specific number of bytes.
Meaning
calloc stands for contiguous allocation. malloc stands for memory allocation.
Syntax
calloc follows a syntax similar to void *calloc(size_t_num, size_t size); malloc follows a syntax similar to void *malloc(size_t_size);.
Number of Arguments
calloc takes two arguments. They are a number of blocks and size of each block. malloc takes one argument. It is a number of bytes.
Speed
calloc takes little longer than malloc. That is because of the extra step of
malloc is faster than calloc.
initializing the allocated memory by zero.
Notations:
list2 is a pointer to list2[0]
(list2 + i) is a pointer to list2[i], that is &list2[i]
*(list2 + i) is nothing but list2[i]
Example : struct
{
char name[10];
int age;
float salary;
} E1,E2;
Note: E1 and E2 are structure variables
typedef struct {
int month;
int day;
int year;
} DATE;
typedef struct {
char name[10];
int age;
float salary;
DATE dob;
} HUMAN_BEING;
HUMAN_BEING person1;
person1.dob.month = 2;
person1.dob.day = 11;
person1.dob.year = 1944;
Availaible at VTU HUB (Android App)
Self Referential Structures
One or more of its components is a pointer to itself.
typedef struct {
char data; Construct a list with three nodes
struct list *link; item1.link=&item2;
} list ; item2.link=&item3;
malloc: obtain a node
list item1, item2, item3;
item1.data=„a‟;
a b c
item2.data=„b‟;
item3.data=„c‟;
item1.link = item2.link = item3.link = NULL;
4 bytes
10 20 30 40 50
0 1 2 3 4 5 …..?????
10 20 30 40 50
0 1 2 3 4 5 …..?????
10 20 30 40 50
0 1 2 3 4 5
…..?????
10 20 30 40 50
0 1 2 3 4 5
…..?????
10 20 30 40 50
0 1 2 3 4 5
…..?????
10 20 30 40 50
0 1 2 3 4 5
…..?????
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42Swap
77 77
42 35 12 101 5
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
1 2 3 4 5 6
42 35 12Swap12
77 77 101 5
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
CS 307 Fundamentals of Computer
87 Science
Availaible at VTU HUB (Android App)
bubble sort
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
1 2 3 4 5 6
42 35 12 77 5 101
20 10 40 5
20 > 10 40 5
10 > 20 5 40
10 > 5 20 40
30
10 20 30 40 50
0 1 2 3 4 5
How to
search…..?????
One can also show that the running time for the average case
is approximately equal to the running time for the worst case
that is Log2n
Coef 2 1 1 10 3 1
Expon 1000 0 4 3 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Dense matrix
sparse matrix
col1 col2 col3 col4 col5 col6 < row column Value>
15 0 0 22 0 15
row0
row1 0 11 3 0 0 0
row2 0 0 0 6 0 0
row3 0 0 0 0 0 0
row4
91 0 0 0 0 0
row5
0 0 28 0 0 0
This INSERT function can be implemented using string operation as given below
ie, Initial substring of T before position K,which has length K-1 is concatenated with string S
and result is concatenated with remaining part of T which begins in position K and has
LENGTH(T)-(K-1)= Length(T)-K+1