How can you emulate recursion with a stack?

前端 未结 4 808
半阙折子戏
半阙折子戏 2020-12-17 07:42

I\'ve heard that any recursive algorithm can always be expressed by using a stack. Recently, I\'ve been working on programs in an environment with a prohibitively small avai

4条回答
  •  臣服心动
    2020-12-17 08:16

    Your particular example is tail-recursive, so with a properly optimising compiler, it should not consume any stack depth at all, as it is equivalent to a simple loop. To be clear: this example does not require a stack at all.

提交回复
热议问题