我基本是按王道数据结构书里的代码敲的,但是运行不了,还有两个警告,请求指点
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef char ElemType;
//二叉树
typedef struct BSTNode{
ElemType data;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
//队列节点
typedef struct{
ElemType data;
struct LinkNode *next;
}LinkNode;
//queue
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
//初始化队列
void InitQueue(LinkQueue Q){
Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
//判空
bool IsEmpty(LinkQueue Q){
printf("\nisempty");
if(Q.front==Q.rear) return true;
else
return false;
}
//入队
void EnQueue(LinkQueue Q,BSTree T){
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=T->data;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
//出队
bool DeQueue(LinkQueue Q,BSTree p){
if(Q.front==Q.rear) return false;
LinkNode *t=Q.front->next;
p->data=t->data;
Q.front->next=t->next;
if(Q.rear==t)
Q.rear=Q.front;
free(t);
return true;
}
//按先序遍历的顺序创建二叉树
BSTree Create(){
BSTree T=NULL;
ElemType ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else{
T=(BSTree)malloc(sizeof(BSTNode));
T->data=ch;
T->lchild=Create();
T->rchild=Create();
}
return T;
}
//层次遍历
void LevelOrder(BSTree T,LinkQueue Q){
InitQueue(Q);
BSTree p;
EnQueue(Q,T);//根节点入队
while(!IsEmpty(Q)){
DeQueue(Q,p);//队头节点出队
printf("%c",p->data);//访问出队节点
if(p->lchild!=NULL)
EnQueue(Q,p->lchild);
if(p->rchild!=NULL)
EnQueue(Q,p->rchild);
}
}
int main(){
BSTree T;
LinkQueue Q;
T=Create();
LevelOrder(T,Q);
return 0;
}