Application of stack: infix expression to suffix expression

Source: Internet
Author: User

1. infix expression to postfix expression

Rule: (Stack implementation)

1) If the operand is encountered, we will output it directly.

2) If an operator is encountered, we put it on the stack and we put it in the stack when we encounter the opening parenthesis.

3) If a closing parenthesis is encountered, the stack element pops up and the output of the operator is ejected until an opening parenthesis is encountered. Note that the left parenthesis only pops up and does not output.

4) If any other operator is encountered, such as ("+", "*", "("), and so on, pops the element from the stack until it encounters an element that finds a lower priority (or the stack is empty) . After the elements are ejected, the operators encountered are pressed into the stack. there is a point to note that only when encountering ") "In case we just pop up" (", otherwise we will not pop up" (".

5) If we read the end of the input, all the elements in the stack are popped in turn.

2. Evaluation of suffix expressions

(1) build a stack s;
(2) Read the suffix expression from left to right, read the number to convert it to the value of the stack s, read the operator from the stack to pop up two numbers to Y and X, and then the "X operator Y" form the computer results, and then press the stack s;
(3) If the suffix expression is not read, repeat the above procedure, the final output stack at the top of the value is the end.

Application of stack: infix expression to suffix expression

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.