### Infix to Postfix conversion Using Stack in C Programming

#### Infix Expression:

When the operators are in-between the operands just like a + b then the expression is known as Infix Expression.#### Postfix Expression:

When the operators are placed after the operands by following some rules just like ab+ then the expression is known as Postfix Expression.#### Algorithm:

**1.**Create an empty Stack.

**2.**Read all elements from the Infix expression.

**2.1**If the element is Left parenthesis then

**PUSH**it into the Stack.

**2.2**If the element is Right parenthesis then

**POP**all the elements from the Stack and display with Postfix string until left matching opening parenthesis found.

**2.3**If the element is Operand then display with Postfix string.

**2.4**If the element is Operator

**2.4.1**Check if the Stack is empty,

**PUSH**onto the Stack.

**2.4.2**Otherwise, check the priority level from the top of the Stack. If the priority of the operator is greater than the priority level of the operator placed at the top of the stack,

**PUSH**onto stack otherwise

**POP**from Stack and repeat

**2.4**step again.

**3.**At the end of the expression Stack must be empty if not then

**POP**and display with Postfix string.

#### Code:

#include<stdio.h>

struct stack{

int top;

char arr[20];

}Stack;

int checkPriority(char operator){

if(Stack.top==-1){

return 1;

}else if(Stack.arr[Stack.top]=='(' || Stack.arr[Stack.top]=='{' || Stack.arr[Stack.top]=='['){

return 1;

}else if(operator==Stack.arr[Stack.top]){

return 0;

}else if(operator=='(' || operator=='{' || operator=='['){

return 1;

}else if((Stack.arr[Stack.top]=='+' || Stack.arr[Stack.top]=='-') && (operator=='/' || operator=='*' || operator=='/' || operator=='^')){

return 1;

}

return 0;

}

void push(char operator){

if(Stack.top==19){

printf("Stack Overflow\n");

}else{

Stack.top++;

Stack.arr[Stack.top]=operator;

}

}

char pop(){

char operator;

if(Stack.top==-1){

printf("Stack UnderFlow\n");

}else{

operator=Stack.arr[Stack.top];

Stack.top--;

}

return operator;

}

int isMatching(char opening,char closing){

if(opening=='(' && closing==')'){

return 1;

}else if(opening=='{' && closing=='}'){

return 1;

}else if(opening=='[' && closing==']'){

return 1;

}

return 0;

}

int main(){

char infix[]="A^B*C-D+E/F/(G+H)",symbol;

char postfix[20]="";

int i=0,j=0;

Stack.top=-1;

while(infix[i]){

if(infix[i]=='+' || infix[i]=='-' || infix[i]=='*' || infix[i]=='/' || infix[i]=='^' || infix[i]=='(' || infix[i]=='{' || infix[i]=='['){

if(checkPriority(infix[i])){

push(infix[i]);

i++;

}

else{

postfix[j]=pop();

j++;

}

}else if(infix[i]==')' || infix[i]=='}' || infix[i]==']'){

while(1){

symbol=pop();

if(isMatching(symbol,infix[i])){

break;

}else{

postfix[j]=symbol;

j++;

}

}

i++;

}else{

postfix[j]=infix[i];

j++;

i++;

}

}

for(i=j;Stack.top!=-1;i++){

postfix[i]=pop();

}

printf("Result : %s\n",postfix);

return 0;

}

## Comments

## Post a Comment