Stack & Expression Notations

1. Stack Definition

A Stack is a linear data structure that follows the principle LIFO (Last In First Out).

What is Associativity?

Associativity defines the order in which operators of the same precedence are evaluated in an expression.

Examples:

Operator Associativity Example

2. Operator Precedence

Precedence Operators Associativity
Highest ( )
2 ^ Right to Left
3 * / Left to Right
Lowest + - Left to Right

3. Expression Types

Infix Expression

Postfix Expression

Prefix Expression

4. Infix to Postfix Conversion Rules

  1. Print operands as they arrive.
  2. If stack is empty or top is (, push operator.
  3. If symbol is (, push it.
  4. If symbol is ), pop until ( is found.
  5. If higher precedence, push operator.
  6. If lower precedence, pop and print.
  7. If equal precedence, apply associativity.
  8. Pop all remaining operators at the end.
Associativity Rule:
Left → Right : Pop then Push
Right → Left : Push directly