rbtree.txt 中insert 操作,为何用2级指针??

2级指针用的还是不熟。心虚。

Inserting data in the tree involves first searching for the place to insert the
new node, then inserting the node and rebalancing ("recoloring") the tree. The search for insertion differs from the previous search by finding the
location of the pointer on which to graft the new node. The new node also
needs a link to its parent node for rebalancing purposes. Example: int my_insert(struct rb_root *root, struct mytype *data)
{
struct rb_node **new = &(root->rb_node), *parent = NULL; /* Figure out where to put new node */
while (*new) {
struct mytype *this = container_of(*new, struct mytype, node);
int result = strcmp(data->keystring, this->keystring); parent = *new;
if (result < )
new = &((*new)->rb_left);
else if (result > )
new = &((*new)->rb_right);
else
return FALSE;
} /* Add new node and rebalance tree. */
rb_link_node(&data->node, parent, new);
rb_insert_color(&data->node, root); return TRUE;
} Removing or replacing existing data in an rbtree
------------------------------------------------
找到位置,set value。
如果是一级指针,data->node 和 parent 肯定可以关联,但是parent(*rb_right, *rb_left) 和data->node 怎么关联,到底是左还是右,这里的逻辑有意思,直接记录你要替换的地址,等会直接
把你换掉,所以用2级指针。
若用一级指针p,能操作的只有p指向的内存块,无法改变p 存储的地址。 二级指针总结:
  1. 你想改变的是什么?内存块还是指针本身。
  2. 使用二级指针,基本操作是pp,就是一级指针整个换掉,小心内存泄漏(*pp, &*pp 这两块内存),其中一级指针多为判断条件。

最新文章

  1. 设置共享,实现Linux和Windows之间的共享
  2. Spring4.1新特性——Spring缓存框架增强(转)
  3. 数据库和3G的结合使用
  4. MySQL: Tree-Hierarchical query
  5. eclipse常用10个快捷键[转载]
  6. web.xml配置
  7. cocos2d-x实战 C++卷 学习笔记--第7章 动作、特效(一)
  8. 常用JDBC连接字符串
  9. VMware linux 增加根目录空间 (使用图形分区工具gparted LiveCd)
  10. ADN中国队参加微软Kinect他赢得了全国比赛三等奖,我们的创意项目与团队Kinect于Naviswork虚拟之旅
  11. Mac实用操作技巧(二)
  12. 面试题解:输入一个数A,找到大于A的一个最小数B,且B中不存在连续相等的两个数字
  13. Front-end Job Interview Questions
  14. HDFS笔记(二)
  15. java反编译工具cfr
  16. js keyup、keypress和keydown事件
  17. 【Java】 剑指offer(31) 栈的压入、弹出序列
  18. iOS:使用NSRegularExpression正则去掉一串字符串中所有的特殊字符和标点
  19. usb枚举
  20. CentOS 7.4 初次手记:第二章 CentOS安装步骤

热门文章

  1. javascript 富文本 注意事项
  2. [2] day 02
  3. BZOJ 1367 [Baltic2004]sequence (可并堆)
  4. MPlayer 开始支持RTSP/RTP流媒体文件
  5. Tensorflow原理通用
  6. jQuery(Dom节点操作)
  7. 机房工程-在线式、后备式UPS选择(转载)
  8. BA-楼宇自控系统设计论文(转载)潘翌庆 元晨
  9. struts自己定义拦截器--登录权限控制
  10. Docker推出了Docker云,给大家介绍下哈!