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

广义表(C语言代码链式存储结构)


typedef enum
{
	ATOM,LIST
}Nodetype;//枚举类型
typedef struct GLnode
{
	Nodetype tag;//原子 或者 子表
	union {
		char atom;//字母
		struct
		{
			struct GLnode* head;
			struct GLndoe* tail;
		};
	};
}GLnode;
//创建原子结点
GLnode* createAtom(char data)
{
	GLnode* newnode = (GLnode*)malloc(sizeof(GLnode));
	if (newnode == NULL)
	{
		perror("error");
		exit(1);
	}
	newnode->tag = ATOM;
	newnode->atom = data;
	return newnode;
}
//创建子表结点
GLnode* createList(GLnode* head, GLnode* tail)
{
	GLnode* newnode = (GLnode*)malloc(sizeof(GLnode));
	if (newnode == NULL)
	{
		perror("error");
		exit(1);
	}
	newnode->tag = LIST;
	newnode->head = head;
	newnode->tail = tail;
	return newnode;
}
//打印广义表
void printNode(GLnode* node)
{
	if(node == NULL)
	{
		printf("()");
	}
	if (node->tag == ATOM)
	{
		printf("%c", node->atom);
	}
	else
	{
		printf("(");
		GLnode* pcur = node;
		while (pcur)
		{
			printNode(pcur->head);
			if (pcur->tail != NULL)
			{
				printf(",");
			}
			pcur = pcur->tail;
		}
		printf(")");
	}
}
int main()
{
	GLnode* atomA = createAtom('a');
	GLnode* atomB = createAtom('b');
	GLnode* atomC = createAtom('c');
	GLnode* atomD = createAtom('d');

	GLnode* suiblist = createList(atomA, createList(atomB, NULL));
	GLnode* list = createList(atomC, createList(suiblist,createList(atomD,NULL)));

	printNode(list);
	printf("\n");
	printNode(suiblist);

}

 

相关文章:

  • Linux基础33-C语言篇之字符串的基础操作【入门级】
  • 浅入浅出Selenium DevTools
  • 【数据结构】_顺序表
  • SringAop
  • 互联网医院实时数据监测智能分析系统设计概述(上)
  • 基于springboot后台管理的校园网站系统(源码+lw+部署文档+讲解),源码可白嫖!
  • 【HarmonyOS NEXT】控制 WebP 格式动图播放次数的实现方案
  • 【Prometheus】prometheus服务发现与relabel原理解析与应用实战
  • 如何用 Python 进行机器学习
  • python学习七
  • linux上搭建Lago并部署
  • word转换为pdf后图片失真解决办法、高质量PDF转换方法
  • C语言32个关键字
  • 面试【进阶】 —— 说下csr、ssr、ssg 的区别?
  • 【MySQL 的数据目录】
  • Spring Cloud Alibaba与Spring Boot、Spring Cloud版本对应关系
  • 【AIDevops】Deepseek驱动无界面自动化运维与分布式脚本系统,初探运维革命之路
  • 进程通信——信号
  • ESP 32控制无刷电机2
  • 【前端进阶】10 掌握前端框架模板引擎的实现原理
  • 大连无网站的企业有哪些/站长之家域名查询排行
  • 做王境泽表情的网站/个人怎么做百度竞价
  • 广告电商怎么做/seo推广费用
  • 怎样自己免费搭建网站/新东方教育培训机构
  • 网站优化哪里好/全国疫情突然又严重了
  • wordpress做网站好吗/seo排名优化公司哪家好