简体   繁体   English

初始化为NULL时c ++ Struct指针无法读取内存

[英]c++ Struct pointer unable to read memory when initialised as NULL

I'm creating a program that incorporates Binary search tree algorithm but I've run into a problem that I'm not sure how to solve. 我正在创建一个包含二进制搜索树算法的程序,但是遇到了一个不确定如何解决的问题。 Heres the relevant bits of my code. 这是我的代码的相关位。

struct node {
    string data;
    node *left = NULL;
    node *right = NULL;
};

Thats my struct for the node. 多数民众赞成在我的节点结构。

void Insert_Rec(string word, node* ptr) {

if (ptr->data == "") {

    ptr->data = word;
    ptr->left = NULL;
    ptr->right = NULL;
    cout << "overwitten!" << endl;
}
else if (word < ptr->data) {
    if (ptr->left != NULL) {
        cout << "Recursing left!";
        Insert_Rec(word, ptr->left);
    }
    else {
        ptr->data = word;
        ptr->left = NULL;
        ptr->right = NULL;
        cout << "Inserted!";
    }
}

And the problem lies there, the program never enters if(ptr->left != NULL) statement. 问题就在这里,程序永远不会输入if(ptr-> left!= NULL)语句。 Looking at my visual studio debugger, the ptr->left shows "" instead of NULL. 看着我的Visual Studio调试器,ptr-> left显示“”而不是NULL。 How can i solve this!? 我该如何解决! I've tried some of the other solutions here but they are either not relevant or just dont work!! 我在这里尝试了其他一些解决方案,但它们要么不相关,要么就不起作用!

program never enters if(ptr->left != NULL) statement 程序永远不会输入if(ptr-> left!= NULL)语句

well ptr->left starts out NULL, and you never assign anything else to it, so it will stay NULL forever. ptr->left开头为NULL,并且您从不分配其他任何内容,因此它将永远保持NULL。

if (ptr->left) {
    cout << "Recursing left!";
    Insert_Rec(word, ptr->left);
}
else {
    /* this just overwrites the existing node in-place
       but you should be creating a new node for the left child
    ptr->data = word;
    ptr->left = NULL;
    ptr->right = NULL;
    */
    ptr->left = new node{word, nullptr, nullptr};
    cout << "Inserted!";
}

There are a number of other issues with your code (Vlad-from-Moscow's answer shows a better design for this function, but you really need to fix it at the container class level), but this is the immediate blocker. 您的代码还有很多其他问题(Vlad-from-Moscow的答案显示了此函数的更好设计,但您确实需要在容器类级别对其进行修复),但这是立即阻止程序。

Your function implementation does not make sense in whole. 您的函数实现没有整体意义。

The function can be defined the following way 该功能可以通过以下方式定义

void Insert_Rec( node **head, const std::string &word ) 
{
    if ( *head == nullptr )
    {
        *head = new node { word, nullptr, nullptr };
    }
    else if ( word < ( *head )->data )
    {
        Insert_Rec( &( *head )->left, word );
    }
    else
    {
        Insert_Rec( &( *head )->right, word );
    }
}  

It is the function that should allocate a new node if it is required. 如果需要,此功能应分配一个新节点。

If you want that the BST would not contain duplicates then change the last else statement to else if statement like 如果您希望BST不包含重复项,则将最后一个else语句更改为else if语句,例如

else if ( ( *head )->data < word )

The function can be called the following way 该函数可以通过以下方式调用

node *head = nullptr;
//...
Insert_Rec( &head, "Hello" );

Also instead of the "double" pointer you can use a referenced type as the type of the first function parameter. 另外,您可以使用引用的类型作为第一个函数参数的类型,而不是“ double”指针。

For example 例如

void Insert_Rec( node * &head, const std::string &word ) 
{
    if ( head == nullptr )
    {
        head = new node { word, nullptr, nullptr };
    }
    else if ( word < head->data )
    {
        Insert_Rec( head->left, word );
    }
    else
    {
        Insert_Rec( head->right, word );
    }
} 

You've set node *left=NULL and node *right=NULL in struct. 您已经在结构中设置了节点* left = NULL和节点* right = NULL。 Remove that NULL. 删除该NULL。 Leave only two pointers. 只留下两个指针。 Otherwise, it's just null. 否则,它为空。 Anyway, keep in mind that very rarely you want to initialise the pointers in a struct. 无论如何,请记住,很少要初始化结构体中的指针。

struct node {
    // simple is better (-:
    string data;
    struct node *left;
    struct node *right;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM