当前位置: 首页 > news >正文

广义表-C语言

广义表(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;
}

http://www.lryc.cn/news/297460.html

相关文章:

  • uniapp+uView 【详解】录音,自制音频播放器
  • 机器学习---概率图模型(隐马尔可夫模型、马尔可夫随机场、条件随机场)
  • cool 框架 node 后端封装三方Api post请求函数
  • awd总结
  • 【react】react+es6+antd5.13.2+ts,antd表格的操作如何在父组件写?
  • virtio笔记
  • 初始web服务器(并基于idea来实现无需下载的tomcat)
  • 软件文档测试
  • 从零开始手写mmo游戏从框架到爆炸(七)— 消息封装
  • 从Unity到Three.js(画线组件line)
  • LCP 30. 魔塔游戏 - 力扣(LeetCode)
  • 数据结构——单向链表和双向链表的实现(C语言版)
  • TCP和UDP相关问题(重点)(4)——4.使用TCP的协议有哪些?使用UDP的协议有哪些?
  • Python进阶--爬取美女图片壁纸(基于回车桌面网的爬虫程序)
  • [office] excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 #笔记#学习方法
  • Pandas 对带有 Multi-column(多列名称) 的数据排序并写入 Excel 中
  • 如何为Kafka加上账号密码(一)
  • Elasticsearch的Index Lifecycle Management(ILM)
  • 2、学习 Nacos 注册中心
  • Java 如何操作 nginx 服务器上的文件?
  • 时序预测 | MATLAB实现基于CNN-GRU-AdaBoost卷积门控循环单元结合AdaBoost时间序列预测
  • 中创ET4410 台式LCR数字电桥 简单开箱测评
  • 格式化dingo返回内容
  • QGIS编译(跨平台编译)之四十六:minizip编译(Windows、Linux、MacOS环境下编译)
  • MySQL进阶查询篇(1)-索引的类型与创建
  • 【STL】list模拟实现
  • 常用的文件系统、存储类型小整理
  • Java写标准输出进度条
  • leetcode 算法 69.x的平方根(python版)
  • 【golang】24、go get 和 go mod:indrect 与 go mod tidy