C 修改函数内的结构指针
我正在用C编写一个二进制搜索树的实现,下面是“插入”过程的一部分:C 修改函数内的结构指针,c,pointers,struct,C,Pointers,Struct,我正在用C编写一个二进制搜索树的实现,下面是“插入”过程的一部分: void insert(void *key, struct bst *tree) { void n = NULL; struct bst **y = &n; struct bst **x = &(tree->root); struct bst *node = init(key); int cmp; while (*x != NULL) {
void insert(void *key, struct bst *tree) {
void n = NULL;
struct bst **y = &n;
struct bst **x = &(tree->root);
struct bst *node = init(key);
int cmp;
while (*x != NULL) {
y = x;
cmp = <compare node and x's key>
if (cmp > 0)
x = &((*x)->left);
else
x = &((*x)->right);
}
if (y == NULL) /* Empty Tree */
tree->root = node;
cmp = <compare node and y's key>
else if (cmp > 0)
(*y)->left = node;
else
(*y)->right = node;
void插入(void*键,结构bst*树){
void n=空;
结构bst**y=&n;
结构bst**x=&(树->根);
struct bst*node=init(键);
int-cmp;
而(*x!=NULL){
y=x;
cmp=
否则如果(cmp>0)
(*y)->左=节点;
其他的
(*y)->右=节点;
我使用双结构指针(x和y)来更改原始树。是否需要双指针,或者我是否可以使用指向树本身的简单指针来更改
编辑:在注释中得到了答案,我不需要指针。您所显示的代码中不需要双指针。@CharlieBurns那么,一个简单的结构bst*x,*y就足够了吗?是的,并相应地更改其余部分。