#include "stdio.h"
#include "conio.h"
typedef int DataType;
typedef struct node
{ DataType info;
struct node *link;
}Node,*Stack;
void InitStack(Stack *p)
{
Stack q;
q =(Stack)malloc(sizeof(Node));
q->link=NULL;
*p = q;
return;
} /* 初值化栈 */
void Push(Stack H,DataType x)
{
Stack q;
q =(Stack)malloc(sizeof(Node));
q->info=x;
q->link=H->link;
H->link=q;
return;
}/* 数值进栈*/
void Pop(Stack H)
{
Stack q;
q = H->link;
H->link=H->link->link;
free(q);
return;
}/*数值出栈 */
DataType Top(Stack H)
{
return H->link->info;
}/* 显示栈的头信息 */
int EmptyStack(Stack H)
{
if(H->link==NULL)
return 1;
else
return 0;
}/* 清空栈 */
void infix2postfix(char *expr)
{
Stack S;
int op;/*to get the oeprator or operand*/
int top;
InitStack(&S);
while(*expr!='/0')
{
op=*expr;
if(op>='a' && op<= 'z')
printf("%c", op);
else
{
top= Top(S);
if(EmptyStack(S)||op=='(')
Push(S,op);
else if(op=='*' || op=='/')
{
while(top=='*'||top=='/')
{
printf("%c",top);
Pop(S);
if(EmptyStack(S))
break;
top=Top(S);
}
Push(S,op);
}
else if(op=='+' || op=='-')
{
while(top!='('&&!EmptyStack(S))
{
printf("%c",top);
Pop(S);
if(EmptyStack(S))
break;
top=Top(S);
}
Push(S,op);
}
else if(op==')')
{
while(top!='(')
{
Pop(S);
printf("%c",top);
top=Top(S);
}
Pop(S);
}
}
expr++;
}
while(!EmptyStack(S))
{
printf("%c", Top(S));
Pop(S);
}
return;
}/* 完成后序变前序的功能 */
void main()
{
printf("The infix travellist is:/n");
printf("a+b*c+(d*e+f)*g/n");
printf("The postfix travelist is:/n");
infix2postfix("a+b*c+(d*e+f)*g"); +
getch();
return;
}