简体   繁体   English

指向 C++ 中结构的指针

[英]Pointer to pointer for struct in C++

I am trying to solve the Problem Populate Inorder Successor for all nodes in a BST on GFG.我正在尝试解决 GFG 上BST 中所有节点的填充有序后继的问题。 Some Code is given (not able to paste here as it shows so much code in here)给出了一些代码(无法粘贴在这里,因为它在这里显示了很多代码)

and I need to code the populateNext(node* p) where p is the root node of tree.我需要对populateNext(node* p)进行编码,其中 p 是树的根节点。 I coded the function as below but it is not working and giving output as 3->-1 instead of 3->8 8->10 10->12 12->-1我将 function 编码如下,但它不起作用并将 output 设为3->-1而不是3->8 8->10 10->12 12->-1

void populateNext(struct node* p)
{
    static node* x=NULL;
    if(p==NULL) return;
    populateNext(p->right);
    p->next=x;
    x=p;\
    populateNext(p->left);
}

I think the x node created is never modified so the pointer in main function ptr in not moving forward.我认为创建的x节点永远不会被修改,因此主 function ptr中的指针不会向前移动。 Then I looked for more solutions and found a similar one with a double pointer.然后我寻找更多的解决方案,并找到了一个类似的带有双指针的解决方案。 I don't know why is mine one not working but the below one is working.我不知道为什么我的一个不工作,但下面的一个工作。

void find(struct node* root,struct node **nextr)
{
    if(root)
    {
        find(root->right,nextr);
        
        root->next = *nextr;
        
        *nextr = root;
        
        find(root->left,nextr);
    }
}
void populateNext(struct node* p)
{
   struct node* next = NULL;
   find(p,&next);
}

I know that pointer x is storing the address of struct variable and with every function call, I am assigning my pointer a new node pointer so it should work.我知道指针 x 正在存储结构变量的地址,并且每次 function 调用时,我都在为我的指针分配一个新的节点指针,因此它应该可以工作。 I tried declaring the node x as global too but still the same answer 3->-1 .我也尝试将节点x声明为全局但仍然是相同的答案3->-1

I tried searching about pointer to pointer for structs but everywhere only how to do it is given.我尝试搜索指向结构指针的指针,但到处都只给出了如何做到这一点。 I want to ask why code is not working for single pointer and why it worked for pointer to pointer and what is the use of pointer to pointers ie why we use is?我想问为什么代码不适用于单指针以及为什么它适用于指针指针以及指针指针的用途是什么,即我们为什么使用是? What are the limitations of single pointer?单指针的限制是什么?

Better name, than double pointer, is pointer to pointer .比双指针更好的名称是指向指针的指针 It means, that you are allocating 4 bytes (in 32-bit systems) to hold address of something.这意味着,您正在分配 4 个字节(在 32 位系统中)来保存某物的地址。 It is up to the programmer what it is.它是什么取决于程序员。 If you use pointer to pointer, you are holding address of other address, which may hold the other address of something (like this struct).如果您使用指向指针的指针,则您持有其他地址的地址,该地址可能持有某物的其他地址(如这个结构)。 Any combinations may be done.可以进行任何组合。

In the first attempt, each recursive call has it's own x variable - this is wrong.在第一次尝试中,每个递归调用都有它自己的x变量——这是错误的。 The algorithm need the common nextr variable, which is allocated only once, and used on every recursion level.该算法需要公共的nextr变量,它只分配一次,并在每个递归级别上使用。 This is why your program starts to work.这就是您的程序开始工作的原因。

The second attempt allocated next address on the stack, initialized with NULL .第二次尝试在堆栈上分配next地址,用NULL初始化。 This address was passed to the recursive find function ( &next ), which in turn know were it is, and write to it ( *nextr = root ).该地址被传递给递归find function ( &next ),后者又知道它是什么,并写入它 ( *nextr = root )。 This write made on the stack-allocated next variable from populateNext , like on the every find call.这个写入是在populateNext的堆栈分配的next变量上进行的,就像在每个 find 调用上一样。

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

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