Infix to Prefix conversion Using Stack in C

Infix Expression:

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

Prefix Expression:

When the operators are placed before the operands by some rules just like +ab then the expression is known as Prefix Expression.

Algorithm:

1. Create an empty Stack.

2. Reverse the Infix expression.

3. Read all elements from the Infix expression.

3.1 If the element is Left parenthesis then PUSH it into the Stack.

3.2 If the element is Right parenthesis then POP all the elements from the Stack and display with Prefix string until left matching opening parenthesis found.

3.3 If the element is Operand then display with Prefix string.

3.4 If the element is Operator

3.4.1 Check if the Stack is empty, push onto the Stack.

3.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 3.4 step again.

4. At the end of the expression Stack must be empty if not then POP and display with Prefix string.

5. Reverse the Prefix expression.

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 prefix[20]="";
    int i=0,j=0,l=0;
    Stack.top=-1;
    strrev(infix);

    while(infix[l]){
        if(infix[l]=='('){ infix[l]=')'; }
        else if(infix[l]==')'){ infix[l]='('; }
        else if(infix[l]=='{'){ infix[l]='}'; }
        else if(infix[l]=='}'){ infix[l]='{'; }
        else if(infix[l]=='['){ infix[l]=']'; }
        else if(infix[l]==']'){ infix[l]='['; }
        l++;
    }
    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{
                prefix[j]=pop();
                j++;
            }
       }else if(infix[i]==')' || infix[i]=='}' || infix[i]==']'){
            while(1){
                symbol=pop();
                if(isMatching(symbol,infix[i])){
                   break;
                }else{
                    prefix[j]=symbol;
                    j++;
                }
            }
            i++;
       }else{
            prefix[j]=infix[i];
            j++;
            i++;
       }
    }
    for(i=j;Stack.top!=-1;i++){
        prefix[i]=pop();
    }
    strrev(prefix);
    printf("Result : %s\n",prefix);
return 0;
}

Comments

Popular posts from this blog

c/c++ Programming questions || Practice Problems for Beginners || C Programs List

Change your Application background in Scene Builder || JavaFX FXML Tutorial

Implementation of Graph in C || Directed and Undirected Graph