关于多重指针问题

qq75437828 2010-02-02 11:34:06
typedef struct TREE_NODE{
TREE_TYPE value;
struct TREE_NODE *left;
struct TREE_NODE *right;
}TreeNode;



TreeNode **link;

link = ¤t–>left;

*link = current–>left;

请问一下,这两句意义上有什么不同?

原题如下:
void
delete( TREE_TYPE value )
{
TreeNode *current;
TreeNode **link;
/*
** First, locate the value. It must exist in the tree or this routine
** will abort the program.
*/
link = &tree;
while( (current = *link) != NULL && value != current–>value ){
if( value < current–>value )
link = ¤t–>left;
else
link = ¤t–>right;
}
assert( current != NULL );
/*
** We’ve found the value. See how many children it has.
*/
if( current–>left == NULL && current–>right == NULL ){
/*
** It is a leaf; no children to worry about!
*/
*link = NULL;
free( current );
}
else if( current–>left == NULL || current–>right == NULL ){
/*
** The node has only one child, so the parent will simply
** inherit it.
*/
if( current–>left != NULL )
*link = current–>left;
else
*link = current–>right;
free( current );
}
else {
/*
** The node has two children! Replace its value with the
** largest value from its left subtree, and then delete that
** node instead.
*/
TreeNode *this_child;
TreeNode *next_child;
this_child = current–>left;
next_child = this_child–>right;
while( next_child != NULL ){
this_child = next_child;
next_child = this_child–>right;
}
value = this_child–>value;
delete( value );
current–>value = value;
}
}
...全文
212 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
某某9 2010-02-02
  • 打赏
  • 举报
回复
link = ¤t–>left;
这个是返回link是TreeNode **类型的,link的值变了,变为current–>left的地址
. 注意->的优先级高于&
*link = current–>left
这个是返回*link是TreeNode *类型的。link的值没变,他指向的指针的值变了,和current–>left指向的地址一样

nobody@noone 2010-02-02
  • 打赏
  • 举报
回复
link = ¤t–>left;

*link = current–>left;
请问一下,这两句意义上有什么不同?

link是二级指针 设放的是 一级指针P的地址
一级指针P放的是变量X的地址
变量X放数据

link = xxx 改变的是link的值,也就是将 link指向另一个P
*link = xxx 改变的是P的值 也就是将P指向另一个X

回到
link = ¤t–>left;
link的值变了,变为current–>left的地址
*link = current–>left;
link的值没变,他指向的指针的值变了,和current–>left指向的地址一样
qq75437828 2010-02-02
  • 打赏
  • 举报
回复
可以详细解释下在原题中有什么不同的作用吗?谢谢
xylicon 2010-02-02
  • 打赏
  • 举报
回复
link = ¤t–>left;
这个是返回link是TreeNode **类型的
*link = current–>left
这个是返回*link是TreeNode *类型的。
james_hw 2010-02-02
  • 打赏
  • 举报
回复
link = ¤t–>left;

*link = current–>left;
请问一下,这两句意义上有什么不同?

只针对这两句。

第一句,link 的值改变了,而第二句link的值没有改变,但link保存的值改变了
honghu069 2010-02-02
  • 打赏
  • 举报
回复
把二级指针看成一级指针就好懂了
crst_zh 2010-02-02
  • 打赏
  • 举报
回复
一个给指针赋值
一个给元素赋值(此处“元素”也是一个指针)。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧