简体   繁体   中英

How to use loop and stack to simulate recursion correctly?

(I'm not good at English so my expression may not very clear and correct.)

I want to simulate recursion by using loop and stack.

My goal is not about how to improve the performance because the original recursion way to solve Fibonacci is very ineffective, too. And the simulation can hardly have better performance. I wonder the way to change recursion to loop and stack.

The recursion version of solving Fibonacci Sequence(just a example of recursion). Very simple

int fib(int i)
{
    if (i == 0)
        return 0;
    if (i == 1)
        return 1;
    return fib(i - 1) + fib(i - 2);
}

It's my simulation for the recursion

int fib2(int a)
{
    Stack *stack = NULL;
    int temp = -1;
    int i[3] = {a, -1, -1};
    stack = stack_push(stack, i);
    while(!stack_empty(stack))
    {
        int *top = stack_top(stack);
        if (temp != -1 && top[1] == -1)
        {
            top[1] = temp;
            temp = -1;
        }
        else if(temp != -1 && top[2] == -1)
        {
            top[2] = temp;
            temp = -1;
        }
        if (top[0] == 0)
        {
            stack = stack_pop(stack);
            temp = 0;
            continue;
        }
        else if(top[0] == 1)
        {
            stack = stack_pop(stack);
            temp = 1;
            continue;
        }
        else
        {
            int j[3] = {top[0], -1, -1};
            if (top[1] == -1)
            {
                j[0] = top[0] - 1;
                stack = stack_push(stack, j);
            }
            else if (top[2] == -1)
            {
                j[0] = top[0] - 2;                
                stack = stack_push(stack, j);
            }
            else
            {
                temp = top[1] + top[2];
                stack = stack_pop(stack);
            }
            continue;
        }
    }
    return temp;
}

The stack is implemented by linked list, and the related functions are very simple. It works fine, but the way I do it, I believe, is too tardy and difficult.

I just wonder how can I do it easier? (not to use loop to solve Fibonacci but to simulate recursion)

What I really concern about is how to handle more than 1 recursion function call.

For 1 function call like this.

int sum(int i)
{
    if (i == 0)
        return 0;
    return i + sum(i - 1);
}

It is very easy to simulate by using loop and stack. And also effective.

int sum2(int a)
{
    Stack *stack = NULL;
    while (a > 0)
    {
        stack = stack_push(stack, a);
        a--;
    }
    int i = 0;
    while (!stack_empty(stack))
    {
        i += stack_top(stack);
        stack = stack_pop(stack);
    }
    return i;
}

But for more than 1 call, what I know is just to use such a stupid way to do (put a -1 as a sign).

I don't understand why a recursive approach is bad but if you'd like to do this with a loop it shouldn't be too hard I've got some 'sorta' pseudo-code for you you don't even need a linked list and this should reduce the computational complexity of your program a fair bit.

int main() {
  int fib_max = 10;

  int node_1 = 0;
  int node_2 = 0;
  int node_s = 0;

  for ( int n = 0; n < fib_max; n ++)
    {
      if (node_2 == 0)
    node_2 = 1;

      node_s = node_2 + node_1;
      node_1 = node_2;
      node_2 = node_s;
    }
}

This is just something I cooked up, hopefully it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM