基本操作:
#include<stdio.h>
typedef struct lnode
{
int tag; //判断是原子节点还是子表结点
union
{
elemtype data; //存放原子结点值
struct lnode *sublist;//指向子表的指针
} val;
sttruct lnode *link;
} GLNode;
void fun1(GLNode *g)
{
GLNode *g1=g->val.sublist;
while(g1!=NULL)
{
if(g1->tag==1)
fun1(g1);
else
{
//进行原子处理
}
g1=g1->link;
}
}
void fun2(GLNode *g)
{
if(g!=NULL)
{
if(g->tag==1)
{
fun2(g->val.sublist);
}
else
{
//进行原子处理
}
}
fun2(g->link);
}
int length(GLNode *g) //求广义表的长度
{
int i=0;
GLNode *g1=g->val.sublist;
while(g1!=NULL)
{
i++;
g1=g1->link;
}
return i;
}
int hength(GLNode *g) /*不熟*/
{
GLNode *g1;
int max,dep;
if(g->tag==0)
return 0;
g1=g->val.sublist;
if(g1==NULL)
return 1;
while(g1!=NULL)
{
if(g1->tag==1) //g1是否是子表,若g1是原子则深度为0;
{
dep=hength(g1);
if(dep>max)
max=dep;
}
g1=g1->link;
}
return max+1;
}
void print(GLNode *g)
{
if(g!=NULL)
{
if(g->tag==0)
printf("%d",g->val.data);
else
{
printf("( ");
if(g->val.sublist==NULL)
printf("#");
else
{
print(g->val.sublist);
printf(")");
}
}
if(g->link!=NULL)
{
printf(",");
print(g->link);
}
}
}
GLNode init(char *&s) //建立广义表
{
GLNode *g;
char ch=*s++;
if(ch!='\0')
{
g=(GLNode *)maoolc(sizeof(GLNode));
switch(ch)
{
case '(': g->tag=1;g->val.sublist=init(s);break;
case ')': g==NULL;break;
case '#': g=NULL;break;
default : g->tag=0;g->val.data=ch; break;
}
}
else
{
g=NULL;
}
ch=*s++;
if(g!=NULL)
{
if(ch==',')
g->link=init(s);
else
{
g->link=NULL;
}
}
return g;
}
void destory(GLNode *&g) //销毁广义表
{
GLNode *g1,*g2;
g1=g->val.sublist;
while(g1!=NULL)
{
if(g1->tag==0)
{
g2=g1->link;
free(g1);
g1=g2;
}
else
{
g2=g1->link;
destory(g1);
g1=g2;
}
}
free(g);
}
int count1(GLNode *g) //计算原子数
{
int n=0;
if(g->tag==0)
n++;
else
n+=count1(g->val.sublist);
n+=count1(g->link);
return n;
}
int count2(GLNode *g) //计算原子数
{
int n;
while(g!=NULL)
{
if(g->tag==0)
n++;
else
{
n+=count2(g->val.sublist);
}
g=g->link;
}
return n;
}
例题:
#include<stdio.h>
#include<stdlib.h>
#define elemtype char
typedef struct lnode
{
int tag;
union
{
elemtype data;
struct lnode *sublist;
} val;
struct lnode *link;
}GLNode;
GLNode *init(char *&s) //初始化一个广义表
{
GLNode *g;
char ch=*s++;
if(ch!='\0')
{
g=(GLNode *)malloc(sizeof(GLNode));
if(ch=='(')
{
g->tag=1;
g->val.sublist=init(s);
}
else if(ch==')'||ch=='#')
{
g=NULL;
}
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
g->tag=0;
g->val.data=ch;
}
}
else
{
g=NULL;
}
/*ch后移一位得到ch';若ch'是逗号则g->link不为空*/
ch=*s++;
if(g!=NULL)
{
if(ch==',')
g->link=init(s);
else
g->link=NULL;
}
return g;
}
void print(GLNode *g)
{
if(g!=NULL)
{
if(g->tag==0)
printf("%c ",g->val.data);
else
{
printf("( ");
if(g->val.sublist==NULL)
printf("#");
else
{
print(g->val.sublist);
}
//printf("0k!");
printf(" )");
}
}
if(g==NULL)
{
printf("#");
}
if(g->link!=NULL)
{
printf(",");
print(g->link);
}
}
int hength(GLNode *g)
{
int max=0,dep;
GLNode *g1;
if(g->tag==0)
return 0;
else
{
g1=g->val.sublist;
if(g1==NULL)
return 1;
else
{
while(g1!=NULL)
{
dep=hength(g1);
if(max<dep)
max=dep;
g1=g1->link;
}
}
}
return max+1;
}
int length(GLNode *g)
{
int i=0;
//GLNode *g1=g->val.sublist;
while(g!=NULL)
{
i++;
g=g->link;
}
return i;
}
void destory(GLNode *&g)
{
GLNode *g1=g->val.sublist,*g2;
while(g1!=NULL)
{
if(g1->tag==0)
{
g2=g1->link;
free(g1);
g1=g2;
}
else
{
g2=g1->link;
destory(g1);
g1=g2;
}
}
free(g);
}
int main()
{
int h,l;
char *s="(b,(b,a,(#),d),((a,b),c,((#))))";
// char *s="(#)";
GLNode *p;
p=init(s);
printf("广义表:\n");
print(p);
printf("\n");
h=hength(p);
printf("广义表的深度h: %d\n",h);
l=length(p);
printf("广义表的长度L: %d\n",l);
destory(p);
printf("已销毁广义表\n");
return 0;
}