广义表

来源:互联网 发布:瑞典隆德大学知乎 编辑:程序博客网 时间:2024/05/20 10:24
#include#include#include#include//广义表的头尾链表存储结构typedef enum { ATOM, LIST } ELemTag;typedef int AtomType;//ATOM=0,表示原子//List=1,表示子表typedef struct GLNode{ELemTag tag;//用于区别原子节点和表节点union {AtomType atom;//即int atom用作原子类型节点的值域成员struct {struct GLNode *hp, *tp;}htp;//定义了表节点结构体,/*只要是表,就可以递归减小规模,拆成表头表尾,因此只有元素节点中才会存放具体值,表节点中是两个指针,一指表头,一指表尾*/}atom_htpx;//是原子节点值域和表节点指针域的联合体域 }*GList;//广义表//求深度int GListDepth(GList L){if (!L)return 1;//空表深度为1if (L->tag == ATOM){return 0;}GList pp;int dep;int max;for (max = 0, pp = L; pp; pp = pp->atom_htpx.htp.tp){dep = GListDepth(pp->atom_htpx.htp.hp);if (dep > max)max = dep;}return max + 1;}//复制广义表void GListCopy(GList &T, GList L){if (!L)T = NULL;else{T = (GList)malloc(sizeof(GLNode));if (!T)exit(OVERFLOW);T->tag = L->tag;if (L->tag == ATOM){T->atom_htpx.atom = L->atom_htpx.atom;}else{GListCopy(T->atom_htpx.htp.hp, L->atom_htpx.htp.hp);GListCopy(T->atom_htpx.htp.tp, L->atom_htpx.htp.tp);}}}//建立广义表void CreateGList(GList &L, char str[]){if (strcmp(str, "()") == 0)L = NULL;else{if (strlen(str) == 1){L = (GList)malloc(sizeof(GLNode));L->tag = ATOM;L->atom_htpx.atom = str[0];}else{L = (GList)malloc(sizeof(GLNode));L->tag = LIST;SubString()}}}void DestroyGList(GList &L){if (!L)return;if (L->tag == LIST){defstroy }}
原创粉丝点击