DSA03 Infix2postfix
DSA03 Infix2postfix
INSTITUTE OF TECHNOLOGY
DHULE (M.S.)
DEPARMENT OF COMPUTER ENGINEERING
Remark
Subject : Data Structure Lab
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char stack[SIZE];
int top = -1;
char pop()
{
char item;
if (top < 0) {
printf("stack under flow: invalid infix expression");
getchar();
exit(1);
}
else {
item = stack[top];
top = top - 1;
return (item);
}
}
push('(');
strcat(infix_exp, ")");
i = 0;
j = 0;
item = infix_exp[i];
push(item);
}
else if (item == ')')
{
x = pop();
while (x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else {
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;
item = infix_exp[i];
}
if (top > 0) {
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if (top > 0) {
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
postfix_exp[j] = '\0';
}
int main()
{
char infix[SIZE], postfix[SIZE];
InfixToPostfix(infix, postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
Output: