32.数据结构笔记之三十二广义表实现一
“谁要是游戏人生,他就一事无成;谁不能主宰自己,永远是一个奴隶。--歌德”
我们来看下广义表的代码实现。
1. 广义表实现一
1.1 main
输入一个数字,调用函数creatglist创建广义表。
广义表的内容是一个char,超过一个char就失效。
1.2 定义
typedefstructgnode
{
inttag;
union
{
charatom;
structgnode*sublist;
}val;
structgnode*link;
}*Gnode;
1.3 creatglist
createglist函数,参数是我们输入的一个数字。
如果不是‘\0’,创建一个头节点(malloc)。
如果是’(‘ 表示是子表,则设置tag为1,创建子表链
否则为tag为0。
然后读取下一字符,如果不是‘,’则设置为NULL。
Gnode creatglist(char *s)
{//创建广义表
Gnodeh;
charch;
ch=*s;
s++;
if(ch!='\0') //串未结束判断
{
h=(Gnode)malloc(sizeof(Gnode));
if(!h)printf("存储空间分配失败\n");
if(ch=='(')
{
h->tag=1;
h->val.sublist=creatglist(s); //递归创建广义表
}
elseif(ch==')') h=NULL;
else
{
h->tag=0;
h->val.atom=ch;
}
}
elseh=NULL;
ch=*s;
s++;
if(h!=NULL)
{
if(ch==',')
h->link=creatglist(s);
else
h->link=NULL;
}
returnh;
}
1.4 empty
判断广义表是否为空。
1.5 length
计算广义表的长度。
1.6 Copy
复制广义表。
1.7 insert
向广义表中插入一个字符。
1.8 depth
计算广义表的深度。
1.9 output
输出广义表。
1.10 delfirstnode
删除第一个元素。
1.11 源码
#include <stdio.h>
#include <malloc.h>
#define OK 1
#define ERROR -1
#define status int
typedef struct gnode
{
int tag;
union
{
char atom;
struct gnode *sublist;
} val;
struct gnode *link;
} *Gnode;
Gnode creatglist(char *s)
{//创建广义表
Gnode h;
char ch;
ch=*s;
s++;
if(ch!='\0') //串未结束判断
{
h=(Gnode)malloc(sizeof(Gnode));
if(!h) printf("存储空间分配失败\n");
if(ch=='(')
{
h->tag=1;
h->val.sublist=creatglist(s); //递归创建广义表
}
else if(ch==')') h=NULL;
else
{
h->tag=0;
h->val.atom=ch;
}
}
else h=NULL;
ch=*s;
s++;
if(h!=NULL)
{
if(ch==',')
h->link=creatglist(s);
else
h->link=NULL;
}
return h;
}
void output(Gnode h)
{
if(h!=NULL)
{
if(h->tag==1)
{
printf("(");
if(h->val.sublist==NULL)
printf(" ");
else
output(h->val.sublist);
}
else
printf("%c",h->val.atom);
if(h->tag==1)
printf(")");
if(h->link!=NULL)
{
printf(",");
output(h->link);
}
}
}
void empty(Gnode h)
{
if(h->val.sublist==NULL)
printf("广义表为空\n");
else
printf("广义表非空\n");
}
int length(Gnode h)
{
Gnode p=h;
int len=0;
if(p==NULL)
return len;
else
return length(h->link)+1;
}
Gnode copy(Gnode h)
{
Gnode b;
if(h==NULL) return NULL;
b=(Gnode)malloc(sizeof(Gnode));
b->tag=h->tag;
if(h->tag==1)
b->val.sublist=copy(h->val.sublist);
else
b->val.atom=h->val.atom;
b->link=copy(h->link);
return b;
}
void insert(Gnode &h,char e)
{//把元素e插入广义表h,为其第一个元素
Gnode p;
p=(Gnode)malloc(sizeof(Gnode));
p->tag=0;
p->val.atom=e;
p->link=h->val.sublist;
h->val.sublist=p;
}
int depth(Gnode h)
{//求广义表的深度
int max=0,dep;
while(h!=NULL)
{
if(h->tag==1)
{
dep=depth(h->val.sublist);
if(dep>max)
max=dep;
}
h=h->link;
}
return max+1;
}
void delfirstnode(Gnode &h)
{//删除第一元素并输出其值
Gnode p,q;
p=(Gnode)malloc(sizeof(Gnode));
p=copy(h);
p->val.sublist->link=NULL;
printf("广义表的第一元素为: ");
output(p->val.sublist);printf("\n");
q=h->val.sublist;
h->val.sublist=q->link;
printf("删除第一元素后,广义表h为:\n");
output(h);
}
void main()
{
int len,dep;
char s[50],elem;
Gnode h,b;
scanf("%s",s);
h=creatglist(s);
empty(h);
printf("广义表h为\n");
output(h);printf("\n");
printf("广义表的长度为: ");
len=length(h->val.sublist);
printf("%d\n",len);
b=copy(h);
printf("广义表b为: ");
output(b);printf("\n");
insert(b,'e');
printf("广义表b为: ");
output(b);printf("\n");
printf("广义表h的深度为:\n");
dep=depth(h->val.sublist);
printf("%d\n",dep);
delfirstnode(b);
}