void Create(GList1 &L, SString S) { SString emp, sub, hsub; GList1 p; StrAssign(emp, "()"); if (!(L = (GList1)malloc(sizeof(GLNode1)))) exit(OVERFLOW); if (!StrCompare(S, emp)) { L->tag = LIST; L->hp = NULL; } else if (StrLength(S) == 1) { L->tag = ATOM; L->atom = S[1]; } else { L->tag = LIST; SubString(sub, S, 2, StrLength(S) - 2); sever(sub, hsub); Create(L->hp, hsub); p = L->hp; while (!StrEmpty(sub)) { sever(sub, hsub); Create(p->tp, hsub); p = p->tp; } } L->tp = NULL; }
void CreateGList(GList1 &L, SString S) { SString emp, sub, hsub; GList1 p; StrAssign(emp, "()"); if (!StrCompare(S, emp)) { InitGList(L); } else { SubString(sub, S, 2, StrLength(S) - 2); sever(sub, hsub); Create(L->hp, hsub); p = L->hp; while (!StrEmpty(sub)) { sever(sub, hsub); Create(p->tp, hsub); p = p->tp; } p->tp = NULL; } }
status create_gen_list(gen_list &gl, char *str) { int n = strlen(str); char *sub = (char*)malloc(sizeof(char) * (n - 2)); char *head_sub = (char*)malloc(sizeof(char) * (n - 2)); assert(sub != NULL && head_sub != NULL); /* sub = "1, 2, 3" */ strncpy(sub, str+1, n-2); sub[n - 2] = '\0'; /* Create head node */ if(gl == NULL) { gl = (gl_node*)malloc(sizeof(gl_node)); assert(gl != NULL); gl->tag = head; gl->head_p = gl->tail_p = NULL; } /* Create general list */ gl_node *p = gl; while(strlen(sub) != 0) { p = p->tail_p = (gl_node*)malloc(sizeof(gl_node)); assert(p != NULL); p->head_p = p->tail_p = NULL; /* 1, 2, 3 ==> head_sub = "1", sub = "2, 3" */ /* (1, 2), 3, 4 ==> head_sub = "(1, 2)", sub = "3, 4" */ /* Separate sub string and head_sub string */ if(sever(sub, head_sub)) { if(head_sub[0] == '(') { p->tag = child_list; create_gen_list(p->head_p, head_sub); } else { p->tag = atom; p->atom = atoi(head_sub); } } } }
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; }
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; }
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; }