[英]Returning pointer from function and calling function with pointer recursively - BST
Recently I started to write BST implementation and I'm still struggling with pointers. 最近,我开始编写BST实现,但我仍在努力使用指针。 First of my problem is that I try to find 'predecessor' of given node with function, which given pointer of given node as a argument returns pointer to the predecessor of given node.
我的第一个问题是,我尝试使用函数查找给定节点的“前任”,该给定节点的给定指针作为参数返回指向给定节点的前任的指针。 I tried to think logically with calling a function and giving argument, but I'm stuck.
我试图通过调用函数和给出参数来进行逻辑思考,但是我陷入了困境。
First of all, I've declared 'root' node in 'main' like this: tree *root;
首先,我在'main'中声明了'root'节点,如下所示:
tree *root;
, then I call a function from which I call a function findPredecessor
like this: del(&root, k1);
,然后调用一个函数,从中调用函数
findPredecessor
像这样: del(&root, k1);
, here's a declaration of a del
function: void del(tree **root, int k)
then I use 'iterator' with which I go through whole BST tree declared like this: tree *w = NULL
. ,这是一个
del
函数的声明: void del(tree **root, int k)
然后我使用'iterator'来遍历整个BST树,声明如下: tree *w = NULL
。 Finally here's how I call findPredecessor
function (inside del
function): tree *predecessor = findPredecessor(w);
最后,这是我如何调用
findPredecessor
函数(在del
函数内部): tree *predecessor = findPredecessor(w);
and I get an error C2040 'FindPredecessor': 'tree *(tree *)' differs in levels of indirection from 'int ()'
, here's a body of a findPredecessor
function: 我得到一个错误
C2040 'FindPredecessor': 'tree *(tree *)' differs in levels of indirection from 'int ()'
,这是findPredecessor
函数的主体:
tree* findPredecessor(tree *w) {
w = w->left;
while (w->right != NULL) {
w = w->right;
}
return w;}
I'm also strugling with deallocating memory from whole tree, here's a body a function delAll: 我也在努力从整棵树中分配内存,这是一个函数delAll的主体:
void delAll(tree **root) {
if (*root == NULL) {
return;
}
delAll(&((*root)->left));
delAll(&((*root)->right));
free(*root);
}
,which I'm calling from 'main' same manner as I do with del
. ,我从'main'调用的方式与
del
。 I know my problem is some kind references/pointers related, I would be thankful for help. 我知道我的问题是与某种参考/指针相关的,感谢您的帮助。 Sorry for my english.
对不起我的英语不好。
I think you are calling findPredecessor() before declaring it. 我认为您在声明它之前正在调用findPredecessor()。 Just add a forward declaration.
只需添加一个前向声明。 Or you can rearrange your code such that findPredecessor() is implemented before del().
或者,您可以重新排列代码,以便在del()之前实现findPredecessor()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.