Find minimum cost to travel to the destination railways station
There are N stations on a railway track. You are at the first station and you want to go to the final railway station. You can either go directly or take breaks in between. Given the cost of travel for each pair of stations, find the minimum cost of travel.
Recursive implementation of Insertion Sort
We discussed Insertion sort in detail earlier. This post shows the recursive implementation of Insertion Sort.
Print permutations of string replaced with digits
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’ […]
Non-recursive solution to tower of hanoi
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 […]
Print array in the forward and backward direction recursively
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 […]
Recursive function to reverse a String
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 […]
Recursive function to traverse linked list in reverse order
Write a recursive function to print the Linked list in reverse order …
Output of a recursive function
What will be the output of the below function if n is positive ?
Output of a C function
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); }
Recursive function to reverse a linked list
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.