Day 1 Introduction To Data Structures: Vritika Naik Twitter: @naikvritika Linkedin: Vritika Naik
Day 1 Introduction To Data Structures: Vritika Naik Twitter: @naikvritika Linkedin: Vritika Naik
Day 1 Introduction To Data Structures: Vritika Naik Twitter: @naikvritika Linkedin: Vritika Naik
INTRODUCTION TO DATA
STRUCTURES
VRITIKA NAIK
Twitter: @NaikVritika
LinkedIn: Vritika Naik
• Data Types
• Data Structures
• Algorithms
TOPICS TO • Arrays
• Pointers
BE • Dynamic Memory Allocation
COVERED • Structures
• Recursion
Data Type
type *var-name;
Function Pointers
• Like normal data pointers(int *, char *, etc), we can have pointers to functions.
#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;
(*fun_ptr)(10);
return 0;
}
What is so special about function pointers?
calloc() function
calloc() function allocates memory contiguously.
Syntax
void* calloc(number_of_blocks, number_of_bytes);
realloc() function
Alters/update the size of an existing allocated memory blocks (which has been created by either malloc() or calloc()).
Syntax
void* realloc(ptr, updated_memory_size);
free() function
Clears the pointer (assigns NULL to the pointer) to clear the dynamically allocated memory.
Syntax
free(ptr);
Structures
A structure creates a data type that can be used to group items of possibly different types into a single type.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1}; p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Recursion
PROCESS OF CALLING ITSELF
Sum of n natural
numbers
int sum(int n)
{
if (n != 0) // sum() function calls
itself
return n + sum(n-1);
else return n;
}
Factorial
void display1(int n)
{
if( n==0 )
return;
printf("%d ",n);
display1(n-1);
}/*End of display1()*/
Displaying numbers
void display2(int n)
{
if( n==0 )
return;
display2(n-1);
printf("%d ",n);
}/*End of display2()*/
Power
int fib(int n)
{
if(n==0 || n==1)
return(1);
return(fib(n-1) + fib(n-2));
}
Tail Recursion: A call is tail-recursive if Head Recursion: A call is head-recursive
nothing has to be done after the call returns i.e. when the first statement of the function is the
when the call returns, the returned value is recursive call.
immediately returned from the calling function