Infix to postfix conversion in C

Table of Contents

In this guide, we will see about Infix to postfix conversion in C.

In the computer, all the arithmetic expressions first convert to their postfix from infix. After the conversion, the evaluation will start.


Algorithm

  • First, we have to fix the precedence of the expression.
  • To convert the infix expression to its postfix expression, we have to take care of the following conditions:
      • If the characters are in between 0-9 or a-z or A-Z, then we insert them to another array.
      • If there is “ ( ” then we simply insert it into the stack.
      • If there is “ ) ” then we pop the element from the stack until we get the “ ( ”.
    • If there is some operator like +, -, *, / , then check the precedence of that operator to the top element of the stack. If the precedence of the operator is less than the top element of stack then we pop that element from the stack and insert it into another array. Otherwise, push that operator into the stack.
  • After all of this condition if the stack remains not empty, then we have to pop all the elements from the stack until it becomes empty

Implementation:

Output:

Enter the length : 23
Enter the expression : a+b*(c^d-e)/(f+g*h^s)-i
Postfix : abcd^e-*fghs^*+/+i-

That’s all about Infix to postfix conversion in C

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *