簡體   English   中英

我正在嘗試使用 cpp 中的鏈表實現二叉樹,但我的代碼不起作用

[英]I'm trying to implement Binary tree using linked list in cpp, But my code is not working

我想使用鏈表實現二叉樹,為此我也使用鏈表使用堆棧和隊列

堆棧和隊列我也使用過鏈表

在我的代碼中沒有語法錯誤,它編譯成功但運行代碼的時間它說分段錯誤

請有人找到我的錯誤並修復它..

在下面的代碼中,我將 int 分為三個單獨的文件:- 第一個文件是 class 文件(用於定義所有類,即堆棧、隊列、樹)

第二個文件是 function 文件,其中存在所有函數體,在每個 class 中聲明。

第三個文件是主文件。

第一個文件(class.h)

#ifndef CLASS
#define CLASS

#include <iostream>

using namespace std;

                                    //tree class (start)
class Node
{
public:
    Node *lchild;
    int data;
    Node *rchild;
};

class tree
{
private:
    Node *root = NULL;
public:
    tree();
    void preorder();
};                  
                                    //tree class (end)...
                                    //queue class (start)

class Q_Node
{
public:
    Node *data;
    Q_Node *next;
};

class Queue
{
private:
    Q_Node *first, *reare;
public:
    Queue(){first = reare = new Q_Node;}
    void enqueue(Node *x);
    Node * dequeue();
    void display();
    int isEmpty();
};
                                    //tree class (end)...
                                    //stack class (start)

class S_Node
{
public:
    Node *data;
    S_Node *next;
};

class Stack
{
private:
    S_Node *top;
public:
    Stack(){top = NULL;}
    void push(Node *x);
    Node * pop();
    int isEmpty();
    
};
                                    //stack class (end)...
#endif

第二個文件(functions.cpp)

#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;
        }
    }
}

第三個文件(main.cpp)

#include <iostream>
#include "classes.h"

using namespace std;

int main(int argc, char const *argv[])
{
    tree t;
    t.preorder();
    return 0;
}

對於您的Queue class,您似乎無法決定, first是持有第一個元素的節點還是持有第一個元素的節點的前任。

Queue::Queue first初始化一個data未初始化的節點。 Queue::enqueue然后在first之后添加一個新節點, dequeue返回first中的data指針,該指針仍未初始化,因此指向任意地址。 訪問此地址很有可能產生訪問沖突錯誤,這就是您的情況。


順便說一句:請注意

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

不會產生預期的結果。 如果無法分配更多 memory 則拋出std::bad_alloc而不是new返回nullptr 您需要使用nothrow變體才能使其工作:

#include <new>
...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM