EXPERIMENT – 5
AIM
Write a program to implement the conversion of infix to postfix expression using Stack.
INTRODUCTION
Infix notation is a way of writing arithmetic expressions in which the operators are placed between the
operands. For example, the infix notation of the expression “3 + 4” is “3 + 4”. Postfix notation, also
known as Reverse Polish Notation (RPN), is another way of writing arithmetic expressions in which the
operators are placed after the operands. For example, the postfix notation of the expression “3 + 4” is
“3 4 +”. To convert an infix expression to postfix, we can use the stack data structure.
C CODE
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == ‘(‘)
return 0;
if(x == ‘+’ || x == ‘-‘)
return 1;
if(x == ‘*’ || x == ‘/’)
return 2;
return 0;
int main()
char exp[100];
char *e, x;
printf(“Enter the expression : “);
scanf(“%s”,exp);
printf(“\n”);
e = exp;
while(*e != ‘\0’)
if(isalnum(*e))
printf(“%c “,*e);
else if(*e == ‘(‘)
push(*e);
else if(*e == ‘)’)
while((x = pop()) != ‘(‘)
printf(“%c “, x);
else
while(priority(stack[top]) >= priority(*e))
printf(“%c “,pop());
push(*e);
e++;
while(top != -1)
printf(“%c “,pop());
}return 0;
RESULT
CONCLUSION
Infix to Postfix conversion program in c has been successfully made and implemented.