簡體   English   中英

C 結構體指針和引用指針

[英]C Pointer of Pointer of struct and reference

我正在嘗試在 C 中實現 BST。 這是代碼:

int main(int argc, char* argv[]) {
    int_bst_node_t * tree_p = NULL;
    test_insert(&tree_p, 40);
}

static void test_insert(int_bst_node_t ** t_p, int n) {
    printf("inserting %d into tree ", n);
    printf("\n");
    if (int_bst_insert(t_p, n)) {
        printf("result ");
        print_tree_elements(*t_p);
        printf("\n");
    }
    else {
       printf("insert failed\n");
    }
}

其他文件中的代碼:

// Create a node, assign values
bool createNode(int n, int_bst_node_t *node) {
    int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
    if( localNode == NULL )
    {
        puts("\n Unable to allocate memory");
        return false;
    }
    localNode->data = n;
    localNode->left = NULL;
    localNode->right = NULL;

    node = localNode;
    //////    Prints right values 
    printf("\n LocalNode Data: %d  Node Data: %d", localNode->data, node->data);
    free(localNode);
    return true;
}

/* 
 * insert n into *t_p
 * does nothing if n is already in the tree
 * returns true if insertion succeeded (including the case where n is already
 * in the tree), false otherwise (e.g., malloc error)
*/
bool int_bst_insert(int_bst_node_t ** t_p, int n) {
    if (*t_p == NULL) {
        printf("*t_p %d IS null", *t_p);
        int_bst_node_t node;
        // Pass node as a ref, so data is updated
        if (createNode(n, &node) == false)
            return false;

        // Prints invalid data
        printf("\n Data of new node : %d", node.data);
        *t_p = &node;

        /*
          Need  to assign node to *t_p, so it is updated, and prints the values
          when calling next function in test_insert()

        int_bst_node_t *t = *t_p;
        printf("\n Data of *tp node : %d", t->data);  
        */     
     } else 
        printf("*t_p %d is NOT null", *t_p);

    printf("\n"); 
    return true;
 }

我無法將節點的值/引用設置為 t_p。 這僅適用於根節點; 此外,它將是更多的節點。 我無法獲得 ** 的概念來更新值。 我嘗試了變化,都失敗了。

如果有人可以幫助我,我會很高興。

謝謝你。

function createNode至少沒有意義,因為它按值接受指向節點的指針。 在為節點分配 memory 后,您立即釋放它,使指向分配的 memory 的指針無效。

int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
//...
node = localNode;
//...
free(localNode);

function int_bst_insert也沒有意義。 除了這樣的錯誤

*t_p = &node;

它應該插入一個相對於二叉搜索樹中已存在節點的值的節點。

注意該函數中不得有任何消息 output。

可以通過以下方式定義函數。

int_bst_node_t * createNode( int n ) 
{
    int_bst_node_t *localNode = malloc(sizeof(int_bst_node_t));

    if( localNode != NULL )
    {
        localNode->data = n;
        localNode->left = NULL;
        localNode->right = NULL;
    }

    return localNode;
}

bool int_bst_insert( int_bst_node_t ** t_p, int n ) 
{
    while ( *t_p != NULL )
    {
        if ( n < ( *t_p )->data )
        {
            t_p = &( *t_p )->left;
        }
        else
        {
            t_p = &( *t_p )->right;
        }
    }

    *t_p = createNode( n );  

    return *t_p != NULL;
}

暫無
暫無

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

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