08. Stack (ii) Stack application

Source: Internet
Author: User

I. Application of stacks-recursion1. Recursive functions: Put a straightCall itself or through a series of call statements indirectlycall your own function, called a recursive function. 2. Stacks and recursionrecursive function is actually a forward and return process, quite with the stack, out of the stack. In the forward stage, for each layer of recursion, the function's local variables, parameter values, and return addresses are pressed into the stack . In the rollback phase, the local variable at the top of the stack, the parameter value, and the return address are popped (out of the stack ) to return the rest of the execution code in the call hierarchy, that is, the state of the call is resumed. 3. Recursive application-Fibonacci sequenceThe Fibonacci sequence describes the breeding problem of rabbits, which has a very distinct feature: the sum of two adjacent items in front of it, constituting the latter. the mathematical model is:| 0, when n=0, where n is the number of monthsF (n) =| 1, when N=1         | F (n-1) +f (n-2), when n>1. where n is the number of months experienced, and F (n) is the amount of rabbits in the nth month. recursive implementation of source code:
/* Fibonacci Recursive function * Implements print the first 40 Fibonacci sequence    */int Fb (int i)    //i for the first month {    if (i<2)        return i==0?0:1;            When N=0, N=1, the total number of rabbits returned in the month =0/1    return Fb (i-1) +FB (n-2);    When n>1, the number of rabbits returned in the first month}int main () {    int i;    for (int i=0;i<40;i++)    //calculate and print the number of rabbits before 40 months    {          printf ("%d", Fb (i));      }    return 0;}

second, the application of the stack-arithmetic expression evaluation1. infix expression converted to suffix expression(1) Target: infix expression "+ (3-1) *3+10/2" to suffix expression "9 3 1-3 * + 2/+"(2) rule: Iterate through each number and symbol of the infix expression from left to right. If the number on the output, if the symbol, then determine its priority with the top of the stack , is the right parenthesis or priority lower than the top of the stack symbol, then the stack top elements in turn out of the stack and output, and the current symbol into the stack until the final output suffix expression. 2. Suffix expression calculation results(1) target: calculate suffix expression "9 3 1-3 * + 2/+"(2) Rules: From left to right to iterate through the expression of each number and symbol, encountered is the number on the stack, encountered is a symbol, will be in the top of the stack two numbers out of the stack, the operation, and then the results into the stack, has been the final result. Summary: Converting infix expressions to postfix expressions is key to the fact that the stack is used to enter and exit the operation of the symbol to perform the operation of the suffix expression is the key to the result-the stack is used to enter and exit the number of operations.

08. Stack (ii) Stack application

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.