The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

recursive -> iterative by dennismv2004-12-21 02:24:43
  I don't have time to give a full answer... by Liava2004-12-21 03:55:22
    Qsort by dennismv2004-12-21 04:15:00
      I'd still call that a recursive solution, though by Liava2004-12-21 04:30:46
        recursion by dennismv2004-12-21 04:50:19
          Well, the algorithm is as follows: by Liava 2004-12-21 11:46:07
Whenever you have a recursive call, push all your local variables and parameters onto the stack. Reset all locals to their default values and set the arguments to the values for the new function call. Then, goto the top of the function.

Change any returns to check if the stack is empty. If it is not, then you must pop all the variables off the stack and then goto the position after the "recursive call".

Things get a little more complicated if you have a function with multiple recursive calls, because you then need to store a variable on the stack with the point of return so you know which point to goto when you return.

This type of thing would be easiest to do in a language like C# or Java, since you can make a stack an array of objects and then push whatever you need.

In C++, you would need an array of void pointers, and then you'd copy each variable into the list. If you make use of classes, things get even messier, unless you have copy constructors defined for each class, or you declare all your objects on the heap.

However, at least in C++, you could declare a struct that matched your stack frame and then allocate that as a whole, and push it as a single unit. This will simply reduce the number of heap allocations required; it won't actually help with the object copying problem.
[ Reply ]
            ok by dennismv2004-12-21 19:34:10
              Not what I meant by Liava2004-12-21 22:05:31
                Much of this could be avoided. by williamashbless2004-12-21 23:32:22

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)