鱼C论坛

 找回密码
 立即注册
查看: 3555|回复: 4

[已解决]数据结构二叉树层次遍历和队列问题

[复制链接]
发表于 2015-11-13 19:05:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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.已停止运行
最佳答案
2015-11-15 20:10:47
你这代码书写让人无语啊!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-11-15 20:10:47 | 显示全部楼层    本楼为最佳答案   
你这代码书写让人无语啊!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-11-15 20:11:18 | 显示全部楼层
代码摆整齐一点啊!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-11-29 12:58:07 | 显示全部楼层
过来看看  一起学习一起研究一下:smile:smile:smile:smile
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-5 17:11:16 | 显示全部楼层
调用出错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 15:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表