Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part B · Q10a6 marksCO1Module 1asked in 2 papers

Question 10a

Write algorithm for convert the given infix expression to postfix expression. Using a stack convert infix expression A+(B-C\D*E)+F/G to postfix expression.
Transcription notes:
  • The scan prints “C\D” with a backslash, which is not a C operator. It is most likely a typo for “/”. Any solution must state this assumption.

Topics: Infix to postfix conversion

Watch this exact question run

Loading visualizer…

Solution

Assumptions:
  • The scan prints “C\D”; we read it as C/D (division). If a different operator was intended, only that symbol's position in the stack changes.
algorithm
Algorithm InfixToPostfix(E)
1. for each token t of E from left to right
2.   if t is an operand: output t
3.   else if t = '(' : push t
4.   else if t = ')' : pop and output until '(' ; pop '('
5.   else: while top is an operator with prec(top) ≥ prec(t)   // ≥ because + − * / are left-assoc
6.             pop and output
7.         push t
8. pop and output all remaining operators
Conversion of A+(B−C/D*E)+F/G (generated)checked by code
SymbolStackPostfix
AemptyA
++A
(+ (A
B+ (A B
-+ ( -A B
C+ ( -A B C
/+ ( - /A B C
D+ ( - /A B C D
*+ ( - *A B C D /
E+ ( - *A B C D / E
)+A B C D / E * -
++A B C D / E * - +
F+A B C D / E * - + F
/+ /A B C D / E * - + F
G+ /A B C D / E * - + F G
endemptyA B C D / E * - + F G / +

Postfix: A B C D / E * - + F G / +

How to write it in the exam

Postfix: A B C D / E * − + F G / +

How this answer was verified
  • tests/linear-expression.test.ts asserts ABCD/E*-+FG/+
  • infix_to_postfix.c differential test

Similar questions