为了账号安全,请及时绑定邮箱和手机立即绑定

这个我用wintc运行总是提示出现错误,请问该怎么解决?

这个我用wintc运行总是提示出现错误,请问该怎么解决?

青春有我 2023-04-02 18:14:21
大家帮帮忙,我想用c实现广义表的创建和遍历,但再最开始的定义广义表就出现一堆错误。 以下是伪码算法typedef enum { ATOM, LIST } ElemTag; typedef struct GLNode { ElemTag tag; union { AtomType atom; //元素有2个域 struct { struct GLNode *hp,*tp; }ptr; //表有3个域 } } *GList; 我编写的 #include<stdio.h> #define ATOM 0 #define LIST 1 typedef enum { ATOM, LIST}ElemTag ; typedef struct GLNode { ElemTag tag; union { int atom; struct { struct GLNode *hp,*tp; }ptr; } }*GList; GList L; main(){} 没学过enum 和union的用法所以我觉得是不是这两个函数用错了。请大家帮我改下程序顺便告诉我那两个函数该怎么用,谢谢。
查看完整描述

1 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

囧~~
#include <stdio.h>
#include <stdlib.h>
#define NULL 0

typedef enum{ATOM, LIST}ElemTag; /*ATOM==0:原子,LIST==1:子表*/

typedef struct GLNode 
{
int tag; /*公共部分,区分原子和表结点*/
union /*原子结点和表结点的联合部分*/ 

char atom; /*原子结点的值域*/  
struct GLNode *sublist; /*表结点表头指针*/
};
struct GLNode *next; /*下一个元素结点*/
}GList;

void CreateGList(GList **gl);
void PrintGList(GList *gl);
int GListDepth(GList *gl);

int main(void)
{
GList *gl;

printf("建立一个广义表,以右括号结束\n");
CreateGList(&gl);

printf("输出广义表:");
PrintGList(gl);

printf("\n");

printf("广义表的深度:");
printf("%d\n", GListDepth(gl->sublist));

return 0;
}

/*************************************************
函数名称:CreateGList

函数功能:创建一个广义表(递归构件子表)
输入的时候是换一行输入,最后要多输右括号,是递归的原因

被本函数调用的函数清单:无

调用本函数的函数清单:无

输入参数:gl,取用指针的指针的形式,可以对其直接修改

输出参数:无

函数返回值:(void)
**************************************************/
void CreateGList(GList **gl)
{
char ch;
scanf("%c", &ch);
getchar(); /*吞食scanf留下的换行*/

if(ch == '#') /*如果输入的是#表示为空*/
{
*gl = NULL;
}
else if(ch == '(') /*如果是左括号就递归构件子表*/
{
*gl = (GList *)malloc(sizeof(GList));
(*gl)->tag = LIST;
CreateGList(&((*gl)->sublist));
}
else /*就是只有原子的情况下*/
{
*gl = (GList *)malloc(sizeof(GList));
(*gl)->tag = ATOM;
(*gl)->atom = ch;
}

scanf("%c", &ch); /*此处输入的必为逗号或者右括号*/
getchar();

if((*gl) == NULL)
{
;
}
else if(ch == ',') /*如果是逗号就递归构件下一个子表*/
{
CreateGList(&((*gl)->next));
}
else if(ch == ')') /*如果是右括号就结束*/
{
(*gl)->next = NULL;
}
}

/*************************************************
函数名称:GListDepth

函数功能:求广义表的深度(递归子表->到子表..(最长+1))

被本函数调用的函数清单:无

调用本函数的函数清单:无

输入参数:gl

输出参数:无

函数返回值:(void)
**************************************************/
int GListDepth(GList *gl)
{
int max, dep;

if(!gl)
return 1;

for(max = 0; gl; gl = gl->next)
{
if(gl->tag == LIST)
{
dep = GListDepth(gl->sublist); /*求以gl->sunlist的子表深度*/

if(dep > max)
{
max = dep;
}//if
}//if
}//for

return max + 1; /*各元素的深度的最大值加一*/
}

/*************************************************
函数名称:PrintGList

函数功能:打印广义表(递归打印子表)

被本函数调用的函数清单:无

调用本函数的函数清单:无

输入参数:gl

输出参数:无

函数返回值:(void)
**************************************************/
void PrintGList(GList *gl)
{
if(gl->tag == LIST)  
{
printf("("); /*先输出左括号*/

if(gl->sublist == NULL)
{
printf("#");
}
else
{
PrintGList(gl->sublist); /*递归打印子表*/
}
printf(")"); /*结束打印右括号*/
}
else
{
printf("%c", gl->atom);

}

if(gl->next != NULL) /*如果没结束就继续递归打印子表*/
{
printf(", ");
PrintGList(gl->next);
}


查看完整回答
反对 回复 2023-04-05
  • 1 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信