+ -
当前位置:首页 → 问答吧 → Unhandled exception in bitree.exe:0xc000005 :access Violation

Unhandled exception in bitree.exe:0xc000005 :access Violation

时间:2011-11-11

来源:互联网

C/C++ code

/***********************bitree.cpp**************/
#include <iostream.h>
#include "struct.h"
int main()
{
    Stack *sp=NULL;
    Bitree *B_tree=NULL;
    cout<<"please create a bitree first"<<endl;
    create(sp,B_tree);    
    return 0;
}

C/C++ code

/***************struct.h********************************/
#define MAX 5                //节点个数

typedef struct node 
{
    char data;
    struct node *left,*right;
}Node;

typedef struct tree
{
    Node *root;

}Bitree;

typedef struct stackdef 
{
    Bitree *top;
    Bitree *base;

}Stack;

void init_stack(Stack *sp);
int  empty(Stack *sp);
void push(Stack *sp,Bitree *B_tree);
void pop(Stack *sp);
void create(Stack *sp ,Bitree *B_tree);


C/C++ code

/******************function.cpp*************************/
#include <iostream.h>
#include "struct.h"
#include <malloc.h>

#define NULL 0


void init_Node(Bitree *B_tree)
{
    B_tree->root=NULL;

}

void init_stack(Stack *sp)            //初始化栈初始化
{
    
        //sp->top=NULL;//如果这两句加上执行这句也会出现同样的问题
    //sp->base=NULL;
    sp->base=new  Bitree [MAX];//(Bitree *)malloc(sizeof(Node)*MAX);  //就是运行在这里时出现错误
    if(!sp->base)
        cout<<"内存不足!"<<endl;
    sp->top=sp->base;    
}

Bitree * gettop(Stack *sp)    返回栈顶
{
    return sp->top;
}

int  empty(Stack *sp)     //判断栈是否为空
{
    return (sp->top==sp->base?0:1);
}

void push(Stack *sp,Bitree *t)
{    
    if(sp->top-sp->base==(MAX-1))
        throw "栈满!\n";
    else
        sp->top = t;
        sp->top++;
}

void pop(Stack *sp)
{
    if(sp->base==sp->top)
        throw "栈空!\n";
    sp->top = NULL;
    sp->top--;
}

void create(Stack *sp,Bitree *Btemp)//构建二叉树
{
    char flag;
    char ch;
    init_stack(sp);
    cout<<"input a char :"<<'\t';
    cin.get(ch);
    if(ch!='#')
    {
        Node *temp=(Node *)malloc(sizeof(Node));
        Btemp->root=temp;
        push(sp,Btemp);
    }
    while(empty(sp)==1)   
    {         //栈非空
        cout<<"input a char :"<<'\t';
        cin.get(ch);
        Btemp=sp->top;
        if(ch!='#')      
        {
            Node *temp=(Node *)malloc(sizeof(Node));
            Btemp->root=temp;
            push(sp,Btemp);
            flag='l';
        }
        else
        {
            if(flag=='l')
            {
                Btemp->root->left=NULL;
                flag='r';
            }
            else
            {
                Btemp->root->right=NULL;
                pop(sp);
            }
        }

    }
}


我想用堆栈的方法构建一个二叉树
但运行到栈初始化函数时就出现下面的问题
Unhandled exception in bitree.exe:0xc000005 :access Violation

我也有点不确定我的我的栈结构体定义是否有问题 求大神帮忙看一下 

作者: wulin900515   发布时间: 2011-11-11

C/C++ code
int main()
{
    Stack *sp=NULL;
    Bitree *B_tree=NULL;
    cout<<"please create a bitree first"<<endl;

    //create函数需要sp有效不是NULL才行
    //在create前先要给sp赋个有效的值,比如 sp = new Stack;
    create(sp,B_tree);    
    return 0;
}


另外你的代码里又有new,又有malloc,不统一啊,这样不太好

作者: oo   发布时间: 2011-11-11

恩 这个问题解决了 谢谢啦!
但这个程序还是有问题 [
code=C/C++]
void push(Stack *sp,Bitree *t)
{
if(sp->top-sp->base==(MAX-1))
throw "栈满!\n";
else
{
sp->top= t;
*(sp->top)++;
}
}

void pop(Stack *sp)
{
if(sp->base==sp->top)
throw "栈空!\n";
sp->top = NULL;
*(sp->top)--;
}
[/code]

就是这个入栈出栈程序 求继续帮帮忙

作者: wulin900515   发布时间: 2011-11-11

热门下载

更多