Infix to Postfix conversion
Infix to Postfix conversion
================================================================================
- Infix expression: The expression of the form a operator b (a + b). When an operator is in-between every pair of operands.
- Postfix expression: The expression of the form a b operator (ab+). When an operator is followed by every pair of operands.
- Algorithm:
- begin
- x = read first symbol from infix
- if ( x is '(' or ')' )
- if (x is '(' )
- push (x)
- end if
- if (x is ')' )
- pop() everything from stack and send to output till we get first '(' parenthesis but '(' not send to output
- end if
- end if
- if (x is operator)
- if (operator[TOP] < current (x) operator)
- push (x) i.e. push current operator on stack
- end if
- if (operator[TOP] > current (x) operator)
- pop(TOP operator) and send to output and push (current x) operator on the stack
- end if
- if (TOP == -1)
- push(x)
- end if
- end if
- x = read next symbol
- go to step 3
- print output string
- end
- begin
- x = read first symbol from infix
- if ( x is '(' or ')' )
- if (x is '(' )
- push (x)
- end if
- if (x is ')' )
- pop() everything from stack and send to output till we get first '(' parenthesis but '(' not send to output
- end if
- end if
- if (x is operator)
- if (operator[TOP] < current (x) operator)
- push (x) i.e. push current operator on stack
- end if
- if (operator[TOP] > current (x) operator)
- pop(TOP operator) and send to output and push (current x) operator on the stack
- end if
- if (TOP == -1)
- push(x)
- end if
- end if
- x = read next symbol
- go to step 3
- print output string
- end
Comments
Post a Comment