The code is as follows:
#include <stdio.h>#include<stdlib.h>#include<ctype.h>#defineStack_init_size 20#defineStackincrement 10typedefCharElemtype;typedefstruct{elemtype*Base; Elemtype*top; intStackSize;} Sqstack;voidInitstack (Sqstack *s) {s-Base= (Elemtype *)malloc(Stack_init_size *sizeof(Elemtype)); if(!s->Base) {exit (0); } s->top = s->Base; S->stacksize =stack_init_size;}voidPush (Sqstack *S,elemtype e) { if(s->top-s->Base>= s->StackSize) {s-Base= (Elemtype *)realloc(s->Base, (s->stacksize+stackincrement) *sizeof(Elemtype)); if(!s->Base) {exit (0); } } * (s->top) =e; S->top++;}voidPop (sqstack *s,elemtype *e) { if(S->top = = s->Base){ return; } *e = *--(s->top);}intStacklen (Sqstack s) {return(S.top-s.Base);}intMain () {sqstack s; CharC, E; Initstack (&s); printf ("Please enter infix expression with # As the end flag:"); scanf ("%c",&c); while(c! ='#' ){ while(c>='0'&& c<='9') {printf ("%c", c); scanf ("%c",&c); if(c<'0'|| C>'9') {printf (" "); } } if(')'==c) {pop (&s, &e); while('('!=e) {printf ("%c", E); Pop (&s,&D); } } Else if('+'==c | |'-'==c) {if( !Stacklen (s)) {Push (&s,c); } Else { Do{pop (&s,&e); if('('==e) {push (&s,e); } Else{printf ("%c", E); } } while(Stacklen (s) &&'('!=e); Push (&s,c); } } Else if('*'==c | |'/'==c | |'('==c) {push (&s,c); } Else if('#'==c) { Break; } Else{printf ("Error , input malformed \ n"); return-1; } scanf ("%c",&c); } while(Stacklen (s)) {pop (&s,&e); printf ("%c", E); } return 0;}
Operation Result:
C language Implementation infix expression to suffix expression