Data Structure Infix->Postfix

#include
#include
#include

struct stack{
char x;
struct stack *next;
} ;

struct stack *push(struct stack *top, char val){
struct stack *ptr;
ptr=(struct stack*) malloc (sizeof(struct stack*));
ptr->x=val;
if (top==NULL){
top=ptr;
top->next=NULL;
} else {
ptr->next=top;
top=ptr;
}
return top;
}
struct stack *pop (struct stack *top)
{
struct stack *ptr;
ptr=top;
if(top==NULL){
printf(“\n STACK UNDERFLOW”);
}
else
{
top = top -> next;
}

return top;
}
int preced(char val) {
if(val==’+’ || val==’-‘) return 1;
else if (val==’*’ || val==’/’) return 2;
else if (val=='(‘) return 0;
};

void main(){
char infix[51],postf[51]=””;
struct stack *opr;
opr=(struct stack*) malloc (sizeof(struct stack*));
opr=NULL;

int n=0,counter;
do{
printf(“Enter infix string [0-50 digit, single number only] : “);
scanf(“%s”,&infix) ;fflush(stdin);

for(int i=0;i=’0′ && infix[i]<='9')
|| infix[i]=='+'
|| infix[i]=='-'
|| infix[i]=='*'
|| infix[i]=='/'
|| infix[i]=='('
|| infix[i]==')') counter=1;
else {counter=0; break;}
}
} while (counter!=1);

for(int i=0;i=’0’ && infix[i]x!='(‘){
postf[n]=opr->x; n++;
opr=pop(opr);
} opr=pop(opr);
continue;
}

if(opr!=NULL && (preced(infix[i]) x))){
if (preced(infix[i]) == preced(opr->x)){
postf[n]=opr->x; n++;
opr=pop(opr);
opr=push(opr,infix[i]);
} else {
while(opr!=NULL && preced(infix[i]) x)){
postf[n]=opr->x; n++;
opr=pop(opr);
}
opr=push(opr,infix[i]);
}
}
else {
opr=push(opr,infix[i]);
}
}

}
while(opr->next!=NULL){
postf[n]=opr->x; n++;
opr=pop(opr);
} postf[n]=opr->x; n++;
opr=pop(opr);

printf(“\nPostfix: “);
for(int i=0;i<strlen(postf);i++) printf("%c",postf[i]);

getchar();
}

Category: Uncategorized
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>