I have an algorithm that works perfectly, but it uses recursion. I know there are patterns for just about everything, but I could not find one for this case.
I just need some simple examples that show how to modify an algorithm, specifically the part where a method or function calls itself. I’ve seen iteration algorithms that do it with a while loop. So there must be a simple checklist to follow in order to convert an recursive algorithm into an iterational one.
You can definitely model recursion with iteration and a custom call stack. Since recursion is nothing but execution of same instructions in a new environment, you can model your own environment using a simple stack structure, and just wrap your algorithm in a loop, pushing your current mini-environment at the start of an iteration and popping it whenever you finish a loop iteration, or exit it prematurely via
breakorcontinue.