Learning data structure-Chapter 3: Stacks and queues (application of stacks, bracket matching, middle and back prefix expression conversion)

Chapter 3: Stacks and Queues

1. Stack application

1.1 Bracket matching

In math we are [(A+b)*c] - (E-F)often there will be [ ]and ( )to represent the priority of operations, we have such [ ]and ( )extracted sequences called 括号匹配序列.

Matching sequence

  • ( [ ( ) ] )
  • [ ] [ ] ( )
  • ( ) [ ( ) ]

The above is the correct sequence that can be matched, and each one has an (or [corresponding to )it ].

Unmatched sequence

  • ( [ ( ) ]
  • ] [ ] ( )
  • ( ] [ ( ) ]

The following is a sequence of correct matching. We label each one and input it into the stack once to determine whether the wide number matches.

Insert picture description here

Algorithm idea:

  • 1. Initialize an empty stack, read the wide numbers sequentially
  • 2. If it is the right wide number, it will be matched with the top element of the stack
    • If it matches, pop the top element of the stack and proceed to the next element
    • If it does not match, the sequence is illegal
  • 3. If it is a left wide number, push it to the stack
  • 4. If all elements are traversed and the stack is not empty, the sequence is illegal

1.2 Expression evaluation

For example A+B+C-E-F, ifwe can solve this simple expression evaluation by traversing and adding judgment, but [(A+B)*C]-(E-F)this kind of more complicated expression has some complicated priorities. This requires the stack to solve, the classification of several expressions is introduced below.

Insert picture description here

When solving the problem of expression evaluation, we usually have to convert or convert 中缀表达式to , but the most commonly used is to convert . The suffix expression is often used to calculate the value of an expression.前缀表达式后缀表达式后缀表达式

1.3 Infix expression to pre & suffix

Infix to prefix & suffix

[(A+B)*C]-(E-F)

We first calculate (A+B) according to the priority of the operation and turn it into a prefix: +AB, [+AB*C]-(E-F)then multiply it by C and turn it into a prefix: * +ABC, so that the conversion of the first square bracket is completed, and then according to the operation priority , Convert [EF] into a prefix: -EF, now the expression is:, * +ABC-(-EF)and then put the middle one -to the front:-*+ABC-EF

Prefix expression:- * + A B C - E F

The process is the same as the one above, but just put the operation symbol number at the end.

Suffix expression:A B + C * E F - -

The algorithm idea of ​​using the stack to convert the infix to the suffix:

( ( A + B ) * C ) - ( E - F )

  • Numbers are directly added to the suffix expression
  • Operator:
  • a. If (yes, push it to the stack; .b
  • b. If it is' ), add the operators in the stack to the postfix expression in turn until they appear (, and delete them from the stack
  • c. If it is +, -, *,/
    • 1. The stack is empty and push into the stack;
    • 2. The top element of the (stack is , into the stack;
    • 3. The priority of the element higher than the top of the stack is pushed into the stack;
    • 4. Otherwise, pop the top operators of the stack one by one until an operator with a lower priority is OR (; `
  • d. The traversal is complete, if the stack is not empty, all elements will be popped up in turn.

Use this idea to execute it again to pretend the expression as a suffix:

In order to facilitate the text description, define one Has a suffix expression and define a Zstack.
First, ( (push the stack sequentially (rule: a), Z=( (then, then the number A, directly add to the suffix expression, H=Athen +push the stack (rule: c 2), Z=*((+then add B directly to the suffix expression:, H=ABthen )push the stack conform to the rules b, at this time Z=(, H=AB+and then *push, Z=(*then )push conform to the rules b, at this time Z=空, H=AB+C*and then -(turn the stack, at this time Z=-(, then E, directly into the postfix expression: H=AB+C*Ethen -the stack, then Z=-(-followed F added directly postfix expression, H=AB+C*EFand then )push conform to the rules b, then at this time z=-, H=AB+C*EF-and comply with the rules d, at this time z=空, H=AB+C*EF- -.

2. Recursion

Recursion If itself is applied in the definition of a function, procedure or data structure, it is called recursively defined, referred to as recursion

Common recursive examples:

Fibonacci sequence: 0, 1, 1, 2, 3, 5...

Starting from the third item in this sequence, each item is equal to the sum of the first two items.

Insert picture description here

Use C to implement the recursive process

int Fib(int n){
    
    //n是第几个斐波那契数
    if(n==0){
    
    
       return 0;
    }else if(n==1){
    
    
       return 1;
    }else if(){
    
    
       return Fib(n-1)+Fib(n-2);
    }
}

The essence of recursion lies in whether the original problem can be converted into a problem with the same attributes but on a smaller scale.

2.1 Problems caused by recursion

  • In the process of recursive calling, the system opens up a recursive working stack for each layer of return points, local variables, and incoming actual parameters for data storage. Too many recursions can easily cause stack overflow.
  • In general, the efficiency of recursion is not high

For example, the above Fibonacci sequence function, if we pass in 5, the whole process is:

Insert picture description here

We will find that we find that some of the same results will be repeated many times. Such repeated calls will cause the problem of inefficient recursion.

When a recursive algorithm is converted to a non-recursive algorithm, it is often done with the help of a stack.

Rimuke

Welcome to follow the official account 理木客, insist on sharing original knowledge, more exciting waiting for you to discover, look forward to your attention

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106265581