Stack (prefix, infix, postfix expression, recursive) Stack

Third, the stack (Stack)

1, the stack concept

In contrast with the stack queue, the queue is entered in the first team, the team of the tail linear list, the stack is only insertion and deletion linear table (first in first out) at the top of the stack. We usually allow insertion and deletion of one end of the stack becomes, and the other end is called bottom of the stack. Insert stack to stack, the stack is popped delete operation, the stack does not contain any data elements called empty stack.
Here Insert Picture Description
Stack said:

  • With an array of analog Stack
  • Stack simulation using a linked list

2, the stack of application scenarios

Prefixes, suffixes and infix expression

  • Expression is the former operator prefix, the number of characters in the

(3+4)*5-6 ====> - * + 3 4 5 6

  • Infix expression form of expression we usually write

(3+4)*5-6

  • Postfix expression, first the number of characters, after the operator

(3+4)*5-6 =====>3 4 + 5 * 6

Postfix infix expression turn

  • Initializing two stacks, the stack operators s1 and store intermediate results stack s2 (list can also be expressed);
  • Scan from left to right infix expression
  • Operand is encountered, which is pressed into s2,
  • Encountered operator, compares it to the top element precedence:
  • If s1 is a null, or stack symbol left parenthesis "(", the operator will directly push
  • Otherwise, if higher priority than the priority of the stack, the operator onto the stack s1
  • Otherwise, the pop-up operator stack s1 and s2 pressed into in the fourth step to continue to cycle
  • Encountered parentheses:

If it is left bracket "(" directly into the s1

If a right parenthesis ")", the operator sequentially eject s1 stack, and pressed into s2, until it left parenthesis is encountered, at which time the parentheses discarded.

  • The rightmost above steps are repeated until the expression
  • The remaining operators s1 sequentially ejected and pressed s2
  • S2 sequentially pop-up element and outputting a result of the reverse infix expressions corresponding postfix notation.

2.1 four arithmetic expression (analog calculator)

Here Insert Picture Description

  • Infix expressions embodiment uses calculations, infix expression "7 + 2 * 6-4" traditional forms of expression.

1, to find a symbolic expression of the index by index

2, if the current symbol pointing to numbers on the stack into a digital, if the symbol into the symbol on the stack

3、当index指向运算符时,若此时符号栈为空,直接入栈,若符号栈不为空,则比较index指向的符号与栈顶的符号的优先级,若index指向的运算符大于等于栈顶的运算符时,将index指向的运算符入栈,否则将数字栈中前两位运算符(num1、num2)出栈,符号栈中的栈顶指向的运算符出栈,运算后**(num2运算符num1)**放入数字栈中。

4、当表达式扫描完毕,就顺序的从数字栈和符号栈中pop出相应的数字和符号进行运算。

5、当数字栈中仅有一个元素时,就是表达式的值。

  • 采用后缀表达式计算 9 3 1 -3 * +10 2 / +

1、通过一个index索引查找表达式的符号

2、若当前指向的符号为数字,就放入数字栈中,若是符号,就将处于栈顶的里两个数字出栈,并进行运算

3、将结算结果进栈,一直到最终获得结果。

2.2、递归

递归(recursion)是指通过重复将问题分解为同类的子问题而解决问题的方法。简单来说,递归就是自己调用自己。

递归需要遵守的规则:

1、执行一个方法时,就创建一个新的受保护的独立空间(栈空间)

2、方法的局部变量是独立的,不会互相影响

3、如果方法中使用的是引用类型的变量(比如数组),就会共享该引用类型的数据。

4、递归必须向退出递归的条件逼近,否则就是无限递归,出现StackOverFlowError

5、当一个方法执行完毕,或者遇到return,就会返回,遵守谁调用,就将结果返回给谁,同时当方法执行完毕或者返回时,该方法也就执行完毕。

例子:
Here Insert Picture Description

 public static  void getValue(int value){
        if(value>2){
            getValue(value-1);
        }
        System.out.println("the value is "+value);

    }

递归的的经典算法

  • 迷宫

    描述:有一个迷宫地图,有一些可达的位置,也有一些不可达的位置(障碍、墙壁、边界)* 。从一个位置到下一个位置只能通过向上(或者向右、或者向下、或者向左)走一步来实现,从起点出发,如何找到一条到达终点的通路。

    分析:

定义一个二维数组,1表示位置不可到达,2表示通路可以走,3表示位置已经走过,走不通,0表示还未走过的点
* 采用递归的算法
* 1、map表示地图
* 2、i,j表示从地图的那个位置开始出发(1,1* 3、如果小球到map[i][j](终点)位置,则说明通路找到
* 4、策略:下->->->左(算法的优化是策略的变化)
  • 八皇后问题

    描述:在一个8*8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,共有多少种摆法?

    分析:

1、第一个皇后放在第一行第一列
2、第二个皇后放在第二行第一列,然后判断是否Ok,如果不Ok,继续放在第二列、第三列,依次把所有列都放完,找到一个合适的
3、继续放第三个皇后,继续放第一列、第二列,.......直到第8个皇后放在一个不冲突的位置,这就算找到了一个正确解
4、当找到一个正确解时,在栈回退到上一个栈时,就会开始回溯,即将第一皇后放到第一列的所有正确解,全部得到
5、然后回头继续第一个皇后放第二列,后面继续执行1,2,3,4步骤
说明:
使用一个1维数组来表示,arr[8]={0,4,7,5,2,6,1,3},下标对应第几行,arr[i]=value,value表示第i+1个皇后放在第i+1列。

四、排序(Sort Algorithm)

Published 71 original articles · won praise 42 · views 60000 +

Guess you like

Origin blog.csdn.net/dreame_life/article/details/104127534