# Recursion

We discussed Insertion sort in detail earlier. This post shows the recursive implementation of Insertion Sort.

Given a string, print the variation of string as shown below Input String: ‘hat’ Replace one character with numeric one(‘1’): ‘1at‘, ‘h1t‘, ‘ha1‘ Replace two characters with ‘1’ each: ‘11t‘, ‘h11‘, ‘1a1‘ Replace three characters with ‘1’ each: ‘111‘ In all the strings above, wherever the numeric characters are consecutive, add them. So ’11t’ will become ‘2t’, Similarly ‘111’ […]

We discussed problem of Tower of Hanoi earlier and written a recursive function to solve the problem, Recursive functions take lot of extra memory (New activation record for each call on the stack) (A detailed analysis of recursion is done in this post of mine). Todays question is to write a Non-recursive function to solve […]

Write a recursive function which will print an array in forward and backward order (depending on a parameter). The signature of the function should be /* If the array is 1 2 3 4 5, then Output should be * 1 2 3 4 5 – if (forward == true) * 5 4 3 2 […]

We have already seen the problem to reverse the words in a string, in the solution to that problem we have also written function to reverse the string. That was a non-recursive (iterative) function. Write a recursive function to reverse the String. For example: If the String is “ABCD” then the output should be “DCBA“. Before […]

Write a recursive function to print the Linked list in reverse order …

What will be the output of the below function if n is positive ?

Given the following structure of Node of the Linked List: struct Node{ int data; Node *link; }; What is the purpose of the below function void myFun(Node* head) { if(NULL == head){ return; } printf(“%d “, head->data); if(head->link != NULL ) myFun(head->link->link); printf(“%d “, head->data); }

Earlier I talked about Reversing a Singly linked list without using recursion. Today let’s write a recursive function to reverse a singly linked list.

What is recursion? What is head-recursion and tail-recursion? Should we use recursive or non-recursive functions?