Professional Documents
Culture Documents
91900133035
DSA
#include<stdio.h>
#include<malloc.h>
#include<string.h>
//for LL Opearation
struct MyStr
char ch;
};
void infixToPostfix();
int getPriority(char );
int operatorORoperand(char);
void addToPostfix(char);
void setStack();
char pop();
int main()
char *p;
p=(char *)malloc(sizeof(char)*50);
scanf("%s",p);
strToLL(p);
//printing Infix
head=infix;
printLL(head);
setStack();
infixToPostfix();
//print POSTFIX
head=postfix;
printLL(head);
return 0;
}
void setStack()
stack->ch='(';
stack->next=NULL;
switch(ch)
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
//operator
return 0;
}
else
//operand
return 1;
int i=0;
while(*(p+i) !='\0')
temp->ch=*(p+i);
temp->next=NULL;
if(infix==NULL)
infix=temp;
else
head=infix;
while(head->next!=NULL)
head=head->next;
head->next=temp;
}
i++;
while(head!=NULL)
printf("%c",head->ch);
head=head->next;
if(head==NULL)
return;
printLLR(head->next);
printf("%c",head->ch);
void infixToPostfix()
inhead=infix;
while(inhead!=NULL)
{
//if operand
if(operatorORoperand(inhead->ch)==1)
addToPostfix(inhead->ch);
//if operator
else
push(inhead->ch);
//== or >
else
addToPostfix(pop());
//'-'
push(inhead->ch);
printf("\n");
printf("%c | ",inhead->ch);
tos=stack;
printLLR(tos);
printf(" | ");
pohead=postfix;
printLL(pohead);
printf("\n");
inhead=inhead->next;
while(stack->ch != '(')
addToPostfix(pop());
void addToPostfix(char c)
temp->ch=c;
temp->next=NULL;
if(postfix==NULL)
postfix=temp;
else
head=postfix;
while(head->next!=NULL)
head=head->next;
head->next=temp;
temp->ch=ch;
temp->next=NULL;
temp->next=stack;
stack=temp;
char pop()
char ch;
ch = stack->ch;
temp=stack;
stack=stack->next;
free(temp);
return(ch);
}