Infix to Prefix conversion using two stacks

Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)

Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )

Given an Infix expression, convert it into a Prefix expression using two stacks.

Examples:

Input : A * B + C / D
Output : + * A B/ C D 

Input : (A - B/C) * (A/K-L)
Output : *-A/BC-/AKL
分析:
  1. Traverse the infix expression and check if given character is an operator or an operand.
  2. If it is an operand, then push it into operand stack.
  3. If it is an operator, then check if priority of current operator is greater than or less than or equal to the operator at top of the stack. If priority is greater, then push operator into operator stack. Otherwise pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until priority of current operator is less than or equal to operator at top of the operator stack.
  4. If current character is ‘(‘, then push it into operator stack.
  5. If current character is ‘)’, then check if top of operator stack is opening bracket or not. If not pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until top of operator stack is an opening bracket.
  6. The final prefix expression is present at top of operand stack.
 1 class Solution {
 2     boolean isOperator(char C) {
 3         return C == '-' || C == '+' || C == '*' || C == '/' || C == '^';
 4     }
 5 
 6     int getPriority(char C) {
 7         if (C == '-' || C == '+') {
 8             return 1;
 9         } else if (C == '*' || C == '/') {
10             return 2;
11         } else if (C == '^') {
12             return 3;
13         } else {
14             return 0;
15         }
16     }
17     String infixToPrefix(String infix) {
18         Stack<Character> operators = new Stack<>();
19         Stack<String> operands = new Stack<String>();
20 
21         for (int i = 0; i < infix.length(); i++) {
22             if (infix.charAt(i) == '(') {
23                 operators.push(infix.charAt(i));
24             }
25             else if (infix.charAt(i) == ')') {
26                 while (!operators.empty() && operators.peek() != '(') {
27                     String op1 = operands.pop();
28                     String op2 = operands.pop();
29 
30                     char op = operators.pop();
31                     String tmp = op + op2 + op1;
32                     operands.push(tmp);
33                 }
34             } else if (!isOperator(infix.charAt(i))) {
35                 operands.push(infix.charAt(i) + "");
36             }
37             else {
38                 while (!operators.empty() && getPriority(infix.charAt(i)) <= getPriority(operators.peek())) {
39                     String op1 = operands.pop();
40                     String op2 = operands.pop();
41 
42                     char op = operators.pop();
43                     operands.push(op + op2 + op1);
44                 }
45                 operators.push(infix.charAt(i));
46             }
47         }
48         
49         while (!operators.empty()) {
50             String op1 = operands.pop();
51             String op2 = operands.pop();
52 
53             char op = operators.pop();
54             operands.push(op + op2 + op1);
55         }
56         return operands.peek();
57     }
58 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/11318743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值