|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedef struct BTnode{char data; struct BTnode *lchild; struct BTnode *rchild;}Btnode,*Btree;typedef struct Qnode{char data; struct Qnode *next;}Qnode;typedef struct{Qnode *front; Qnode *rear;}LinkQueue;int count=0;LinkQueue InitQueue(LinkQueue *q)//创建队列{ Qnode *p; p=(Qnode *)malloc(sizeof(Qnode)); p->next=NULL; q->front=q->rear=p; return *q;}void EnQueue(LinkQueue *q,char a)//入队{ Qnode *p; p=(Qnode *)malloc(sizeof(Qnode)); p->data=a; p->next=NULL; q->rear->next=p; q->rear=p;}char DeQueue(LinkQueue *q)//出队{ Qnode *p;char x; if(q->front==q->rear) printf("队列为空,无法删除。\n"); return false; p=q->front->next;//为什么是next??? x=p->data; q->front->next=p->next; if(q->rear==p) q->rear=q->front;//到这一步很多都不是很懂 free(p); return x;}Btree creatBtree()//创建{ Btree tree; char a; scanf("%c",&a); if('0'==a) tree =NULL; else {tree=(Btree )malloc(sizeof(Btree)); tree->data=a; tree->lchild=creatBtree(); tree->rchild=creatBtree(); } return tree;}void LevelOrder(Btree tree)//层次{ char a; LinkQueue *q; InitQueue(q); EnQueue(q,tree->data); while(q->front!=q->rear) {a=DeQueue(q); printf("%c\n",a); if(tree->lchild!=NULL) EnQueue(q,tree->lchild->data); if(tree->rchild!=NULL) EnQueue(q,tree->rchild->data); }}int main(){ int level=1,i; Btree tree=NULL; printf("请输入树的数据(请按先序遍历的规则输入):\n"); tree=creatBtree(); while(tree!=NULL) printf(" 3.输出层次遍历*\n"); scanf("%d",&i); switch(i){ case 4:printf("层次遍历的结果为:\n");LevelOrder(tree);break; } } printf("该二叉树的深度为:%d",TreeDepth(tree)); return 0; }
每次运行就出现exe.已停止运行
|
|