Infix Topostfix
Infix Topostfix
h>
#include<stdlib.h>
#include<string.h>
struct stack{
int size;
int top;
char* arr;
};
int stackTop(struct stack * S1){
return S1->arr[S1->top];
}
}
int isOperator(char ch){
if(ch=='+' || ch=='-' || ch=='*' || ch=='/') return 1;
else return 0;
}
char* infixTopostfix(char* infix){
struct stack *S1 = (struct stack*)malloc(sizeof(struct stack));
S1->size = 100;
S1->top = -1;
S1->arr = (char*)malloc(S1->size* sizeof(char));
char * postfix = (char*)malloc((strlen(infix)+1)*sizeof(char));
int i=0,j=0;
while(infix[i]!='\0'){
if(!isOperator(infix[i])){
postfix[j] = infix[i];
i++;
j++;
}
else{
if(precedence(infix[i])>precedence(stackTop(S1))){
Push(S1,infix[i]);
i++;
}
else{
postfix[j] = pop(S1);
j++;
}
}
}
while(!isEmpty(S1)){
postfix[j] = pop(S1);
j++;
}
postfix[j] = '\0';
return postfix;
}
int main(){
char* infix = "a-b";
printf("Postfix is %s",infixTopostfix(infix));