C++ I';我试图在cpp中使用链表实现二叉树,但我的代码不起作用

C++ I';我试图在cpp中使用链表实现二叉树,但我的代码不起作用,c++,linked-list,stack,binary-tree,C++,Linked List,Stack,Binary Tree,我想使用链表实现二叉树,为此我还使用了使用链表的堆栈和队列 堆栈和队列我也用过链表 在我的代码中没有语法错误,它的编译成功,但是运行代码的时间是错误的 请有人找出我的错误并加以纠正 在下面的代码中,我将int划分为三个单独的文件:- 第一个文件是类文件(用于定义所有类,如堆栈、队列、树) 第二个文件是所有函数体都存在的函数文件,在每个类中声明 第三个文件是主文件 第一个文件(h类) #ifndef类 #定义类 #包括 使用名称空间std; //树类(开始) 类节点 { 公众: 节点*lchild

我想使用链表实现二叉树,为此我还使用了使用链表的堆栈和队列

堆栈和队列我也用过链表

在我的代码中没有语法错误,它的编译成功,但是运行代码的时间是错误的

请有人找出我的错误并加以纠正

在下面的代码中,我将int划分为三个单独的文件:- 第一个文件是类文件(用于定义所有类,如堆栈、队列、树)

第二个文件是所有函数体都存在的函数文件,在每个类中声明

第三个文件是主文件

第一个文件(h类)

#ifndef类
#定义类
#包括
使用名称空间std;
//树类(开始)
类节点
{
公众:
节点*lchild;
int数据;
节点*rchild;
};
类树
{
私人:
Node*root=NULL;
公众:
树();
无效前序();
};                  
//树类(结束)。。。
//队列类(开始)
类Q_节点
{
公众:
节点*数据;
Q_节点*下一步;
};
类队列
{
私人:
Q_节点*首先,*后方;
公众:
Queue(){first=reare=new Q_Node;}
无效排队(节点*x);
节点*dequeue();
void display();
int是空的();
};
//树类(结束)。。。
//堆栈类(开始)
类S_节点
{
公众:
节点*数据;
S_节点*下一步;
};
类堆栈
{
私人:
S_节点*顶部;
公众:
堆栈(){top=NULL;}
无效推送(节点*x);
Node*pop();
int是空的();
};
//堆栈类(结束)。。。
#恩迪夫
第二个文件(functions.cpp)

#包括“classes.h”
//队列类的函数
无效队列::排队(节点*x){
Q_节点*t;
t=新的Q_节点;
如果(!t)

cout对于您的
队列
类,您似乎无法确定
first
是包含第一个元素的节点还是包含第一个元素的节点的前身

Queue::Queue
first
初始化为
data
未初始化的节点。
Queue::enqueue
然后在
first
之后添加一个新节点,
dequeue
返回
first
中的
数据
指针,该指针仍未初始化,因此指向任意地址访问此地址有很高的机会产生访问冲突错误,这就是您的情况


顺便说一句:注意

Q_Node *t;
t = new Q_Node;
if(!t)
    cout<<"Queue is full"<<endl;
else ...
#include "classes.h"


                                            //functions of queue class
void Queue::enqueue(Node *x){
    Q_Node *t;
    t = new Q_Node;
    if(!t)
        cout<<"Queue is full"<<endl;
    else{
        t->data = x;
        t->next = NULL;
        if(!first) first = t;
        if(reare) reare->next = t;
        reare = t;
    }
}

Node * Queue::dequeue(){
    Q_Node *t;
    Node *x = NULL;
    if(!first)
        cout<<"Queue is empty"<<endl;
    else{
        t = first;
        x = t->data;
        first = first->next;
        delete t;
    }
    return x;
}

int Queue::isEmpty(){
    if(first == reare)
        return 1;
    return 0;
}


                                            //functions of stack class

void Stack::push(Node *x){
    S_Node *t;
    t = new S_Node;

    if(!t)
        cout<<"Stack overflow"<<endl;
    else{
        t->data = x;
        t->next = top;
        top = t;
    }
}

Node * Stack::pop(){
    Node *x = NULL;
    S_Node *t;
    if(!top){
        cout<<"Stack is empty"<<endl;
    }else{
        x = top->data;
        t = top;
        top = top->next;
        delete t;
    }
    return x;
}

int Stack::isEmpty(){
    if(top == NULL)
        return 1;
    return 0;
}

                                            //functions of tree class
tree::tree(){
    int x;
    Node *t, *p;

    Queue q;

    cout<<"Enter root value"; cin>>x;

    root = new Node;
    root->data = x;
    root->lchild=root->rchild = NULL;
    q.enqueue(root);

    while(!q.isEmpty()){
        p = q.dequeue();

        cout<<"Enter left child value of "<<p->data<<": "; cin>>x;
        if(x != 1){
            t = new Node;
            t->data = x;
            t->lchild = t->rchild = NULL;
            p->lchild = t;
            q.enqueue(t);
        }

        cout<<"Enter right child value of "<<p->data<<": "; cin>>x;
        if(x != 1){
            t = new Node;
            t->data = x;
            t->lchild = t->rchild = NULL;
            p->rchild = t;
            q.enqueue(t);
        }
    }
}

void tree::preorder(){
    Node *p = root;
    Stack s;
    while(p || !s.isEmpty()){

        if(p){
            cout<<p->data<<" ";
            s.push(p);
            p = p->lchild;
        }
        else{
            p = s.pop();
            p = p->rchild;
        }
    }
}
#include <iostream>
#include "classes.h"

using namespace std;

int main(int argc, char const *argv[])
{
    tree t;
    t.preorder();
    return 0;
}
Q_Node *t;
t = new Q_Node;
if(!t)
    cout<<"Queue is full"<<endl;
else ...
#include <new>
...

Q_Node *t;
t = new(std::nothrow) Q_Node;
if(!t)
    cout<<"Queue is full"<<endl;
else ...