infixToPostfix()
Begin
Read infix expression "infix"
Set length = strlen(infix)
Set p = 0;
for (i = 0; i < length; i++), do
switch(infix[i])
case '(':
push('(')
break
case ')':
while (stack[top] != '('), do
Set postfix[p] = pop()
Increment p by 1
End While
Call pop()
case '+':
case '-':
case '*':
case '/':
case '^':
while (precOf(stack[top]) >= precOf(infix[i]) && stack[top] != '('), do
Set postfix[p] = pop()
Increment p by 1
End While
Call push(infix[i])
default:
Set postfix[p] = infix[i];
Increment p by 1
End Switch
End For
while (stack[top] != -1), do
Set postfix[p] = pop()
Increment p by 1
End While
Set postfix[p] = '\0'
Display postfix
End
Program
#include<stdio.h>#include<string.h>#defineMAX_SIZE100// Stackchar stack[MAX_SIZE];int top =-1;// String to store expressionschar postfix[20];char infix[20];voidpush(char c){if(top == MAX_SIZE -1)printf("Stack Overflow\n");else stack[++top]= c;}charpop(){if(top ==-1){printf("Stack Underflow\n");return'\0';// return U to denote underflow situation}return stack[top--];// return value of stack[top] & top--}intprecedenceOf(char c){if(c =='^')return3;elseif(c =='*'|| c =='/')return2;elseif(c =='+'|| c =='-')return1;elsereturn0;}intmain(){int i, p =0;printf("INFIX TO POSTFIX\n----- -- -------\n");printf("Enter infix expression: ");scanf("%s", infix);for(int i =0; i <strlen(infix); i++){switch(infix[i]){case'(':push('(');// Adding parentheses to the stackbreak;case')':while(stack[top]!='(') postfix[p++]=pop();// Adding all elements to postfix till '(' is encounteredpop();// Remove the '(' from the stackbreak;case'+':case'-':case'*':case'/':case'^':while(precedenceOf(infix[i])<=precedenceOf(stack[top])&& stack[top]!='('){ postfix[p++]=pop();}push(infix[i]);break;default: postfix[p++]= infix[i];}}while(top !=-1) postfix[p++]=pop();// Add all the remaining to postfix postfix[p]='\0';// Add null terminator to postfix stringprintf("The Postfix Expression: %s", postfix);}