自建个网站怎么做,行业网站联盟,厦门企业网站建设,做网站设计可以参照别人的么广义表#xff08;Generalized List#xff09;是一种扩展了线性表的数据结构#xff0c;它在线性表的基础上增加了元素可以是表的特点。在广义表中#xff0c;元素不仅可以是单个的数据元素#xff0c;还可以是一个子表#xff0c;而子表中的元素也可以是数据元素或其他…广义表Generalized List是一种扩展了线性表的数据结构它在线性表的基础上增加了元素可以是表的特点。在广义表中元素不仅可以是单个的数据元素还可以是一个子表而子表中的元素也可以是数据元素或其他的子表这样递归定义形成了一种层次结构
#include stdio.h
#include stdlib.h// 定义广义表节点结构
typedef struct GLNode {char data; // 可以根据需要修改为其他类型struct GLNode *next; // 指向下一个节点struct GLNode *child; // 指向子表
} GLNode, *GList;// 创建广义表节点
GLNode *CreateGListNode(char data) {GLNode *node (GLNode *)malloc(sizeof(GLNode));if (node) {node-data data;node-next NULL;node-child NULL;}return node;
}// 插入节点到广义表
void InsertGListNode(GList *list, char data) {GLNode *node CreateGListNode(data);if (node) {node-next *list;*list node;}
}// 插入子表到广义表
void InsertChildGListNode(GList *list, GList child) {GLNode *node CreateGListNode(\0); // 使用空字符表示子表if (node) {node-child child;node-next *list;*list node;}
}// 打印广义表
void PrintGList(GList list) {GLNode *p list;while (p) {if (p-data ! \0) {printf(%c , p-data);} else {printf(();PrintGList(p-child);printf() );}p p-next;}
}// 释放广义表空间
void FreeGList(GList list) {GLNode *p list;while (p) {GLNode *temp p;p p-next;if (temp-child) {FreeGList(temp-child);}free(temp);}
}int main() {GList list NULL;// 创建广义表 (a, (b, c))InsertGListNode(list, a);GList child1 NULL;InsertGListNode(child1, b);InsertGListNode(child1, c);InsertChildGListNode(list, child1);// 打印广义表printf(广义表: );PrintGList(list);printf(\n);// 释放空间FreeGList(list);return 0;
}