<legend id='h65gZ'><style id='h65gZ'><dir id='h65gZ'><q id='h65gZ'></q></dir></style></legend>

    <small id='h65gZ'></small><noframes id='h65gZ'>

  • <i id='h65gZ'><tr id='h65gZ'><dt id='h65gZ'><q id='h65gZ'><span id='h65gZ'><b id='h65gZ'><form id='h65gZ'><ins id='h65gZ'></ins><ul id='h65gZ'></ul><sub id='h65gZ'></sub></form><legend id='h65gZ'></legend><bdo id='h65gZ'><pre id='h65gZ'><center id='h65gZ'></center></pre></bdo></b><th id='h65gZ'></th></span></q></dt></tr></i><div id='h65gZ'><tfoot id='h65gZ'></tfoot><dl id='h65gZ'><fieldset id='h65gZ'></fieldset></dl></div>
  • <tfoot id='h65gZ'></tfoot>
      <bdo id='h65gZ'></bdo><ul id='h65gZ'></ul>

      1. 用于更简单地遍历链表的指针指向技术是什么?

        时间:2023-10-18

        <tfoot id='5v9ph'></tfoot><legend id='5v9ph'><style id='5v9ph'><dir id='5v9ph'><q id='5v9ph'></q></dir></style></legend>

          <tbody id='5v9ph'></tbody>
        • <i id='5v9ph'><tr id='5v9ph'><dt id='5v9ph'><q id='5v9ph'><span id='5v9ph'><b id='5v9ph'><form id='5v9ph'><ins id='5v9ph'></ins><ul id='5v9ph'></ul><sub id='5v9ph'></sub></form><legend id='5v9ph'></legend><bdo id='5v9ph'><pre id='5v9ph'><center id='5v9ph'></center></pre></bdo></b><th id='5v9ph'></th></span></q></dt></tr></i><div id='5v9ph'><tfoot id='5v9ph'></tfoot><dl id='5v9ph'><fieldset id='5v9ph'></fieldset></dl></div>

            • <bdo id='5v9ph'></bdo><ul id='5v9ph'></ul>
                1. <small id='5v9ph'></small><noframes id='5v9ph'>

                  本文介绍了用于更简单地遍历链表的指针指向技术是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  十年前,有人向我展示了一种遍历链表的技术:您使用了双指针(pointer-to-pointer)而不是单指针.

                  该技术无需检查某些边界/边缘情况,从而生成更小、更优雅的代码.

                  有人知道这个技术到底是什么吗?

                  解决方案

                  我认为你的意思是双指针,就像指向一个指针的指针"一样,这对于在单向链表的末尾插入非常有效

                  strong> 或树结构.这个想法是,一旦找到结尾(空指针),您就不需要特殊情况或尾随指针"来跟随您的遍历指针.因为您可以将指针取消引用到要插入的指针(它指向 到最后一个节点的下一个指针!).像这样:

                  T **p = &list_start;而 (*p) {p = &(*p)->next;}*p = 新 T;

                  而不是这样的:

                  T *p = list_start;如果(p == NULL){list_start = 新 T;} 别的 {而(p->下一个){p=p->下一个;}p->next = 新 T;}

                  注意:它对于为单向链表制作有效的删除代码也很有用.在任何时候执行 *p = (*p)->next 都会删除您正在查看"的节点(当然您仍然需要清理节点的存储).

                  Ten years ago, I was shown a technique for traversing a linked list: instead of using a single pointer, you used a double pointer (pointer-to-pointer).

                  The technique yielded smaller, more elegant code by eliminating the need to check for certain boundary/edge cases.

                  Does anyone know what this technique actually is?

                  解决方案

                  I think you mean double pointer as in "pointer to a pointer" which is very efficient for inserting at the end of a singly linked list or a tree structure. The idea is that you don't need a special case or a "trailing pointer" to follow your traversal pointer once you find the end (a NULL pointer). Since you can just dereference your pointer to a pointer (it points to the last node's next pointer!) to insert. Something like this:

                  T **p = &list_start;
                  while (*p) {
                     p = &(*p)->next;
                  }
                  *p = new T;
                  

                  instead of something like this:

                  T *p = list_start;
                  if (p == NULL) {
                      list_start = new T;
                  } else {
                      while (p->next) {
                          p = p->next;
                      }
                      p->next = new T;
                  }
                  

                  NOTE: It is also useful for making efficient removal code for a singly linked list. At any point doing *p = (*p)->next will remove the node you are "looking at" (of course you still need to clean up the node's storage).

                  这篇关于用于更简单地遍历链表的指针指向技术是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么链表使用指针而不是将节点存储在节点内 下一篇:为链表创建复制构造函数

                  相关文章

                  <i id='fJAqv'><tr id='fJAqv'><dt id='fJAqv'><q id='fJAqv'><span id='fJAqv'><b id='fJAqv'><form id='fJAqv'><ins id='fJAqv'></ins><ul id='fJAqv'></ul><sub id='fJAqv'></sub></form><legend id='fJAqv'></legend><bdo id='fJAqv'><pre id='fJAqv'><center id='fJAqv'></center></pre></bdo></b><th id='fJAqv'></th></span></q></dt></tr></i><div id='fJAqv'><tfoot id='fJAqv'></tfoot><dl id='fJAqv'><fieldset id='fJAqv'></fieldset></dl></div>
                2. <tfoot id='fJAqv'></tfoot>

                    1. <small id='fJAqv'></small><noframes id='fJAqv'>

                      • <bdo id='fJAqv'></bdo><ul id='fJAqv'></ul>
                      <legend id='fJAqv'><style id='fJAqv'><dir id='fJAqv'><q id='fJAqv'></q></dir></style></legend>