#include"iostream.h"
#include"string.h"
typedef struct lnode
{int tag;
union
{int data;
struct lnode *sublist;
}val;
struct lnode *link;
}GLNode;
GLNode *head(GLNode *p)
{return(p->val.sublist);
}
GLNode *tail(GLNode *p)
{return(p->link);
}
GLNode *CreateGL(char *&s)
{GLNode *h;
char ch=*s++;
if(ch!='\0')
{h=(GLNode *)malloc(sizeof(GLNode));
if(ch=='(')
{h->tag=1;
h->val.sublist=CreateGL(s);
}
else if(ch==')')
h=NULL;
else
{h->tag=0;
h->val.data=ch;
}
}
else
{h=NULL;}
ch=*s++;
if(ch==',')
h->link=CreateGL(s);
else
h->link=NULL;
return h;
}
void DispGL(GLNode *h)
{if(h!=NULL)
{if(h->tag==1)
{cout<<"("<<endl;
if(h->val.sublist==NULL)
cout<<" "<<endl;
else
DispGL(h->val.sublist);
}
else
cout<<h->val.data<<endl;
if(h->tag==1)
cout<<" "<<endl;
if(h->link!=NULL)
{cout<<","<<endl;
DispGL(h->link);
}
}
}
void main()
{char *s;
GLNode *glist,*h,*t;
strcpy(s,"(a,(b,c,d))");
glist=CreateGL(s);
cout<<"广义表:"<<endl;
DispGL(glist);
h=head(glist);
cout<<"表头:"<<endl;
if(h->tag==0)
cout<<h->val.data<<endl;
else
DispGL(h);
t=tail(glist);
cout<<"表尾:"<<endl;
DispGL(t);
}




为什么会出现   D:\MSDev98\MyProjects\求表头尾\1.cpp(21) : error C2065: 'malloc' : undeclared identifier
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)