little__cute 2019-11-14 22:36 采纳率: 0%
浏览 103

为什么在队尾插入元素时总是提示内存出错(C语言新人)

写了一个层序遍历创建二叉树的函数,但在入队列部分提示内存出错,求大佬指点...
出错函数:void EnQueue(BiTree e)
错误提示:内存出错

#include<stdio.h>
#include<stdlib.h>
#define link 1;
#define child 0; 

typedef struct BiTNode{
    char data;
    struct BiTNode* lchild;
    struct BiTNode* rchild;
    int LTag = child;
    int RTag = child;
}BiTNode,*BiTree;

typedef struct QNode{
    BiTNode* data;
    struct QNode* next;
}QNode,*QueuePtr;

typedef struct {
    QueuePtr front;//队头指针
    QueuePtr rear;//队尾指针
}LinkQueue;

LinkQueue Q;//用队列存储需要访问的树的结点

void IniiLinkQueue(){
    Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
    Q.front->next = NULL;

    return ;
}

bool QueueEmpty(){
    //判断队列是否为空队列,若不是,返回true;否则,返回false
    if(Q.front == Q.rear)
        return false;
    return true; 
}

void EnQueue(BiTree e){
    //插入e为Q的新的队尾元素
    QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;//报错位置
    Q.rear = p;

    return ;
}

BiTree DeQueue(){
    //删除Q的队头元素,并返回其值
    BiTree e = Q.front->next->data;
    if(Q.front->next == Q.rear)
        Q.rear = Q.front;
    else
        Q.front->next = Q.front->next->next;
    return e;
}

void createBiTree(BiTree T){
    //按层序遍历创建二叉树
    char ch;

    EnQueue(T);//将树的根结点存入队列
    while(QueueEmpty()){
        while((ch = getchar()) != '\n'){
            T = DeQueue();
            if(ch != '-'){
                //若输入字符不为'-',表示当前结点不为空,将字符存入结点,并将其左右孩子地址存入队列中
                T->data = ch;
                T->lchild = (BiTree)malloc(sizeof(BiTNode));
                T->rchild = (BiTree)malloc(sizeof(BiTNode));
                EnQueue(T->lchild);
                EnQueue(T->rchild); 
            } 
            else{
                //若输入字符为'-',表示当前结点为空,无需将其左右孩子入队
                T->data = NULL; 
            }
        }
    }

    return ; 
}
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题