c语言编译时出现叉,用C语言创建多叉树有关问题

#include#define OK 1

#define ERROR 0

#define FALSE 0

#define TRUE 1

typedef char Elemtype;

typedef struct CSNode

{

Elemtype data;

struct CSNode *firstchild,*nextsibling,*pretree;

}CSNode,*CSTree;

typedef CSTree TElemtype;

typedef struct QNode

{

TElemtype data;

struct QNode*next;

}QNode,*QueuePtr;

typedef struct

{

QueuePtr front;

QueuePtr rear;

}LinkQueue;

int InitQueue(LinkQueue &Q)

{

(Q).front=(Q).rear=(QueuePtr)malloc(sizeof(QNode));

if(!(Q).front)exit(0);

(Q).front->next=NULL;

return OK;

}

int EnQueue(LinkQueue &Q,TElemtype e)

{

QueuePtr p;

p=(QueuePtr)malloc(sizeof(QNode));

if(!p) return(0);

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

return OK;

}

int DeQueue(LinkQueue &Q,TElemtype e)

{  if(Q.front==Q.rear) return ERROR;

else

{

QueuePtr a;

a=Q.front->next;

e=a->data;

Q.front->next=a->next;

if(a==Q.rear) Q.rear=Q.front;

free(a);

return OK;

}

}

int QueueEmpty(LinkQueue &Q)

{

if(Q.rear==Q.front)

return OK;

else

return ERROR;

}

int InitTree(CSTree &T)

{

T=NULL;

return OK;

}

void DestroyTree(CSTree &T)

{

if(T)

{

if((T)->firstchild)

DestroyTree((T)->firstchild);

if((T)->nextsibling)

DestroyTree((T)->nextsibling);

free(T);

T=NULL;

}

}

int CreateTree(CSTree &T)

{

int i,len;//len用于计算孩子数目

TElemtype p,q;//q是根结点,p是q的孩子

char temp[30]; //存放孩子元素

LinkQueue Q;

char s;

InitQueue(Q);

printf("请输入树的根:");

(T)->data=getchar();

(T)=(TElemtype)malloc(sizeof(CSNode));

(T)->data=getchar();

(T)->pretree=NULL;

(T)->nextsibling=NULL;

(T)->firstchild=NULL;

EnQueue(Q,T);

while(!QueueEmpty(Q))

{

DeQueue(Q,q);//q为双亲

printf("请输入%c结点的孩子,按“回车键”结束输入:",q->data);

gets(temp);

len=strlen(temp);

if(len==NULL) q->firstchild=NULL;

else

{

q->firstchild=(TElemtype)malloc(sizeof(CSNode));

p=q->firstchild;

p->data=temp[0];

p->pretree=q;

EnQueue(Q,p);

for(i=1;i

{

p->nextsibling=(TElemtype)malloc(sizeof(CSNode));

p->nextsibling->data=temp[i];

p->nextsibling->pretree=p;

EnQueue(Q,p->nextsibling);

p=p->nextsibling;

}

p->nextsibling=NULL;

}

}

return OK;

}

int main()

{

TElemtype T;

CreateTree(T);

}

------解决方案--------------------

你的指针都分配内存了吗?

T就没有。

------解决方案--------------------

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。

提醒:再牛×的老师也无法代替学生自己领悟和上厕所!

单步调试和设断点调试是程序员必须掌握的技能之一。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值