Table of Contents
A common application of stacks is in converting recursive algorithms to iterative forms.
Recursion and iteration (looping) are equally powerful. Any recursive algorithm can be rewritten to use loops instead. The opposite is also true. Any iterative algorithm can be written in terms of recursion only.
Given a choice of which to use, our decision is likey to come down to issues of
expressiveness: some algorithms are just naturally simpler or easier to write with loops. Some are simpler with recursion.
Given the fact that testing and debugging of code usually takes much more effort than writing it in the first case, anything we can do to make code simpler (thuse reducing the opportunities for makeing mistakes) is worth giving serious consideration.
performance: Iteration is usually (though not always) faster than an equivalent recursion.
In the Forum: