We have given an Arithmetic Expression and we have to write a program that converts the infix to postfix using stack in C. The Expression will be given in the form of a string, where alphabetic characters i.e a-z or A-Z denotes operands and operators are ( +, –, *, / ). Expressions can also have brackets i.e ‘(’ and ‘)’. Here are some examples which will help you better understand the concept.
Example 1:
Input: a * ( b + c + d) Output: abc+d+*
Example 2:
Input: a*b Output: ab*
Before discussing the solution of infix to postfix conversion using stack in C, let us first learn about the Arithmetic Notations.
Any arithmetic expression consists of operands and operators. The way we arrange our operators and operands to write the arithmetic expression is called Notation.
The following are the three different notations for writing the Arithmetic expression.
An expression is said to be in infix notation if the operators in the expression are placed in between the operands on which the operator works. For example,
a + b * c
Infix expressions are easy to read, write and understand by humans, but not by computer It’s costly, in terms of time and space, to process Infix expressions
An expression is said to be in postfix notation if the operators in the expression are placed after the operands on which the operator works. For example,
It’s the most used notation for evaluating arithmetic expressions
An expression is said to be in prefix notation if the operators in the expression are placed before the operands on which the operator works. For example,
The precedence order of operators is given in the below table.
Precedence | Type | Operators | Associativity |
1 | Postfix | () [] -> . ++ — | Left to Right |
2 | Unary | + – ! ~ ++ — (type)* & sizeof | Right to Left |
3 | Multiplicative | * / % | Left to Right |
4 | Additive | + – | Left to Right |
5 | Shift | > | Left to Right |
6 | Relational | < >= | Left to Right |
7 | Equality | == != | Left to Right |
8 | Bitwise AND | & | Left to Right |
9 | Bitwise XOR | ^ | Left to Right |
10 | Bitwise OR | | | Left to Right |
11 | Logical AND | && | Left to Right |
12 | Logical OR | || | Left to Right |
13 | Conditional | ?: | Right to Left |
14 | Assignment | = += -+ *= /= %= >>= | Right to Left |
15 | Comma | , | Left to Right |
In this article, we are going to see how we can convert Infix expressions to postfix expressions. For simplicity and understanding purposes, we will consider only ‘+’, ‘-’, ’/’, ‘*’, ‘(’ and ‘)’.
Conversion of Infix to Postfix can be done using stack. The stack is used to reverse the order of operators. Stack stores the operator because it can not be added to the postfix expression until both of its operands are added. The precedence of the operator also matters while converting infix to postfix using stack, which we will discuss in the algorithm. Note: Parentheses are used to override the precedence of operators, and they can be nested parentheses that need to be evaluated from inner to outer.
Here are the steps of the algorithm to convert Infix to postfix using stack in C:
Here is the code for conversion of infix to postfix using Stack in C.
#include using namespace std; int precedence(char ch) < switch(ch)< case '-': case '+': return 1; case '*': case '/': return 2; default: return -1; >> bool isOperand(char ch)< return (ch>='a' && ch<='z') || (ch>='A' && ch <='Z'); >string infixToPostfix(string infix) < int n=infix.size(); stackst; string postfix; for(int i=0;iOutput:
Infix Expression : a*(b+c+d) Postfix Expression : abc+d+*
Time Complexity: O(n), where n is the length of the infix Expression
Space Complexity: O(n)
There are several advantages of postfix notation (also known as Reverse Polish Notation) over infix notation:
Conclusion In this article, we have learned about the infix to postfix conversion using stack in C. We have discussed the algorithm with the dry to convert the infix to postfix using stack in C. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at PrepBytes.
Here are some Frequently Asked Questions related to Infix to Postfix using stack in C.
Ques 1. Why do we need to convert infix to postfix notation? Ans. We can easily differentiate the order of operators, and we can also use the parenthesis to solve that part first when solving mathematical expressions. Because the computer cannot easily distinguish between operators and parenthesis, postfix conversion is required.
Ques 2. How do we convert infix to postfix using stack? Ans. The algorithm for converting infix to postfix notation involves using a stack to keep track of operators and their precedence. We scan the infix expression from left to right, and for each symbol we encounter, we take different actions depending on whether it is an operand, an operator, or a parenthesis.
Ques 3. How do we handle parentheses during conversion to postfix notation? Ans. We can handle parentheses during conversion to postfix notation, When we encounter an opening parenthesis, we push it onto the stack. When we encounter a closing parenthesis, we pop operators from the stack and append them to the postfix notation until we encounter the opening parenthesis, which we discard.
Ques 4. What should be done when a left parenthesis ‘(‘ is encountered? Ans: Ans. When a left parenthesis is encountered, it is placed on the operator stack. When the corresponding right parenthesis is encountered, the stack is popped until the left parenthesis and remove both parentheses.