BIT201 · 2082 · 5 marks
Convert the infix expression 7 * 8 + 10 - 2 to postfix using stack.
Convert the infix expression 7 * 8 + 10 - 2 to postfix using stack. [5]
Data Structures and AlgorithmsChapter 3: Linear Data Structures: Stacks and QueuesInfix to postfix conversion using stackNumerical: infix to postfix conversion
Given Data
Infix expression to convert: $$7 * 8 + 10 - 2$$
Operator precedence:
*,/: higher+,-: lower
Associativity: left to right.
Conversion Rules (Stack Method)
- Scan tokens left to right.
- Operand → append to output.
- Operator → pop operators from stack with precedence greater than or equal to the current one (for left-associative operators), append them to output, then push the current operator.
- End → pop all remaining operators to output.
Step-by-Step Trace
| Step | Token | Action | Stack (bottom→top) | Output |
|---|---|---|---|---|
| 1 | 7 | operand → output | (empty) | 7 |
| 2 | * | push | * | 7 |
| 3 | 8 | operand → output | * | 7 8 |
| 4 | + | * ≥ + so pop *; push + | + | 7 8 * |
| 5 | 10 | operand → output | + | 7 8 * 10 |
| 6 | - | + ≥ - so pop +; push - | - | 7 8 * 10 + |
| 7 | 2 | operand → output | - | 7 8 * 10 + 2 |
| 8 | End | pop - | (empty) | 7 8 * 10 + 2 - |
Verification (evaluate postfix)
Given values: $7,8,+,*$ etc. Compute $7 \times 8 = 56$; $56 + 10 = 66$; $66 - 2 = 64$. Infix: $7 \times 8 + 10 - 2 = 56 + 10 - 2 = 64$. ✓ Matches.
Result
$$\boxed{7\ 8\ *\ 10\ +\ 2\ -}$$
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.