使指针中的值无效 [英] Nullifying a value from a pointer

查看:89
本文介绍了使指针中的值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很久没有做指针算术了,所以我想我会尝试使用C语言并做一个简单的Binary Search Tree.但是,我无法删除.这些路线符合我的预期:

I haven't done pointer arithmetic in a long time, so I figured I'd try my hand at C and do a simple Binary Search Tree. I can't get the hang of deletion, however. Something along these lines works as I expect:

typedef struct Node{
    int value;
    struct Node *left;
    struct Node *right;
}Node;

typedef struct Tree{
    struct Node* root;
}Tree;

int main(){
    Tree *tree = createTree(); 

    treeInsert(10, tree);    // Inserts 10 at root
    treeInsert(30, tree);    // Inserts 30 to root->right
    treeInsert(5, tree);     // Inserts 5 to root->left
    treeInsert(7, tree);     // Inserts 7 to root->left->right
    treeInsert(12, tree);    // Inserts 12 to root->right->left

    // Removes Node "7" from the tree successfully
    free(tree->root->left->right);   // Free memory for this node in the tree
    tree->root->left->right = NULL;  // Set the pointer to NULL

    return 0;
}

我想编写一个nodeDelete(Node *killNode)函数以释放与节点关联的内存,然后将其指向NULL,但是我发现它无法正常运行.

I'd like to write a nodeDelete(Node *killNode) function to free the memory associated with a node, then point it to NULL, but I find it doesn't work like I expect it to.

int main(){
   // ... snip ...

   Node *kill = tree->root->left->right  // Points kill node to Node "7"
   free(kill);                           // Deallocates memory
   kill = NULL;                          // Points kill to NULL, but keeps 
                                         //   tree->root->left->right **undefined** 
   // ... snip ...
}

我认为我的问题是我告诉它kill现在指向NULL,这将它与树中的节点断开连接,并且不影响原始节点指针.我该如何告诉我要将tree->root->left->right指向NULL而不是kill?在这种情况下,我需要一个指向指针的指针吗?

I think my problem is that I'm telling it that kill now points to NULL, which disconnects it from the node in the tree and doesn't effect the original node pointer. How can I tell it that I want to point tree->root->left->right to NULL instead of kill? Do I need a pointer-to-a-pointer in this case?

推荐答案

是的,如果要删除该节点,则需要将tree->root->left->right设置为NULL.这意味着您不能只将该指针的 value 传递给删除函数.

Yes, if you want to delete that node you need to set tree->root->left->right to NULL. This means that you can't just pass the value of that pointer to the deletion function.

您有两个选择:您可以将指针传递到要删除的节点的 parent ,以及有关要删除哪个子级的信息:

You have two options: you can either pass a pointer to the parent of the node to be deleted, along with information about which child to delete:

nodeDelete(Node *parent, int kill_right)
{
    Node *kill;

    if (kill_right) {
        kill = parent->right;
        parent->right = NULL;
    } else {
        kill = parent->left;
        parent->left = NULL;
    }

    free(kill);
}

在这种情况下,您将呼叫nodeDelete(tree->root->left, 1);.

In this case you'd call nodeDelete(tree->root->left, 1);.

或者,您可以将指针传递到要删除的指针:

Alternatively you can pass a pointer to the pointer that you want to remove:

nodeDelete(Node **killptr)
{
    free(*killptr);
    *killptr = NULL;
}

在这种情况下,您将呼叫nodeDelete(&tree->root->left->right);.

In this case you'd call nodeDelete(&tree->root->left->right);.

这篇关于使指针中的值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆