[Data structures]General list
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1024 /* General list */ typedef struct _GLNode{ int tag; //0-atom, 1-node union{ char atom; struct{ struct _GLNode *hp; struct _GLNode *tp; }ptr; }; }GList; int server(char **str, char **hstr) { int n = strlen(*str); int k = 0; int i = 0; char ch; do{ ch = (*str)[i]; if(ch == '('){ k++; }else if(ch == ')'){ k--; } }while(++i<n && (ch != ',' || k != 0) ); if(i<n){ *hstr = *str; (*hstr)[i-1] = 0; *str = *str + i; }else{ *hstr = *str; *str = *str + n; } } int CreateGList(GList **L, char *S) { int n = strlen(S); char *sub = S; char *hsub; GList *p,*q; if(strcmp(sub, "()") == 0){ *L = NULL; }else{ *L = (GList *)malloc(sizeof(GList)); if(strlen(sub) == 1){ (*L)->tag = 0; (*L)->atom = sub[0]; }else{ (*L)->tag = 1; if(sub[0] == '('){ sub[n-1] = 0; sub = sub+1; } p = *L; do{ server(&sub, &hsub); CreateGList(&(p->ptr.hp), hsub); hsub = NULL; if(strlen(sub) != 0){ q = (GList *)malloc(sizeof(GList)); q->tag = 1; p->ptr.tp = q; p = q; } }while(strlen(sub) != 0); p->ptr.tp = NULL; } } return 1; } int GListDepth(GList *L) { GList *p; int max; int dep; if(!L){ return 1; } if(L->tag == 0){ return 0; } for(max = 0, p = L; p; p = p->ptr.tp){ dep = GListDepth(p->ptr.hp); if(dep > max){ max = dep; } } return max + 1; } int CopyGList(GList **T, GList *L) { if(!L){ *T = NULL; }else{ *T = (GList *)malloc(sizeof(GList)); (*T)->tag = L->tag; if(L->tag == 0){ (*T)->atom = L->atom; }else{ CopyGList(&(*T)->ptr.hp, L->ptr.hp); CopyGList(&(*T)->ptr.tp, L->ptr.tp); } } return 1; } /* * There may be a bug that some brackets will lost * */ int PrintGList(GList *L) { if(!L){ printf("()"); return 0; } if(L->tag == 0){ printf("%c", L->atom); }else{ if(L->ptr.hp && L->ptr.hp->tag == 1){ printf("("); } PrintGList(L->ptr.hp); if(L->ptr.tp != NULL){ printf(","); PrintGList(L->ptr.tp); } if(L->ptr.hp && L->ptr.hp->tag == 1){ printf(")"); } } } void main() { GList *L,*T; printf("log.anycle.com\n\n"); printf("Original data:\n"); char *original = "((a,b,c,d,(b,b,d,e)),a,(b,()))"; char *str = malloc(strlen(original) + 1); char *hstr; strcpy(str, original); printf("%s\n", str); printf("Create general list:\n"); CreateGList(&L, str); printf("General list depth:\n"); printf(" %d\n", GListDepth(L)); printf("Copy general list:\n"); CopyGList(&T, L); printf("Print general list:\n"); PrintGList(L); printf("\n"); }
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: