树的创建问题
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
//那就等现实给你一巴掌,醒了。就不沉沦了…
typedef int Status;
typedef char TElemType;
TElemType Nil=' ';
#define form "%d"
typedef struct CSNode{
TElemType data; //顶点信息
struct CSNode *firstchild,*nextsibling;//第一个孩子,第一个的孩子的兄弟
}CSNode,*CSTree;
typedef CSTree QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,* QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q);
Status EnQueue(LinkQueue &Q,QElemType e);
Status DeQueue(LinkQueue &Q,QElemType &e);
Status QueueEmpty(LinkQueue Q);
void CreateTree(CSTree &T){
char c[20];
CSTree p,p1;
LinkQueue q;
int i,m;
InitQueue(q);
printf("请输入根结点(字符型,空格为空):");
scanf("%c",&c[0]);
printf("dsgf%d",c[0]);
//fflush();//////////////////
if(c[0]!=Nil){//非空树
T=(CSTree)malloc(sizeof(CSNode));
T->data=c[0];
T->nextsibling=NULL;
EnQueue(q,T);
while(!QueueEmpty(q)){
DeQueue(q,p);
printf("请按长幼顺序输入结点%c的所有孩子:",p->data);
scanf("%s",c);
if(c[0]==Nil) break;//跳出循环。。。 m=strlen(c);
if(m>0){
p1=p->firstchild=(CSTree)malloc(sizeof(CSNode));
p1->data=c[0];//第一个孩子
EnQueue(q,p1);//入队
for(i=1;i <m;i++){
p1=p->nextsibling=(CSTree)malloc(sizeof(CSNode));
p1->data=c[i];//兄弟
EnQueue(q,p1);//入队
}
p1->nextsibling=NULL;
}
else
p->firstchild=NULL;
}
}
else
T=NULL;
}
void InitTree(CSTree &T);
void main(){
CSTree T;
InitTree(T);
CreateTree(T);
}
树的二叉链表CreatTree()当空格时数的结点为空,if(c[0]==Nil) break;//跳出循环。我是这么解决的可是不对。。。
大侠帮忙看一下。。。。