#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX 50
typedef enum {ATOM,LIST}ElemTag;
typedef char AtomType;
typedef struct lnode{
ElemTag flag; //ATOM==0原子; LIST==1 子表
union{ //原子结点和表结点的联合部分
AtomType atom;
struct lnode * hp;
}val;
struct lnode * tp;
}*Gl, Lnode;
void InitGlnode(Gl *gl, char *ch); //创建广义表(扩展线性链表的存储结构)
bool sever(char *head, char *tail); //分离函数
int Gllen(Gl gl); //长度
int Depth(Gl gl); //深度
void show(Gl gl); //打印
void pshow(Gl gl);
int main(void)
{
Gl gl = NULL;
char ch1[MAX] = "((a,b),(c,d))";
char ch2[MAX] = "((a),(b),((c)),((1,2,(r,(g,h,e),h))),(h))";
char ch3[MAX] = "((),(),())";
char ch4[MAX] = "(a,((b,c),d))";
char ch5[MAX] = "((),(e),(a,(b,c,d)))";
InitGlnode(&gl, ch2);
show(gl);
// pshow(gl);
printf("\n");
printf("len = %d\n",Gllen(gl));
printf("dep = %d\n",Depth(gl));
return 0;
}
void InitGlnode(Gl *gl, char *ch) //创建广义表(扩展线性链表的存储结构)
{
int len = strlen(ch);
char head[MAX];
char tail[MAX];
if(len == 0) //空串直接返回空
{
*gl = NULL;
}
else if(len == 1) //创建原子结点
{
(*gl) = (Gl)malloc(sizeof(Lnode));
(*gl)->flag = ATOM;
(*gl)->val.atom = ch[0];
(*gl)->tp = NULL;
}
else if(ch[0] == '(')
{
strcpy(tail, ch);
sever(head, tail); //分离串
if(head[0] == '(' && strlen(tail) == 0) //说明是这种(a,b,c)类型的串,无法分离
{
strcpy(tail, head);
head[0] = '\0';
(*gl) = (Gl)malloc(sizeof(Lnode)); //建立 LIST 结点
(*gl)->flag = LIST;
(*gl)->val.hp = NULL;
(*gl)->tp = NULL;
strcpy(tail, ch);
strncpy(tail,ch + 1,len-2); //分离最外层括号
tail[len-2] = '\0';
sever(head, tail); //分离串
InitGlnode(&(*gl)->val.hp, head); //递归创建
if((*gl)->val.hp != NULL && strlen(tail) != 0)
InitGlnode(&(*gl)->val.hp->tp, tail);
}
else //(a,b),c 这中类型的串
{
InitGlnode(gl, head); //递归创建
InitGlnode(&(*gl)->tp, tail);
}
}
else //分离出 原子结点
{
strcpy(tail, ch);
sever(head, tail);
InitGlnode(gl, head); //递归创建
InitGlnode(&(*gl)->tp, tail);
}
}
bool sever(char *head, char *tail) //分离函数
{
int k = 0;
int n = strlen(tail);
int i = 0;
char ch = tail[0];
if(*tail == '\0')
{
*head = '\0';
*tail = '\0';
return true;
}
while(i < n &&(ch != ',' || k != 0 ))
{
if(ch == '(')
k++;
else if(ch == ')')
k--;
i++;
ch = tail[i];
}
if(i < n)
{
tail[i] = '\0';
strcpy(head, tail);
strcpy(tail, tail+i+1);
}
else if(k != 0)
{
return false;
}
else
{
strcpy(head, tail);
tail[0] = '\0';
}
return true;
}
void show(Gl gl) //打印
{
if(gl != NULL)
{
if(gl->flag == ATOM)
printf("%c", gl->val.atom);
else
{
printf("(");
if(gl->val.hp != NULL)
show(gl->val.hp);
printf(")");
}
if(gl->tp != NULL)
{
printf(",");
show(gl->tp);
}
}
}
int Gllen(Gl gl)
{
int i = 0;
Gl p = gl->val.hp;
while(p != NULL)
{
i++;
p = p->tp;
}
return i;
}
int Depth(Gl g) //深度
{
int max = 0, dep;
Gl g1;
if(g->flag == ATOM)
return 0;
g1 = g->val.hp;
if(g1 == NULL)
return 1;
while(g1 != NULL)
{
if(g1->flag == LIST)
{
dep = Depth(g1);
if(dep > max)
max = dep;
}
g1 = g1->tp;
}
return max + 1;
}
void pshow(Gl gl)
{
if(gl == NULL)
return;
else
{
if(gl->flag == ATOM)
printf("%c ", gl->val.atom);
else
pshow(gl->val.hp);
pshow(gl->tp);
}
}
11-07
5376
11-15
2235
03-24
1397