修改如下,请注意标位置,另外还有一些函数缺少返回值语句,但不影响编译(可能会收到警告信息),请自行根据需要修改这部分:
//---------------------------------------------------------------------------
# include
# include
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
char str[100];
typedef enum{ATOM,LIST}ElemTag ;
struct GLNode
{
ElemTag tag;
union
{
char atom;
struct
{
struct GLNode *hp, *tp;
}ptr;
}atom_ptr;
}glists;
struct GLNode *p , *q;
struct stack
{
struct GLNode *elem[MAXSIZE];
int top ;
}S;
struct GLNode *build_ptr() //////////注意这里
{
struct GLNode *p1;
p1=(struct GLNode *)malloc(sizeof(struct GLNode )); /////////注意这里
p1->tag=LIST;
p1->atom_ptr.ptr.hp=NULL;
p1->atom_ptr.ptr.tp=NULL;
return(p1);
}
struct GLNode *build_atom(char c) ///////////注意这里
{
struct GLNode *p1;
p1=( struct GLNode *)malloc(sizeof(struct GLNode));
p1->tag=ATOM;
p1->atom_ptr.atom=c;
return p1;
}
struct GLNode *pop(struct stack *s) ///////////////注意这里
{
if (s->top<0)
{
printf("stack underflow !\n");
return(NULL);
}
else
{
s->top--;
return (s->elem[s->top+1]);
}
}
int push(struct stack *s, struct GLNode *x) /////////////////注意这里
{
if (s->top >=MAXSIZE-1)
{
printf("stack overflow !\n");
return(FALSE);
}
else
{
s->top++;
s->elem[s->top]=x;
return(TRUE);
}
}
void get_glist()
{
int i ;
printf("请输入广义表:\n");
for(i=0;;i++)
{
str[i]=getchar();
}
}
struct GLNode * built_glist()
{
int i;
i=0;
S.top=-1;
while(str[i]!='/0')
{
switch(str[i])
{
case '(':
p=build_ptr();
if(S.top>-1)
S.elem[S.top]->atom_ptr.ptr.hp=p;
push(&S,p);
case ',':
p=build_ptr();
q=pop(&S);
q->atom_ptr.ptr.tp=p;
push(&S,p);
case ')':
pop(&S);
default :
p=build_atom(str[i]);
S.elem[S.top]->atom_ptr.ptr.hp=p;
}
}
}
void main ()
{
get_glist();
built_glist();
}
//---------------------------------------------------------------------------