0

Which data structure can be used to "eliminate" recursion from a recursive program? Array, stack, queue, or list? How is it used to that end?

When I eliminate recursion from the program computing factorial $n! = n(n-1)!$, I replace recursion with a loop. But I don't see how to do a similar transformation in general.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
user1917769
  • 301
  • 5
  • 12
  • This is because the factorial program is a very easy to implement without a recursive algorithm, therefore no need for a special data structure. But still, you need one of those data structures if you want to apply the recursive algorithm (i.e. to simulate the recursion). – Parham Nov 23 '13 at 20:53

1 Answers1

1

Let me restate your question: Which data structure does the CPU use to implement recursion? This data structure is knows as the call stack. As you can imagine, it's a stack. I strongly suggest that you consult the Wikipedia page (or any other source) and try to understand why a stack is used and how it works. This is much more important than answering a multiple-choice question.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503