c++ - NULL 指针问题?

标签 c++ null pointers

void Insert(AVLnode * & root, string X)
{
    if ( root == NULL)
    {
        root = GetNode(X);
    }
    else if ( root->info > X )
    {
        Insert(root->left,X);
        if ( height(root->left) - height(root->right) == 2 )
            if ( X < root->left->info )
                RotateRR(root);
            else
                RotateLR(root);
    }
    else if ( root->info < X )
    {
        Insert(root->right,X);
        if ( height(root->right) - height(root->left) == 2 )
            if ( X > root->right->info )
                RotateRR(root);
            else
                RotateLR(root);
    }
    root->height = max( height(root->left), height(root->right) )+1;
}
AVLnode* find(AVLnode* root,string X)
{
    if (!root) return 0;
    if ( root->info == X )
        return root;
    else if (root->info < X)
        find(root->left,X);
    else if (root->info > X)
        find(root->right,X);
    return 0;   
}
int main(int argc,char* argv)
{
    AVLnode* Dic;
    Insert(Dic,"adf");
    return 0;
}

第一次在 Insert 中,root 为 NULL,但当我调试时,它会跳过 root == null。怎么回事?

最佳答案

问题出在 main()AVLnode* Dic; 语句中。您正在从 main()insert() 发送一个未初始化的指针。它包含垃圾值。将其初始化为 NULL

关于c++ - NULL 指针问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1781182/

相关文章:

c# - C# 非空时的方法调用

mysql - 请确认我使用的主键和唯一索引

c++ - 错误 : request for member ‘end’ in . ... 属于非类类型 ‘double’

C: 两个不同的数组指向同一个结构

c++ - cout << setw 没有与 åäö 正确对齐

c++ - 如何将在 Linux 中从命令行编译的 C 程序导出到 IDE?

c++ - 在 C++ 中使用多个 if else 语句

list - SML-NJ 列表等于 Nil 与 Null 列表

c++ - 特定的 c++ 指针/引用错误?

c++ - 未定义 vector<bool> 元素的运算符 &=?