What will be the prefix conversion of following postfix expression?
1940
Postfix expression:IJKI-LM/N-*
O* -IJKI-ILMN
O *-17JK-/LMN
O-*/ JK- / LMN
O* -I/JKI-LMN
Answers
Conversion of Postfix expression into Prefix expression.
Note:
First of all, all the given options are faulty, as you can see that in every option, either an operand or an operator went missing. So, please check the options once again. Here we are providing you the complete procedure and the correct answer.
Given data:
Postfix expression: IJKI-LM/N-*
To find:
The prefix expression of the given postfix expression.
Procedure:
Procedure to follow:
- Scrutinize the given Postfix expression from left to right .
- If the symbol is an operand, then push it onto the Stack.
- If the symbol is an operator, pop the top two elements of the stack and create a string with the operator and the popped elements as follow
string = operator + operand2 + operand1
Now, push the complete string into the Stack .
- Repeat the process till you reach the end of the given postfix expression.
- Pop all of them out of the stack and concatenate them from bottom to top to get the prefix expression.
Application:
The given expression is IJKI-LM/N-*
Step 1:
'I' is an operand. So, push 'I' into the empty stack.
| I |
Step 2:
'J' is an operand. So, push 'J' into the stack.
| J |
| I |
Step 3:
'K' is an operand. So, push 'K' into the stack.
| K |
| J |
| I |
Step 4:
'I' is an operand. So, push 'I' into the stack.
| I |
| K |
| J |
| I |
Step 5:
'-' is an operator. So, pop the top two operands from the stack.
Operand 1: I
Operand 2: K
Operator: -
string = Operator + Operand 2 + Operand 1 = -KI
Push the complete string into the stack.
| -KI |
| J |
| I |
Step 6:
'L' is an operand. So, push 'L' into the stack.
| L |
| -KI |
| J |
| I |
Step 7:
'M' is an operand. So, push 'M' into the stack.
| M |
| L |
| -KI |
| J |
| I |
Step 8:
'/' is an operator. So, pop the top two operands from the stack.
Operand 1: M
Operand 2: L
Operator: /
string = Operator + Operand 2 + Operand 1 = /LM
Push the complete string into the stack.
| /LM |
| -KI |
| J |
| I |
Step 9:
'N' is an operand. So, push 'N' into the stack.
| N |
| /LM |
| -KI |
| J |
| I |
Step 10:
'-' is an operator. So, pop the top two operands from the stack.
Operand 1: N
Operand 2: /LM
Operator: -
string = Operator + Operand 2 + Operand 1 = -/LMN
Push the complete string into the stack.
| -/LMN |
| -KI |
| J |
| I |
Step 11:
'*' is an operator. So, pop the top two operands from the stack.
Operand 1: -/LMN
Operand 2: -KI
Operator: *
string = Operator + Operand 2 + Operand 1 = *-KI-/LMN
Push the complete string into the stack.
| *-KI-/LMN |
| J |
| I |
Step 12:
As we have reached the end of the given postfix expression, it's time to pop all the elements out of the stack and add them from the bottom to the top.
Operand 1: *-KI-/LMN
Operand 2: J
Operand 3: I
Final prefix expression = Operand 3 + Operand 2 + Operand 1
= IJ*-KI-/LMN
Therefore, the prefix expression on the conversion of the given postfix expression is: IJ*-KI-/LMN
Learn more:
- What is a linked list in the data structure?
https://brainly.in/question/6964440
- Edges violating the max-heap property.
https://brainly.in/question/16247860