In C programming I do normally just use an array for a stack; there is also a way to keep track separately of allocated stack size and in-use stack size and to use realloc if you need to add more than the allocated size. (However, sometimes there are variable size; in such case it may be simpler to use malloc especially for large data on one stack item.)
In JavaScript there is built-in Array.prototype.push and Array.prototype.pop functions for implementing a stack.
In Haskell it is also easy due to how lists work; it is like a cons-cell and you can easily do pattern matching with it. (Although, in many cases especially in Haskell recursion is probably more helpful.)
Code:
push x = state $ \y -> ((), x:y);
pop = state $ \(h:t) -> (h, t);
popm = state $ maybe (Nothing, []) (first Just) . uncons;
And then in Forth, there is stack built-in.
Z-machine also has one data stack local to a function call; YZIP has instructions to deal with additional stacks stored in addressable memory.
Another thing that uses recursion is if you are writing a recursive descent parser.
But in many cases recursion can help better especially if you do not need to be too deep.
rainwarrior wrote:
Tail recursion optimization might be important to know about. If you make a self-recursive call at the end of the function, some compilers mayoptimize that call into a loop instead. ....Here's the compiled function (MSVC 2013 with /O2):....
Other C compilers are GCC and clang and it can help to know how well it work with those too.
Quote:
Also, you could do this manually with a goto, and if you really need to rely on it being a loop you should just make it a loop, since optimizations aren't guaranteed in general.
In a lot of cases that does help (but not tree structures), and does make more sense. (Some programming languages don't have goto, but usually a loop will do anyways.)