Lompat ke konten

Labs · 9618 A2 16.2

Reverse Polish

Work out expressions the way a computer does: with a stack and no brackets. Turn infix into Reverse Polish notation with an expression tree, and back again.

18 levelsFree bench

In the exam

9618/31 May/June 2024 · Question 5 · 9 marks

(a) Write the RPN 5 2 + 9 3 - / 3 * in infix form. (b) Write the infix expression ((7 + 3) - (2 * 8)) / 6 in RPN. (c) Evaluate the RPN expression a b - c d + * e / when a = 17, b = 5, c = 7, d = 3 and e = 10. Show the changing contents of the stack.

Points that gain marks

  • (a) One mark each for (5 + 2), then / (9 – 3), then * 3: ((5 + 2) / (9 - 3)) * 3.
  • (b) One mark for 7 3 +, one for 2 8 * - 6 /: 7 3 + 2 8 * - 6 /.
  • (c) One mark for each stack state: 17 and 5 pushed, then 12 (17 - 5).
  • 7 and 3 pushed on top of 12, then 10 (7 + 3).
  • 12 * 10 = 120.
  • 10 pushed, then 120 / 10 = 12 is left on the stack.

In this lab: The levels 'From 9618/31 June 2024' are these three parts; the stack is drawn at every step.

9618/31 May/June 2026 · Question 7 · 9 marks

(a) Write 21 * (15 – 3 + 8) / 7 in RPN. (b) Write c d + a b – a c – * / in infix form. (c) Show the changing contents of the stack as 15 9 – 24 3 / * 18 6 + / is evaluated.

Points that gain marks

  • (a) 21 15 3 – 8 + * 7 /
  • (b) (c + d) / ((a – b) * (a – c))
  • (c) The stack holds 6, then 6 8, then 48; then 48 24; the final total is 2.

In this lab: Levels 'June 2026: to RPN', 'June 2026: to infix' and 'June 2026: evaluate'.

9618/33 May/June 2026 · Question 7 · 9 marks

(a) Outline the process of using a stack to evaluate an RPN expression. (b) Write ((18 – 15 + 3) * 5) / 12 in RPN. (c) Show the changing contents of the stack as 16 3 * 36 6 / 18 6 – * + is evaluated.

Points that gain marks

  • (a) Read the expression from left to right; push each number onto the stack.
  • (a) For an operator, pop the last two values, apply the operator, and push the result; repeat until the end.
  • (b) 18 15 – 3 + 5 * 12 /
  • (c) 48, then 48 6, then 48 6 12, then 48 72, and finally 120.

In this lab: Levels 'June 2026: brackets in brackets' and 'June 2026: a tall stack'.

More real experiments

All labs →

Masuk atau buat akun

IGCSE, A-Level & AP