# 广义表的创建（头尾链表存储表示）

0

1  // c5-4.h 广义表的头尾链表存储结构。在教科书第109页
2  enum ElemTag{ATOM,LIST};//编译器自动将ATOM==0，原子 LIST==1 字表
3  typedef struct GLNode
4  { ElemTag tag;
5    union
6    { AtomType atom;
7      struct
8      { GLNode *hp,*tp;
9      }ptr;
10    };
11  }*GList,GLNode;

1 void CreateGList(GList &L,SString S)
2  {
3    SString sub,hsub,emp;
4    GList p,q;
5    StrAssign(emp,"()");
6    if(!StrCompare(S,emp))
7      L=NULL;
8    else
9    { if(!(L=(GList)malloc(sizeof(GLNode))))
10        exit(OVERFLOW);
11      if(StrLength(S)==1)
12      { L->tag=ATOM;
13        L->atom=S[1];
14      }
15      else
16      { L->tag=LIST;
17        p=L;
18        SubString(sub,S,2,StrLength(S)-2);
19
20        do
21        { sever(sub,hsub);
22          CreateGList(p->ptr.hp,hsub);
23          q=p;
24          if(!StrEmpty(sub))
25          { if(!(p=(GLNode*)malloc(sizeof(GLNode))))
26              exit(OVERFLOW);
27            p->tag=LIST;
28            q->ptr.tp=p;
29          }
30        }while(!StrEmpty(sub));
31        q->ptr.tp=NULL;
32      }
33    }
34  }
35
36 void sever(SString str,SString hstr)
37  {
38    int n,k=0,i=0;
39    SString ch,c1,c2,c3;
40    StrAssign(c1,",");
41    StrAssign(c2,"(");
42    StrAssign(c3,")");
43    n=StrLength(str);
44    do
45    { ++i;
46      SubString(ch,str,i,1);
47      if(!StrCompare(ch,c2))
48        ++k;
49      else if(!StrCompare(ch,c3))
50        --k;
51    }while(i<n&&StrCompare(ch,c1)||k!=0);
52    if(i<n)
53    { SubString(hstr,str,1,i-1);
54      SubString(str,str,i+1,n-i);
55    }
56    else
57    { StrCopy(hstr,str);
58      ClearString(str);
59    }
60  }

1

angelshelter | 大侠五级 |园豆：9678 | 2013-11-11 17:38

您需要登录以后才能回答，未注册用户请先注册