简体   繁体   English

通过 c 中的 function 中的指针传递参数值

[英]Pass argument value by pointer in function in c

I am a newbie in learning c..... So I am trying to create a tree structure from inputs like:我是学习 c 的新手.....所以我试图从以下输入创建一个树结构:

(2, 50) (4, 30) (9, 30) (10, 400) (-5, -40)
(7, 20) (19, 200) (20, 50) (-18, -200) (-2, 29)
(2, 67) (4, 35) (9, 45) (-18, 100) 

Firstly, I have defined some data structures and auxiliary function as below:首先,我定义了一些数据结构和辅助 function 如下:

typedef struct AVLTreeNode
{
    int key;                    //key of this item
    int value;                  //value (int) of this item
    int height;                 //height of the subtree rooted at this node
    struct AVLTreeNode *parent; //pointer to parent
    struct AVLTreeNode *left;   //pointer to left child
    struct AVLTreeNode *right;  //pointer to right child
} AVLTreeNode;

//data type for AVL trees
typedef struct AVLTree
{
    int size;          // count of items in avl tree
    AVLTreeNode *root; // root
} AVLTree;

// create a new AVLTreeNode
AVLTreeNode *newAVLTreeNode(int k, int v)
{
    AVLTreeNode *newNode;
    newNode = malloc(sizeof(AVLTreeNode));
    assert(newNode != NULL);
    newNode->key = k;
    newNode->value = v;
    newNode->height = 0;  // height of this new node is set to 0
    newNode->left = NULL; // this node has no child
    newNode->right = NULL;
    newNode->parent = NULL; // no parent
    return newNode;
}

// create a new empty avl tree
AVLTree *newAVLTree()
{
    AVLTree *T;
    T = malloc(sizeof(AVLTree));
    assert(T != NULL);
    T->size = 0;
    T->root = NULL;
    return T;
}

Then I wrote a function to insert each pair of data into the tree:然后我写了一个 function 将每对数据插入到树中:

int InsertNode(AVLTree *T, int k, int v)
{
    AVLTreeNode *currentNode = T->root;
    AVLTreeNode *parent;
    //if the tree/subtree is empty
    if (currentNode == NULL)
    {
        currentNode = newAVLTreeNode(k, v);
    }
    //keys are equal
    else
    {
        while (currentNode != NULL)
        {
            parent = currentNode;
            if (k == currentNode->key)
            {
                if (v == currentNode->value)
                {
                    return 0;
                }
                else if (v < currentNode->value)
                {
                    currentNode = currentNode->left;
                }
                else if (v > currentNode->value)
                {
                    currentNode = currentNode->right;
                }
            }
            else if (k < currentNode->key)
            {
                currentNode = currentNode->left;
            }
            else if (k > currentNode->key)
            {
                currentNode = currentNode->right;
            }
        }

        currentNode = newAVLTreeNode(k, v);
        currentNode->parent = parent;
    }
    T->size++;
    return 1;
}

I was expecting the values k and v in the function arguments would pass onto T->root->key, and T->root->value respectively, but it doesn't work..... Could someone help me to correct the code and explain to me the reason why my code is wrong please?我期待 function arguments 中的值 k 和 v 将分别传递给 T->root->key 和 T->root->value,但它不起作用.....有人可以帮我纠正代码并向我解释我的代码错误的原因?

The main problem is that your InsertNode() function simply doesn't.主要问题是您的InsertNode() function 根本没有。 It does construct an AVLTreeNode , traverse the tree to the new node's insertion point, and set its parent pointer, but it never updates a child pointer of the parent node, nor the root pointer of the tree in the case that it is initially empty.它确实构造了一个AVLTreeNode ,将树遍历到新节点的插入点,并设置其父指针,但它永远不会更新父节点的子指针,也不会更新树的根指针(如果它最初为空)。 In particular, assigning to currentNode does not have this effect.特别是,分配给currentNode没有这种效果。

However, you could make this code work by adjusting it for currentNode to be a double pointer ( AVLTreeNode ** ), so that you can assign indirectly through it to the wanted root or child pointer.但是,您可以通过将currentNode调整为双指针 ( AVLTreeNode ** ) 来使此代码正常工作,以便您可以通过它间接分配给想要的根或子指针。

AVLTreeNode **currentNode = &T->root;

// ...

currentNode = &(*currentNode)->left;

// ...

// So that you can ultimately do this:
*currentNode = newAVLTreeNode(k, v);

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

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