#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就没有。
------解决方案--------------------
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试是程序员必须掌握的技能之一。