Infix To Postfix Conversion using Stack C Program

Learn how to convert an expression from Infix to Postfix using Stack in C Programming. This code for infix to postfix in c uses two arrays to store infix and postfix expression and a stack for conversion from infix to postfix expression.

What is Polish Notation?

A polish mathematician named Jan Lukasiewicz has given a method to represent arithmetic expressions. It included infix, postfix and prefix methods to represent an expression. The infix method is the conventional method to represent arithmetic expressions since it is better analyzed by the computers.

Infix To Postfix Conversion Example

Infix String: A+ B ^ C + D * E / (F + G)

Postfix String: ABC/+DE*_F+

Steps To Convert Infix Expression to Postfix Expression

Scan the symbols of the Infix string from left to right one by one.

If the character is an operand then shift it to the postfix string (array).

If the character is left parathesis then push it on the stack.

If the character found is right parantheses then pop all the operators from the stack upto the first left parantheses and add all these operators to the postfix string. Now, delete the right and left parantheses.

If the character is an operator then pop the operators that have the greater or equal precedence than the character operator and shift these popped operators to the postfix string. Now, push the scanned symbol operator on the stack.

After all the characters of the Infix string have been scanned, pop all the remaining operators on the stack and then shift them to postfix string.

Note: This stack code for Infix to Postfix Conversion in C Language has been compiled with GNU GCC Compiler and developed using gEdit Editor in Linux Ubuntu Operating System.