免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: aomw10
打印 上一主题 下一主题

链表中使用二重指针的优点有什么 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-08-31 10:57 |只看该作者
本帖最后由 yanwb0613 于 2011-08-31 10:58 编辑

回复 1# aomw10


   刚看到这个问题,跟lz一样对这个问题有疑问

   个人理解,只是为了省一次"."操作:

   如果使用* prev, 那么插入时,  this->prev.next = this;
   如果使用** prev, 那么则为,   this->prev = this。

   说白了,插入,删除时,我们只需要修改前一节点的next指针,而不会修改它的prev和value
   那么prev直接指向前一节点的next元素即可,不需要指向结点本身,再"."到它的next元素

\\

论坛徽章:
0
12 [报告]
发表于 2011-08-31 12:00 |只看该作者
如9楼说的,因为要修改的是一级指针本身,所以需要二级指针,而不是省不省的问题。

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
13 [报告]
发表于 2011-09-01 17:13 |只看该作者
本帖最后由 captivated 于 2011-09-01 17:19 编辑

回复 1# aomw10

            这是为了方便链表遍历。

            一般的链表是这样子滴:
                               header
                                     |
                                  ________                  ________              ________
                                 | next  |     ---->      | next |     ---->   | next |  ----> NULL
              NULL <---- | prev  |     <----      | prev |     <----   | prev |
                                 -----------                   ------------             ------------
             如果我要遍历链表,只能从header开始遍历。


             LZ的结构,链表是酱紫的:
                                  ________                     ________                ________
                                 | next  |     -------->     | next |     ------>  | next |  ----> NULL
              NULL <---- | prev  |                       | prev |                 | prev |
                                 -----------                       ------------              ------------
                                           \                          /         \                      /   
                                               ___________                 ___________
                                               | header1 |                 | header2 |
            这样,任意节点的prev都指向header,我只要能取到一个节点(的内容),就可以通过这个节点的prev(而不需要知道此节点的地址),从此节点开始来前/后遍历链表。

            不好意思啊,手边没有做图工具,将就看吧...

            PS,个人浅见,如果弄错了欢迎拍砖。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
14 [报告]
发表于 2011-09-01 17:26 |只看该作者
看有些链表中定义的item,并非使用 *next, *prev,

而是向前指针使用二重指针

类似如下这种:
struct ...
aomw10 发表于 2011-06-21 20:24


这是 Minix 3 内核代码 kernel/proc.c 中给出的解释:

  1. * The code here is critical to make everything work and is important for the
  2. * overall performance of the system. A large fraction of the code deals with
  3. * list manipulation. To make this both easy to understand and fast to execute
  4. * pointer pointers are used throughout the code. Pointer pointers prevent
  5. * exceptions for the head or tail of a linked list.
  6. *
  7. *  node_t *queue, *new_node;        // assume these as global variables
  8. *  node_t **xpp = &queue;         // get pointer pointer to head of queue
  9. *  while (*xpp != NULL)         // find last pointer of the linked list
  10. *      xpp = &(*xpp)->next;        // get pointer to next pointer
  11. *  *xpp = new_node;                // now replace the end (the NULL pointer)
  12. *  new_node->next = NULL;        // and mark the new end of the list
  13. *
  14. * For example, when adding a new node to the end of the list, one normally
  15. * makes an exception for an empty list and looks up the end of the list for
  16. * nonempty lists. As shown above, this is not required with pointer pointers.
  17. */
复制代码

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
15 [报告]
发表于 2011-09-01 17:40 |只看该作者
回复 2# cobras


    是这样用的么...

    我还以为是
    father -> next = this;
    this -> prev = &father;

    为什么
    father->next = this;
    this->prev = &father->next; // 这样不是prev指向this自己了么?

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
16 [报告]
发表于 2011-09-01 17:45 |只看该作者
回复 14# MMMIX


    就是说,方便在链表的最后插入节点。
    通常,在链表尾插入节点需要遍历整个链表。但是这个不用。

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
17 [报告]
发表于 2011-09-01 17:49 |只看该作者
回复 14# MMMIX


    擦... 不是... 他给出的示例代码还是遍历了整个链表。
    他的意思是说,不用处理链表是不是空链表的情况,同样的代码都能用。

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
18 [报告]
发表于 2011-09-01 17:54 |只看该作者
回复 14# MMMIX


    他还说,不管你从前面插入,还是从后面插入,感觉是一样的{:3_183:}

    其实,童鞋们觉得... 从前面插入与从后面插入,感觉一样吗?{:3_189:}

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
19 [报告]
发表于 2011-09-01 17:56 |只看该作者
回复 2# cobras


    哦 又看了下 是修改节点指针,你那样用也有...

    尼玛,纠结。

论坛徽章:
0
20 [报告]
发表于 2012-11-15 16:05 |只看该作者
谢谢9L,学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP