Java data Structure-application of the stack of linear table-recursion and its application

Source: Internet
Author: User

?? The definition of a recursive function: to call itself directly or through a series of call statements indirectly called its own function, called the recursive function (recursive function must have an end condition, so as not to fall into the endless recursion).

The differences between iterations and recursion are:

? (1). Iteration uses a looping structure, and recursion uses the selection structure.

? (2). Recursion can make the structure of the program clearer, more concise, and easier to understand, thus reducing the time to read code. But a large number of recursive calls create a copy of the function, which consumes a lot of time and memory.

? (3). Iterations do not need to call the function repeatedly and consume additional memory. Therefore, we should choose different ways of implementing the code depending on the situation.

The following explains how to use the stack to implement recursion:

?? Each time the function is called recursively, the system presses the local variables of the function, the parameter values, and the return address into the stack. At the end of the call, the information at the top of the stack (local variables, parameter values, and return addresses) is ejected and the system continues the code after the breakpoint.

The following are other applications of the stack, such as:

Hanoi Tower Problem

Hanoi

?? Hanoi (also known as Hanoi) is a puzzle toy derived from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

Baidu Encyclopedia Answer: Click to view the details

package example;public class Hanoi {    /**    *     * @param n 盘子的数目    * @param origin 源座    * @param assist 辅助座    * @param destination 目的座    */    public void hanoi(int n, char origin, char assist, char destination) {        if (n == 1) {            move(origin, destination);        } else {            hanoi(n - 1, origin, destination, assist);            move(origin, destination);            hanoi(n - 1, assist, origin, destination);        }    }    // Print the route of the movement    private void move(char origin, char destination) {        System.out.println("Direction:" + origin + "--->" + destination);    }    public static void main(String[] args) {        Hanoi hanoi = new Hanoi();        hanoi.hanoi(6, ‘A‘, ‘B‘, ‘C‘);    }}

The factorial problem of n

?? The problem seems very simple, for the smaller number directly can be obtained by simple multiplication, but when the required n is larger, this time the computer operation will be out of bounds, so there is no way to find the factorial of N. The most important problem here is the large number of operations, and the use of arrays for this problem can be a good solution to the problem, the following is the Java version of the source code implementation.

public static BigInteger factorial(int n){        int []a = new int[100];        int i,j;        int p,h;           //p存储当前结果的位数,h为进位        a[0]=1;        p=1;          for(i=2;i<=n;i++)   //循环与2,3,4.....n相乘        {            for(j=0,h=0;j<p;j++)    //让a[]的每位与i相乘            {                a[j]=a[j]*i+h;                h=a[j]/10;                a[j]=a[j]%10;            }            while(h>0)         //如果h不为0            {                a[j]=h%10;                h=h/10;                j++;            }            p=j;            //将当前的位数赋给p        }        StringBuffer sb = new StringBuffer();        for(i=p-1;i>=0;i--)        {            sb.append(a[i]);        }        BigInteger bNum = new BigInteger(sb.toString());        return bNum;    }

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.

Java data Structure-application of the stack of linear table-recursion and its 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.