#include
#define MAXSTACK 5
typedef struct
{
int sp;
char Data[MAXSTACK];
}STACK;
void Initialize(STACK *s);
int push(STACK *s,char item);
int pop(STACK *s,char *ret);
int full(STACK *s);
int empty(STACK *s);
void Initialize(STACK *s)
{
s->sp=0;
}
int push(STACK *s,char item)
{
if(full(s))
{
printf("Stack is full !!!\n");
return 0;
}
else
{
// printf("\tpush -> %d\n",item);
s->Data[s->sp++]=item;
}
return 1;
}
int full(STACK *s)
{
if(s->sp==MAXSTACK)
return 1;
return 0;
}
int pop(STACK *s,char *ret)
{
if(empty(s))
{
// printf("Stack is empty !!!\n");
return 0;
}
else
*ret=s->Data[--s->sp];
return 1;
}
int empty(STACK *s)
{
if(s->sp==0)
return 1;
return 0;
}
void main(void)
{
STACK s;
char *infix="a+b-c+d+e";
char ch;
int i;
Initialize(&s);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]>='a' && infix[i] <= 'z')
{
printf("%c",infix[i]);
}
else{
if (pop(&s,&ch))
printf("%c",ch);
push(&s,infix[i]);
}
}
if (pop(&s,&ch))
printf("%c",ch);
printf("\n");
}
No comments:
Post a Comment