1. RECURSION CONCEPT BY DIVYA
Recursion
Recursive functions are those functions which call
themselves directly or indirectly.
Few examples which uses recursive functions are:
1. Calculation of factorial of a number
2. Fibonacci series
3. Printing values of nodes of single linked list in reverse order
4. Tower of Hanoi
5. Tree traversal( Preorder, Postorder, Inorder)
6. Depth First Search of Graph
How recursion works?
Whenever we try to solve a problem, by representing it
in one or more smaller sub problems, it is used. To stop
the recursion a base condition is used.
The memory allocation is done to a function on a stack,
whenever it is called from main() function.
A recursive function calls itself, the memory for a called
function is allocated on top of memory allocated to
calling function and different copy of local variables is
created for each function call. When the base case is
reached, the function returns its value to the function by
whom it is called and memory is de-allocated and the
process continues.
2. RECURSION CONCEPT BY DIVYA
Calculation of factorial of a number
#include<stdio.h>
int fact(int n)
{
if (n <= 1) // base case
return 1;
else
return n*fact(n-1);
}
int main()
{
int num,X;
printf("Enter the number ");
scanf("%d",&num);
X=fact(num);
printf("nfactorial of the number %d is %d",num,X);
return 0;
}
How it works if num=3
3. RECURSION CONCEPT BY DIVYA
X=fact(3)
=3 * fact(2)
=3 * 2 * fact(1)
=3 * 2 * 1
Printing values of nodes of single linked list in reverse
order
#include<stdio.h>
#include<stdlib.h> /*free( ), malloc( )*/
struct node{
int info;
struct node *link;
}*head=NULL;
void create_list(int n)
{
struct node *N=NULL;
struct node *M=NULL;
int i;
for(i=0;i<n;i++)
{
if(head==NULL)
{
N=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in 1st node");
scanf("%d", &N->info);
N->link=NULL;
head=N;
}
else
4. RECURSION CONCEPT BY DIVYA
{
M=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in %d node",(i+1));
scanf("%d", &M->info);
M->link=NULL;
N->link=M;
N=M;
}
}
}
void display_list()
{
struct node *temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->info);
temp=temp->link;
}
}
void reverse_list(struct node *temp)
{
if(temp==NULL)
return;
reverse_list(temp->link);
printf(" %d ",temp->info);
}
int main()
{
int num_nodes;
printf("Enter the number of nodes you want to create");
scanf("%d",&num_nodes);
create_list(num_nodes);
5. RECURSION CONCEPT BY DIVYA
printf("Output");
display_list();
printf("nList in reverse ordern");
reverse_list(head);
return 0;
}
STEP WISE DEPICTION AFTER EXECUTION OF
RECURSIVE FUNCTION :reverse_list()
1.
8. RECURSION CONCEPT BY DIVYA
Print all the node’s info of a circular single
linked list in reverse order:
#include<stdio.h>
#include<stdlib.h> /*free( ), malloc( )*/
struct node{
int info;
struct node *link;
}*head=NULL,*last;
void create_list(int n)
{
struct node *N=NULL;
struct node *M=NULL;
int i;
for(i=0;i<n;i++)
9. RECURSION CONCEPT BY DIVYA
{
if(head==NULL)
{
N=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in 1st node");
scanf("%d", &N->info);
if(n==1) /*if user wants to create only one node*/
{
N->link=N;/*as it is circular list the node's link will point to
node itself*/
last=N;
}
else
N->link=NULL;
head=N;
}
else
{
M=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in %d node",(i+1));
scanf("%d", &M->info);
if(i==n-1)/*when last node of list is created*/
{
M->link=head;
last=M; /* assign last pointer the address of node*/
}
else
{
M->link=NULL;
}
N->link=M;
N=M;
}
}
}
void display_list()
10. RECURSION CONCEPT BY DIVYA
{
struct node *temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->info);
if(temp==last) /*when last node of list is reached*/
{
printf("n verify the list is circular linked list");
printf("nvalue of info of last node's link
node %d",last->link->info);
break;
}
temp=temp->link;
}
}
int main()
{
int num_nodes;
printf("Enter the number of nodes you want to create");
scanf("%d",&num_nodes);
create_list(num_nodes);
printf("Output");
display_list();
printf("nprint elements in reversen");
reverse_list(head);