广义表

#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 }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值