Is every recursive function convertible to iteration? What characteristic should a recursive function have in order for it to be implemented using iteration?
I've been trying to define the following function using iteration but seems like a no-go! It is supposed to explore all the paths (nodes) in a maze. Can anyone rewrite this using iterations? If it is not possible, why not?
typedef int[0,99] id_t;
bool visited[id_t];
int path[id_t];
int pathCounter = 0;
struct {
id_t id;
bool free;
int neighborNode[4];
} nodeMap[id_t];
void findPath(int current){
visited[current] = true;
for (i : int[0, 3]){
if(nodeMap[nodeMap[current].neighborNode[i]].free == true && visited[nodeMap[current].neighborNode[i]] == false && nodeMap[current].neighborNode[i] != -1){
path[pathCounter] = nodeMap[nodeMap[current].neighborNode[i]].id;
pathCounter++;
findPath(nodeMap[current].neighborNode[i]);
path[pathCounter] = nodeMap[current].id;
pathCounter++;
}
}
path[0] = current;
}
Extension: Is it possible to convert the mentioned recursive function to iteration without implementing my own stack? One of the answers suggested that every tail recursive function can be converted to iteration without using a stack structure...if that's so, is every recursive function convertible to tail recursion? How?