Write algorithms step by step (recursion and stack)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

All my friends who have read the previous blog know that function calling relies mainly on Stack interaction between EBP and ESP. The main feature of recursion is that the function calls itself. If a function calls itself, this function is a recursive function.

Let's take a look at how common functions are called. If function a calls function B and function B calls Function C again, how can data in the stack be stored?

Function a ^ function B | (address decrease) Function C |

For recursive functions, a simple recursive function is used as an example:

int iterate(int value){if(value == 1)return 1;return value + iterate(value -1);}

Next we will use a function for calling to see what will happen?

void process(){int value = iterate(6);}

What is the memory stack?

iterate(int 1) line 96iterate(int 2) line 97 + 12 bytesiterate(int 3) line 97 + 12 bytesiterate(int 4) line 97 + 12 bytesiterate(int 5) line 97 + 12 bytesiterate(int 6) line 97 + 12 bytesprocess() line 102 + 7 bytesmain() line 108mainCRTStartup() line 206 + 25 bytesKERNEL32! 7c817067()

As you can see, there is no difference between recursive functions and common functions. Besides calling itself, it is a common function. So when will this function be returned recursively? This is the key to recursive functions. We can see that the iterate function stops when it reaches 1, so the above stack is in (value = 1), that is, return. Therefore, the key part of a recursive function is: (1) recursive policy; (2) function exit.

Here, we may feel that this is not the case for recursive functions, as it is. However, we also need to keep in mind that the depth of recursion is a question we must consider. The entire recursive process is controllable only when the recursive depth is within a controllable range. When is it uncontrollable? Is the recursive depth greater than a certain number? Is this number related to the length of the thread stack? When the stack overflows, the obtained data has lost its authenticity, so it makes no sense.

We will promote the above question. How can we use our own defined stack to simulate the above recursive call? This not only satisfies recursive attributes, but also ensures that the function is fully controllable.

You can write your own solutions first. The following is just my personal idea.

int iterate(int value){int count = 0;int number  =0;push(value);while(-1 != (number = pop())){if(1 != number)push(number -1);count += number;}return count;}

[Notice: The following blog introduces algorithms and memory]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.