#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 *postfix="ab+c-d+e+";
char left,right;
int i;
Initialize(&s);
for(i=0;postfix[i]!='\0';i++)
{
if(postfix[i]>='a' && postfix[i] <= 'z')
{
push(&s,postfix[i]);
}
else{
if (pop(&s,&right)){
if (pop(&s,&left)){
printf("%c",left);
}
printf("%c",postfix[i]);
printf("%c",right);
}
}
}
if (pop(&s,&right))
printf("%c",right);
printf("\n");
}
1 comment:
Thank You Very Much
Post a Comment