#include<iostream>
using namespace std;
typedef int ElemType;
//结构定义
typedef struct lnode {
int tag;//结点类型标识,0为原子结点,1为子表
union {
ElemType data;
struct lnode* sublist;//指向子表
} val;
struct lnode* link;//指向下一元素
}GLNode,*GList;
GLNode* CreatGL(char* p) {
GLNode* g;
char a = *p++;
if (a != '\n') {
g = (GLNode*)malloc(sizeof(GLNode));
if (a == '(') {
g->tag = 1;
g->val.sublist = CreatGL(p);
}
else if (a == ')') {
g = NULL;
}
else {
g->tag = 0;
g->val.data = a;
}
}
else g = NULL;
a = *p++;
if (g) {
if (a == ',') g->link = CreatGL(p);
else g->link = NULL;
}
return g;
}
int GLDepth(GLNode* g) {
int max = 0, dep;
if (g->tag == 0) return 0;
g = g->val.sublist;
if (g == NULL) return 1;
while (g) {
if (g->tag) {
dep = GLDepth(g);
if (dep > max) max = dep;
}
g = g->link;
}
return (max + 1);
}
void main() {
char s[100];
cin >> s;
char* a = s;
GLNode* p;
p = CreatGL(a);
cout << GLDepth(p) << endl;
cout << GLDepth(p) << endl;
}
数据结构NOJ——14求广义表深度
最新推荐文章于 2024-04-19 08:58:14 发布