示例#1
0
int main(void){
	GList L;
	InitGList(L);
	SString S;
	InitString(S,"(123),(12ddfa)");
	CreateGList(L,S);

	printf("\nResult End!\n");
	system("pause");
	return 0;
}
示例#2
0
 Status CreateGList(GList *L,HString S)
 { /* 采用头尾链表存储结构,由广义表的书写形式串S创建广义表L。设emp="()" */
   HString emp,sub,hsub;
   GList p,q;
   InitString(&emp);
   InitString(&sub);
   InitString(&hsub);
   StrAssign(&emp,"()");
   if(!StrCompare(S,emp)) /* 创建空表 */
     *L=NULL;
   else
   {
     *L=(GList)malloc(sizeof(GLNode));
     if(!*L) /* 建表结点不成功 */
       exit(OVERFLOW);
     if(StrLength(S)==1) /* 创建单原子广义表 */
     {
       (*L)->tag=ATOM;
       (*L)->a.atom=S.ch[0];
     }
     else
     {
       (*L)->tag=LIST;
       p=*L;
       SubString(&sub,S,2,StrLength(S)-2); /* 脱外层括号 */
       do /* 重复建n个子表 */
       {
         sever(&sub,&hsub); /* 从sub中分离出表头串hsub */
         CreateGList(&p->a.ptr.hp,hsub);
         q=p;
         if(!StrEmpty(sub)) /* 表尾不空 */
         {
           p=(GList)malloc(sizeof(GLNode));
           if(!p)
             exit(OVERFLOW);
           p->tag=LIST;
           q->a.ptr.tp=p;
         }
       }while(!StrEmpty(sub));
       q->a.ptr.tp=NULL;
     }
   }
   return OK;
 }
示例#3
0
 Status CreateGList(GList &L,HString S)
 { // 采用头尾链表存储结构,由广义表的书写形式串S创建广义表L。设emp="()"
   HString emp,sub,hsub;
   GList p,q;
   InitString(emp);
   InitString(sub);
   InitString(hsub);
   StrAssign(emp,"()");
   if(!StrCompare(S,emp)) // 创建空表
     L=NULL;
   else
   {
     if(!(L=(GList)malloc(sizeof(GLNode)))) // 建表结点不成功
       exit(OVERFLOW);
     if(StrLength(S)==1) // 创建单原子广义表
     {
       L->tag=ATOM;
       L->atom=S.ch[0];
     }
     else
     {
       L->tag=LIST;
       p=L;
       SubString(sub,S,2,StrLength(S)-2); // 脱外层括号
       do // 重复建n个子表
       {
         sever(sub,hsub); // 从sub中分离出表头串hsub
         CreateGList(p->ptr.hp,hsub);
         q=p;
         if(!StrEmpty(sub)) // 表尾不空
         {
           if(!(p=(GList)malloc(sizeof(GLNode))))
             exit(OVERFLOW);
           p->tag=LIST;
           q->ptr.tp=p;
         }
       }while(!StrEmpty(sub));
       q->ptr.tp=NULL;
     }
   }
   return OK;
 }
 Status CreateGList(GList *L,SString S) /* 算法5.7 */
 { /* 采用头尾链表存储结构,由广义表的书写形式串S创建广义表L。设emp="()" */
   SString sub,hsub,emp;
   GList p,q;
   StrAssign(emp,"()");
   if(!StrCompare(S,emp))
     *L=NULL; /* 创建空表 */
   else
   {
     *L=(GList)malloc(sizeof(GLNode));
     if(!*L) /* 建表结点 */
       exit(OVERFLOW);
     if(StrLength(S)==1) /* S为单原子 */
     {
       (*L)->tag=ATOM;
       (*L)->a.atom=S[1]; /* 创建单原子广义表 */
     }
     else
     {
       (*L)->tag=LIST;
       p=*L;
       SubString(sub,S,2,StrLength(S)-2); /* 脱外层括号 */
       do
       { /* 重复建n个子表 */
         sever(sub,hsub); /* 从sub中分离出表头串hsub */
         CreateGList(&p->a.ptr.hp,hsub);
         q=p;
         if(!StrEmpty(sub)) /* 表尾不空 */
         {
           p=(GLNode *)malloc(sizeof(GLNode));
           if(!p)
             exit(OVERFLOW);
           p->tag=LIST;
           q->a.ptr.tp=p;
         }
       }while(!StrEmpty(sub));
       q->a.ptr.tp=NULL;
     }
   }
   return OK;
 }
示例#5
0
 void main()
 {
   char p[80];
   GList l,m;
   HString t; /* 与main5-51.c不同 */
   InitString(&t); /* 增加此句 */
   InitGList(&l);
   InitGList(&m);
   printf("空广义表l的深度=%d l是否空?%d(1:是 0:否)\n",GListDepth(l),GListEmpty(l));
   printf("请输入广义表l(书写形式:空表:(),单原子:a,其它:(a,(b),b)):\n");
   gets(p);
   StrAssign(&t,p);
   CreateGList(&l,t);
   printf("广义表l的长度=%d\n",GListLength(l));
   printf("广义表l的深度=%d l是否空?%d(1:是 0:否)\n",GListDepth(l),GListEmpty(l));
   printf("遍历广义表l:\n");
   Traverse_GL(l,visit);
   printf("\n复制广义表m=l\n");
   CopyGList(&m,l);
   printf("广义表m的长度=%d\n",GListLength(m));
   printf("广义表m的深度=%d\n",GListDepth(m));
   printf("遍历广义表m:\n");
   Traverse_GL(m,visit);
   DestroyGList(&m);
   m=GetHead(l);
   printf("\nm是l的表头,遍历广义表m:\n");
   Traverse_GL(m,visit);
   DestroyGList(&m);
   m=GetTail(l);
   printf("\nm是l的表尾,遍历广义表m:\n");
   Traverse_GL(m,visit);
   InsertFirst_GL(&m,l);
   printf("\n插入l为m的表头,遍历广义表m:\n");
   Traverse_GL(m,visit);
   printf("\n删除m的表头,遍历广义表m:\n");
   DestroyGList(&l);
   DeleteFirst_GL(&m,&l);
   Traverse_GL(m,visit);
   printf("\n");
   DestroyGList(&m);
 }
示例#6
0
 void main()
 {
   char p[80];
   SString t;
   GList l,m;
   InitGList(&l);
   InitGList(&m);
   printf("空广义表l的深度=%d l是否空?%d(1:是 0:否)\n",GListDepth(l),GListEmpty(l));
   printf("请输入广义表l(书写形式:空表:(),单原子:(a),其它:(a,(b),c)):\n");
   gets(p);
   StrAssign(t,p);
   CreateGList(&l,t);
   printf("广义表l的长度=%d\n",GListLength(l));
   printf("广义表l的深度=%d l是否空?%d(1:是 0:否)\n",GListDepth(l),GListEmpty(l));
   printf("遍历广义表l:\n");
   Traverse_GL(l,visit);
   printf("\n复制广义表m=l\n");
   CopyGList(&m,l);
   printf("广义表m的长度=%d\n",GListLength(m));
   printf("广义表m的深度=%d\n",GListDepth(m));
   printf("遍历广义表m:\n");
   Traverse_GL(m,visit);
   DestroyGList(&m);
   m=GetHead(l);
   printf("\nm是l的表头元素,遍历m:\n");
   Traverse_GL(m,visit);
   DestroyGList(&m);
   m=GetTail(l);
   printf("\nm是由l的表尾形成的广义表,遍历广义表m:\n");
   Traverse_GL(m,visit);
   InsertFirst_GL(&m,l);
   printf("\n插入广义表l为m的表头,遍历广义表m:\n");
   Traverse_GL(m,visit);
   printf("\n删除m的表头,遍历广义表m:\n");
   DestroyGList(&l);
   DeleteFirst_GL(&m,&l);
   Traverse_GL(m,visit);
   printf("\n");
   DestroyGList(&m);
 }
示例#7
0
 Status CreateGList(GList &L,SString S) // 算法5.7
 { // 采用头尾链表存储结构,由广义表的书写形式串S创建广义表L。设emp="()"
   SString sub,hsub,emp;
   GList p,q;
   StrAssign(emp,"()");
   if(!StrCompare(S,emp))
     L=NULL; // 创建空表
   else
   {
     if(!(L=(GList)malloc(sizeof(GLNode)))) // 建表结点
       exit(OVERFLOW);
     if(StrLength(S)==1) // S为单原子
     {
       L->tag=ATOM;
       L->atom=S[1]; // 创建单原子广义表
     }
     else
     {
       L->tag=LIST;
       p=L;
       SubString(sub,S,2,StrLength(S)-2); // 脱外层括号
       do
       { // 重复建n个子表
         sever(sub,hsub); // 从sub中分离出表头串hsub
         CreateGList(p->ptr.hp,hsub);
         q=p;
         if(!StrEmpty(sub)) // 表尾不空
         {
           if(!(p=(GLNode *)malloc(sizeof(GLNode))))
             exit(OVERFLOW);
           p->tag=LIST;
           q->ptr.tp=p;
         }
       }while(!StrEmpty(sub));
       q->ptr.tp=NULL;
     }
   }
   return OK;
 }