UGC-NET Dec 2014 · Stacks

MCQ · 2 marks · Problem PROG-DSTR-0037

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)$?
  1. A
    $4$ PUSH and $3$ POP instructions
  2. B
    $5$ PUSH and $4$ POP instructions
  3. C
    $6$ PUSH and $2$ POP instructions
  4. D
    $5$ PUSH and $3$ POP instructions

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solutionReverse Polish form of \((A*B)+(C*D/E)\):\[A\ B\ *\ C\ D\ *\ E\ /\ +\]Each operand is pushed: A, B, C, D, E gives 5 PUSH. Each of the 4 binary operators \(*,\ *,\ /,\ +\) pops its two operands and pushes the result back, a net removal of one item, so it is counted as one POP.\[\text{5 operands}\to5\ \text{PUSH},\qquad 4\ \text{operators}\to4\ \text{POP},\]leaving the single result on the stack.Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com