How many PUSH and POP operations will be needed to evaluate the following expression by reverse polish notation in a stack machine (A ∗ B) + (C ∗ D / E)?
(A) 4 PUSH and 3 POP instructions
(B) 5 PUSH and 4 POP instructions
(C) 6 PUSH and 2 POP instructions
(D) 5 PUSH and 3 POP instructions
Answer: (B)
Explanation: Reverse polish notation is a system of formula notation without brackets or special punctuation.
To evaluate (A ∗ B) + (C ∗ D / E):
First avoid brackets and punctuation and convert it into postfix form i.e. AB+CDE/*+
Now push AB
On * pop AB and perform A * B. Now push back the result(say it X).
Push CDE.
On / pop DE and push back the result(say it Y).
On * Pop CY and perform * operation and push the result(say it z).
On + pop XZ and perform + opeeration and and push back the final answer.
Above computation include 5 PUSH and 4 POP instructions.
So, option (B) is correct.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.