C语言高手帮忙看一下

来源:百度知道 编辑:UC知道 时间:2024/05/06 19:08:44
这个是广义表的,怎么会有问题呢
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef enum{ATOM, LIST}ElemTag;
typedef struct GLNode
{
int tag;
union
{
char atom;
struct GLNode *sublist;
};
struct GLNode *next;
}GList;

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

int main(void)
{
int i;
GList *gl;
CreateGList(&gl);
PrintGList(gl);
return 0;
}

void CreateGList(GList **gl)
{
char ch;
scanf("%c", &ch);
if(ch == '\n'||ch==')')
{
*gl = NULL;
}
else if(ch == '(')
{
*gl = (GList *)malloc(sizeof(GList));
(*gl)->tag = LIST;
Creat

你有没有注意你的程序出错时,输出的内容是什么?应该是多了个逗号,然后出错吧。说明表遍历结束后
if(gl->next != NULL)
{
printf(",");
PrintGList(gl->next);
}
仍然执行了,也就是说,你的最后一个next没赋为NULL。说明程序对最后一个)都是执行了
if(ch == '\n'||ch==')')
{
*gl = NULL;
}
这段,而这段的gl肯定是某个sublist,因此,最后一个结点的next域没赋NULL。

这个创建表的代码好像我昨天给一个问创建表问题的人改的,不知道是不是你。当时没考虑打印的问题,所以程序不是很完善。