Data Structures & AlgorithmsVery hardGATE CS

The infix expression a*b^c^d-(e+f*g)^h/i+j (^ is right-associative with highest precedence, then * and /, then + and -, all others left-associative) is converted to postfix with the standard operator-stack algorithm. What is the maximum number of symbols (operators and parentheses) on the stack at any instant, and what is the postfix result?

  1. A. 4 and abcd^^*efg*+h^i/-j+
  2. B. 5 and abcd^^*efg*+h^i/-j+
  3. C. 4 and abc^*efg*+h^i/-j+
  4. D. 3 and +j-/i^h+*gfe*^^dcba

Correct answer: A. 4 and abcd^^*efg*+h^i/-j+

Scanning with right-associative ^ (equal-precedence ^ is not popped) the stack peaks at 4 symbols and the postfix is abcd^^*efg*+h^i/-j+.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic