数据结构 栈表达式求值

来源:百度知道 编辑:UC知道 时间:2024/05/14 17:40:03
请大家帮帮忙 设计一个程序 使用栈求解算数表达式 谢谢了啊!!!

用链栈写的中序表达式 希望对你有帮助

#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct Fnode/*符号单链表*/
{
int yxj;
char fh;
struct Fnode *next;
}Fnode;

void F_push(Fnode *h,char c)/*尾插*/
{
Fnode *p;
p=(Fnode*)malloc(sizeof(Fnode));
p->fh=c;
p->next=NULL;
if(c=='+'||c=='-')
p->yxj=1;
else if(c=='*'||c=='/')
p->yxj=2;
else if(c=='('||c==')')
p->yxj=0;
else
{ free(p); return;}
while(h->next!=NULL)
h=h->next;
p->next=h->next;
h->next=p;
}

char F_pop(Fnode *h)/*尾删*/
{
char c;
while(h->next->next!=NULL)
h=h->next;
c=h->next->fh;
free(h->next);
h->next=NULL;
return c;