Posts

Showing posts with the label linked-list

Infix to postfix implementation using linked lists in C

Infix to postfix implementation using linked lists in C I've been trying to debug this program for a long time. It works fine when i input expressions like a+b-c or a/b+c where the first operator has a greater or equal precedence than the second. But for expressions like a-b/c where the first operator has a lesser precedence than the second, the compiler throws a breakpoint. struct stack { char ele; struct stack *next; }; void push(int); int pop(); int precedence(char); struct stack *top = NULL; int main() { char infix[20], postfix[20]; int i=0,j=0; printf("ENTER INFIX EXPRESSION: "); gets(infix); while(infix[i]!='') { if(isalnum(infix[i])) postfix[j++]=infix[i]; else { if(top==NULL) push(infix[i]); else { while(top!=NULL && (precedence(top->ele)>=precedence(infix[i]))) postfix[j++]=pop(); push(infix[i]); } } ++i; } while(top!...