Conversion of Infix to Prefix Expression in C
Let's take the example infix expression to see how the infix to prefix algorithm works:
((a/b)+c)-(d+(e*f))
Step 1: Reverse the Infix Expression and Swap Parentheses
Original Infix: ((a/b)+c)-(d+(e*f))
Reversed String: ))f*e(+d(-)c+)b/a((
Swap Parentheses: ((f*e)+d)-(c+(b/a))
Step 2: Convert Reversed Infix to Postfix
Now, let's convert ((f*e)+d)-(c+(b/a)) to a postfix expression.
Here's the step-by-step table for this process:
S. No Infix expression Stack Prefix Expression
0 ( ((
1 ( (((
2 f ((( f
3 * (((* f
4 e (((* fe
5 ) (( fe*
6 + ((+ fe*
7 d ((+ fe*d
8 ) ( fe*d+
9 - (- fe*d+
10 ( (-( fe*d+
11 c (-( fe*d+c
12 + (-(+ fe*d+c
13 ( (-(+( fe*d+c
S. No Infix expression Stack Prefix Expression
14 b (-(+( fe*d+cb
15 / (-(+(/ fe*d+cb
16 a (-(+(/ fe*d+cba
17 ) (-(+ fe*d+cba/
18 ) (- fe*d+cba/+
19 ) fe*d+cba/+-
Final prefix: -+/abc+d*ef
Implementation of Infix to Prefix in C Using Array
In this infix to prefix algorithm,
The code for converting an infix expression to a prefix expression using an array works by
following three key steps: reversing the infix expression, converting it to postfix, and then
reversing the postfix to obtain the prefix. Here’s how it goes:
1. Reversing the Infix Expression
The input infix expression is first reversed. During this process, the left parentheses
(are swapped with right parentheses) and vice versa.
Example: For ((a/b)+c), the reversed expression becomes )c+(b/a(.
2. Converting Reversed Infix to Postfix
The reversed infix expression is then converted to a postfix expression. This
conversion follows the standard postfix rules:
Operands (like a, b, c, etc.) are directly added to the output.
Operators (like +, -, *, /) are pushed to a stack based on their precedence.
When encountering a right parenthesis ), operators are popped from the stack until a
left parenthesis ( is found.
The precedence of operators dictates the order of popping. For example, * and / have
higher precedence than + and -.
3. Reversing the Postfix to Get Prefix:
Finally, the postfix expression obtained from Step 2 is reversed to form the prefix
expression.
Program to Convert Infix to Prefix
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define max 100
int top=-1; char a[max];
void push(char x)
{
a[++top]=x;
}
char pop()
{ if(top==-1)
return -1;
else
return a[top--];
}
int prcd(char c)
{ if(c==')' || c=='(')
return 0;
else if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
return -1;
}
void reverse(char *str)
{
int i=0, j=strlen(str)-1;
char temp;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;j--;
}
}
void infixtoprefix(char infix[max],char prefix[max])
{
char temp,x;
int i=0,j=0;
reverse(infix);
while(infix[i]!='\0')
{
temp=infix[i];
if(isalnum(temp))
{
prefix[j++]=temp;
}
else if(temp==')')
push(temp);
else if(temp=='(')
{
while((x=pop())!=')' && x!=-1)
{
prefix[j++]=x;
}
}
else
{ while(top!=-1&&prcd(a[top])>=prcd(temp))
{prefix[j++]=pop();}
push(temp);
}
i++;
}
while(top!= -1)
prefix[j++]=pop();
prefix[j]='\0';
reverse(prefix);
}
int main()
{
char infix[max],prefix[max];
printf("Enter the infix expression\n");
scanf("%s",infix);
printf("The infix expression is %s\n",infix);
infixtoprefix(infix, prefix);
printf("The prefix expression is %s\n",prefix);
return 0;
}