有人能帮忙看看,为什么这个程序过不了吗,实在是找不到错在哪了
#include<iostream>
using namespace std;
typedef enum{ATOM,LIST}ElemTag; //ATOM==0:原子;LIST==1:子表
string emp = "()";
typedef struct GLNode
{
ElemTag tag;
union
{
char atom;
struct { struct GLNode* hp, * tp; }ptr;
}union1;
}*GList;
void sever(string& head, string& Tail) {
//将非空串str分割成两部分,hstr是表头
int n = head.size();
int i = -1;
int k = 0; //k记录尚未配对的“(” 数
char ch;
do { //搜索最外层第一个(
++i;
ch = head[i];
if (ch == '(')
++k;
else if (ch == ')')
--k;
} while (i < n && (ch != ',' || k != 0));
if (i < n) {
Tail = head.substr(0, i);
//printf("in\n");
head = head.substr(i + 1, n - i - 1);
//printf("out\n");
}
else {
Tail = head.substr(0, head.size());
head.clear();
}
}
void CreateGList(GList& L, string s) {
//采用头尾链表存储结构,创建L
if ((s.compare(emp) == 0)||s.size()==0) L = NULL;
else {
L = new GLNode;
if (!L) exit(0);
if (s.size() == 1) { //单个元素,建立原子节点
L->tag = ATOM;
L->union1.atom = s[0];
}
else { //表节点 ,表尾
L->tag = LIST;
GList p, q;
p = L; //p是指向当前子表(表尾节点)的指针
string sub;
sub = s.substr(1, s.size() - 2); //去掉外层括号
string hsub;
do { //重复建立n个子表
sever(sub, hsub); //sub中分理出表头串hsub ,同时,sub去除了hsub
CreateGList(p->union1.ptr.hp, hsub);
q = p; //记录p,下面sub不为空,要再建立一个表尾节点,q记录上一层的p,用以连接q->ptr.tp = q
if (!sub.empty()) {
p = new GLNode;
if (!p)exit(0);
p->tag = LIST;
q->union1.ptr.tp = p;
}
} while (!sub.empty());
q->union1.ptr.tp = NULL;
}
}
}
int Equal(GList A, GList B)
/* 判断广义表A和B是否相等,是则返回TRUE,否则返回FALSE */
{
if (A == NULL) {
if (B == NULL)
return 1;
else
return 0;
}
if (A->tag == ATOM) {
if (B->tag == ATOM && B->union1.atom == A->union1.atom)
return 1;
else return 0;
}
if (Equal(A->union1.ptr.hp, B->union1.ptr.hp))
if (Equal(A->union1.ptr.tp, B->union1.ptr.tp))
return 1;
else return 0;
else return 0;
}
int main() {
string s1;
string s2;
cin >> s1;
cin >> s2;
GList L1;
GList L2;
CreateGList(L1, s1);
CreateGList(L2, s2);
if (Equal(L1, L2))
{
cout << 1;
}
else
{
cout << 0;
}
return 0;
}