C++ 实现广义表存储的问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 20:20:49
用C++语言实现

设置广义表的存储结构。如:L=( a, (b,c,d), ((e,f),g), (h,i) )

主要是我不清楚如何实现广义表

给你一个作为参考吧:)

#include<stdio.h>
#include<conio.h>
#include <stdio.h>
#include <stdlib.h>

typedef char elemType;

/************************************************************************/
/* 以下是关于广义表操作的4个简单算法 */
/************************************************************************/
struct GNode{
int tag; /* 标志域:取0表示单元素结点;取1时表示子表结点 */
union{
elemType data;
struct GNode *subList;
};
struct GNode *next; /* 指向后继结点的指针域 */
};

/* 1.求广义表的长度 */
int lenthGList(struct GNode *gl)
{
if(gl != NULL){
return 1 + lenthGList(gl->next);
}else{
return 0;
}
}

/* 2.求广义表的深度 */
int depthGList(struct GNode *gl)
{
int max = 0;
/* 遍历每个结点,求出所以子表的最大深度 */
while(gl != N