Stack and its application

Source: Internet
Author: User

 Stack, also known as a stack, is a linear table with limited operations, and its limitation is that only one end of the table is allowed to insert and delete operations. One end of the stack operation is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack is called a stack or a stack, a push, or a Pop from a stack to remove an element called a stack or a stack. Because the elements of the backward stack must first be out of the stack, the stack is called the LIFO table (last in first out, LIFO).

Application of Stacks

1. Input, then output in reverse order.

2. Grammar check: parentheses match. Each time you scan to the left parenthesis of a large medium and small, make it into the stack, when scanning to the right parenthesis, check whether the top of the stack is the appropriate opening parenthesis, if the stack processing, if not, there is a syntax error. When scanning to the end of a file, if the stack is empty it indicates that no parentheses pairing errors were found.

3. Number conversion: Converts a decimal integer to any of the binary outputs from two to nine.

Conversion method: To convert to R-binary, the original number is divided by the base R (after the end of the business after the division), until the quotient is 0, the resulting sequence of the remainder is the conversion results.

calculation of an arithmetic expression

In infix expressions (which are the arithmetic expressions we normally write), calculations require attention to precedence, parentheses, and the actual order of operators are often inconsistent with the order in which they are in the expression, so Polish scientists propose a suffix expression , Put the operator behind the two operands. In the suffix expression, there is no parenthesis, there is no difference in operator precedence, the calculation process is performed exactly in the order in which the operators appear, the entire calculation process needs to be scanned only once to complete.

infix expressions are converted to Postfix expressions:

The conversion process is as follows: Scan infix expression from beginning to end, if the number is encountered directly to the suffix expression, if the operator is encountered, then the top element of the stack and the precedence of the operator, when the precedence of the operator is greater than the top element of the stack, indicating that the operator has not entered a suffix expression, The operator should be staged in the operator stack, then write its latter operand to the suffix expression, and then put it out of the stack and write to the suffix expression; if the operator precedence is less than or equal to the top element of the stack, the two operands of the top operator of the stack have been written to the suffix expression. The top element of the stack should be stacked and written to the suffix expression, and the new stack top element will still be compared and processed until the top of the stack is less than the priority of the operator currently waiting to be processed, and then the operator is pushed into the stack. Follow the above procedure to scan to the end of the infix expression, and then put the remaining operators out of the stack and write the suffix expression.

Infix expression "+ + (3-1) *3+10/2" converted to postfix expression "9 3 1-3*+ 10 2/+"

Let's take a look at this process in detail.

1. Initialize an empty stack to use for symbols in and out of the stack.

2. The first character is the number 9, the output 9, followed by the symbol "+", into the stack.

3. The third character is "(", is still a symbol, because it is only the left parenthesis, not paired, so the stack.

4. The fourth character is the number 3, the output, the total expression is 9 3, followed by the "-" into the stack.

5. Next is the number 1, the output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the stack top sequentially out of the stack, and output, until "(" out of the stack. At this point there is only "-" above the opening parenthesis, so the output "-", the total output expression is 9 3 1-

6. Then the number 3, the output, the total expression is 9 3 1-3. followed by the symbol "*", because at this time the top of the stack symbol is "+" number, the priority is lower than "*", so do not output, into the stack.

7. Then the symbol "+", at this time the current stack top element than this "+" priority, so the stack of elements out of the stack and output (there is no lower priority than the "+" number, so all out of the stack), the total output expression is 9 3 1-3 * +. Then the current symbol "+" into the stack. That is, the bottom of the top 6 of the stack "+" refers to the infix expression in the beginning of the 9 after the "+", and the bottom of the stack (also the top of the stack) "+" refers to "+ + (3-1) *3+" in the Last "+".

8. Immediately following the number 10, the output, the total expression becomes 9 3 1-3 * + 10.

9. The last digit 2, output, the total expression is 9 3 1-3*+ 10 2

10. Because it has reached the end, all the symbols in the stack are stacked and output. The final output suffix expression results in 9 3 1-3*+ 10 2/+

infix expression to prefix

Follow these steps:
(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;
(2) Scanning infix expression from right to left;
(3) When the operand is encountered, it is pressed into the S2;
(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:
(4-1) If the S1 is empty, or the top operator of the stack is the closing parenthesis ")", the operator is placed directly into the stack;
(4-2) Otherwise, if the priority is higher or equal than the top operator of the stack, the operator is also pressed into the S1;
(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;
(5) When the parentheses are encountered:
(5-1) If the closing parenthesis ")", then press directly into the S1;
(5-2) If the opening parenthesis "(", then pops up the operator at the top of the S1 stack, and presses into the S2 until the closing parenthesis is encountered, this pair of parentheses is discarded;
(6) Repeat steps (2) to (5) until the leftmost side of the expression;
(7) The remaining operators in the S1 are popped and pressed into the S2;
(8) The elements in the S2 pop-up and output, the result is the infix expression corresponding to the prefix expression.
For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a prefix expression is as follows:

So the result is "-+ 1x+ 2 3 4 5".

Suffix expression evaluation

From left to right scan the expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with the operator to do the corresponding calculation (the top element of the op stack of the upper element), and the result into the stack; Repeat the process until the right end of the expression, The value of the last operation is the result of an expression.
For example, the suffix expression "3 4 + 5x6-":
(1) scan from left to right, press 3 and 4 into the stack;
(2) encountered the + operator, so pop up 4 and 3 (4 is the top element of the stack, 3 is the second top element, note the comparison with the prefix expression), calculate the value of the 3+4, 7, and then 7 into the stack;
(3) 5 into the stack;
(4) Next is the x operator, so eject 5 and 7, calculate the 7x5=35, 35 into the stack;
(5) 6 into the stack;
(6) The last is the-operator, which calculates the value of 35-6, or 29, resulting in the final result.

Computer evaluation of the prefix expression:
  A right-to-left scan expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with operators to do the corresponding calculation (stack top element op sub-top element), and the results into the stack;
For example, the prefix expression "-x+ 3 4 5 6":
(1) From right to left scanning, 6, 5, 4, 3 is pressed into the stack;
(2) encountered the + operator, so pop up 3 and 4 (3 is the top element of the stack, 4 is the second top element, pay attention to the suffix expression comparison), calculate the value of the 3+4, 7, and then 7 into the stack;
(3) Next is the x operator, so eject 7 and 5, calculate the 7x5=35, 35 into the stack;
(4) The last is the-operator, which calculates the value of 35-6, or 29, resulting in the final result.
As you can see, it is easy to compute the value of the prefix expression with a computer.

Stack 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.