Infix to Postfix Conversion Example (Using Stack)



Infix to Postfix Example
Pre-requisite :

  • What is Infix and Postfix ?
  • Infix to Postfix Algorithm
Expression Current Symbol Stack Output Comment
A/B^C-D Initial State NULL Initially Stack is Empty
/B^C-D A NULL A Print Operand
B^C-D / / A Push Operator Onto Stack
^C-D B / AB Print Operand
C-D ^ /^ AB Push Operator Onto Stack because Priority of ^ is greater than Current Topmost Symbol of Stack i.e ‘/’ 
-D C /^ ABC Print Operand
D / ABC^ Step 1 : Now ‘^’ Has Higher Priority than Incoming Operator So We have to Pop Topmost Element .
Step 2 : Remove Topmost Operator From Stack and Print it
D NULL ABC^/ Step 1 : Now ‘/’ is topmost Element of Stack Has Higher Priority than Incoming Operator So We have to Pop Topmost Element again.
Step 2 : Remove Topmost Operator From Stack and Print it
D ABC^/ Step 1 : Now Stack Becomes Empty and We can Push Operand Onto Stack
NULL D ABC^/D Print Operand
NULL NULL ABC^/D- Expression Scanning Ends but we have still one more element in stack so pop it and display it