prefix expression = ∗ + 3 + 3 ↑ 3+ 3 3 3
=> * + 3 + 3 ^ 3 ( 3+ 3) 3 // here ^ -> power operator.
=> * + 3 + 3 ^ 3 6 3
=> * + 3 + 3 (3^6) 3
=> * + 3 + 3 729 3
=> * + 3 ( 3 + 729 ) 3
=> * + 3 732 3
=> * ( 3 + 732) 3
=> * 735 3
=> (735 ) * 3
=> 2205 ( ans- C)
Method to follow -
Incase of postfix expression, we push the element on seeing operand in to the stack and on seeing operator we pop top two elements from the stack and again push (pop2( 2nd poped) <operator> pop1(first popped) back into the stack.
Just like postfix, apply the same logic incase of prefix too but, start from the last of prefix expression with one modification, on seeing an operator and after doing 2 pop, we need to push here (1st pop <operator> 2nd pop). And rest everything is same as postfix.
Here- ∗ + 3 + 3 ↑ 3+ 3 3 3 // stack S = Empty.
push 3.
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 3
push 3
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 3 3
push 3
∗ + 3 + 3 ↑ 3 + 3 3 3 // S = 3 3 3
This time its an operator ( + ).
So, pop1=3, pop2 = 3, Now do push ( 3 + 3) => push (6)
∗ + 3 + 3 ↑ 3 + 3 3 3 // S = 6 3
push 3
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 3 6 3
This time its an operator ( ^ ).
So, pop1=3, pop2 = 6, Now do push ( 3 ^ 6) => push (729) // note here, pop1 (3) < operator > pop2 (6).
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 729 3
push 3
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 3 729 3
This time its an operator ( + ).
So, pop1=3, pop2 = 729, Now do push ( 3 + 729) => push (732)
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 732 3
push 3
∗ + 3 + 3 ↑ 3+ 3 3 3 // S= 3 732 3
This time its an operator ( + ).
So, pop1=3, pop2 = 732, Now do push ( 3 + 732) => push (735)
∗ + 3 + 3 ↑ 3+ 3 3 3 // S = 725 3
This time its an operator ( * ).
So, pop1=735, pop2 = 3, Now do push ( 735 * 3) => push (2205)
Ans = 2205