雅蓉最萌 发表于 2015-11-13 19:05:11

数据结构二叉树层次遍历和队列问题

#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.已停止运行

DAY 发表于 2015-11-15 20:10:47

你这代码书写让人无语啊!!!!

DAY 发表于 2015-11-15 20:11:18

代码摆整齐一点啊!

dps521 发表于 2015-11-29 12:58:07

过来看看一起学习一起研究一下:smile:smile:smile:smile

sfdjhwfjwg 发表于 2015-12-5 17:11:16

调用出错
页: [1]
查看完整版本: 数据结构二叉树层次遍历和队列问题