Prerequisite – Stack | Set 1 (Introduction)
Infix expression:The expression of the form a op b. When an operator is in-between every pair of operands.
Postfix expression:The expression of the form a b op. When an operator is followed for every pair of operands.
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the below expression: a op1 b op2 c op3 d
If op1 = +, op2 = *, op3 = +
The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to add a to it. The result is then added to d after another scan.
The repeated scanning makes it very in-efficient. It is better to convert the expression to postfix(or prefix) form before evaluation.
The corresponding expression in postfix form is: abc*+d+. The postfix expressions can be evaluated easily using a stack. We will cover postfix expression evaluation in a separate post.
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
Following is the implementation of the above algorithm
Quiz: Stack Questions
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Postfix to Infix
- Stack | Set 4 (Evaluation of Postfix Expression)
- Convert Infix To Prefix Notation
- Infix to Prefix conversion using two stacks
- Program to convert Infix notation to Expression Tree
- Prefix to Infix Conversion
- Postfix to Prefix Conversion
- Prefix to Postfix Conversion
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Stack Permutations (Check if an array is stack permutation of other)
- Find maximum in stack in O(1) without using additional stack
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Spaghetti Stack
- Implement Stack using Queues
- Design a stack that supports getMin() in O(1) time and O(1) extra space
- Implement a stack using single queue
- Stack Class in Java
- How to create mergable stack?