<legend id='wylh7'><style id='wylh7'><dir id='wylh7'><q id='wylh7'></q></dir></style></legend>
        <bdo id='wylh7'></bdo><ul id='wylh7'></ul>
      <tfoot id='wylh7'></tfoot>
      1. <small id='wylh7'></small><noframes id='wylh7'>

        <i id='wylh7'><tr id='wylh7'><dt id='wylh7'><q id='wylh7'><span id='wylh7'><b id='wylh7'><form id='wylh7'><ins id='wylh7'></ins><ul id='wylh7'></ul><sub id='wylh7'></sub></form><legend id='wylh7'></legend><bdo id='wylh7'><pre id='wylh7'><center id='wylh7'></center></pre></bdo></b><th id='wylh7'></th></span></q></dt></tr></i><div id='wylh7'><tfoot id='wylh7'></tfoot><dl id='wylh7'><fieldset id='wylh7'></fieldset></dl></div>
      2. C++ 按排序顺序添加到链表

        C++ Add to linked list in sorted order(C++ 按排序顺序添加到链表)
        <i id='MXFSM'><tr id='MXFSM'><dt id='MXFSM'><q id='MXFSM'><span id='MXFSM'><b id='MXFSM'><form id='MXFSM'><ins id='MXFSM'></ins><ul id='MXFSM'></ul><sub id='MXFSM'></sub></form><legend id='MXFSM'></legend><bdo id='MXFSM'><pre id='MXFSM'><center id='MXFSM'></center></pre></bdo></b><th id='MXFSM'></th></span></q></dt></tr></i><div id='MXFSM'><tfoot id='MXFSM'></tfoot><dl id='MXFSM'><fieldset id='MXFSM'></fieldset></dl></div>
      3. <small id='MXFSM'></small><noframes id='MXFSM'>

          • <tfoot id='MXFSM'></tfoot>
            <legend id='MXFSM'><style id='MXFSM'><dir id='MXFSM'><q id='MXFSM'></q></dir></style></legend>

                <tbody id='MXFSM'></tbody>

                <bdo id='MXFSM'></bdo><ul id='MXFSM'></ul>
                1. 本文介绍了C++ 按排序顺序添加到链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个使用结构体的链表.现在我让它在最后添加每个元素.但是我想根据 ID 按排序顺序添加每个元素.该结构体有两个元素:字符串名称和长 ID.

                  Hi I have a linked list using structs. Right now I got it to add every element at the end. However I'd like to add each element in sorted order based on the ID. The struct has two elements: string name, and long ID.

                  node* temp = new node;
                  temp->name = nameRead;
                  temp->id = idRead;
                  
                  //check if first item, if so add as head
                  if(head == NULL)
                  {
                      head = temp;
                  }
                  else
                  {
                     node* temp2 = head;
                     while(temp2->next != NULL)
                     {
                        temp2 = temp2->next;
                     }
                     temp2->next = temp;
                  }
                  

                  推荐答案

                  node* temp = new node;
                  temp->name = nameRead;
                  temp->id = idRead;
                  
                  node* temp2 = head;
                  node** temp3 = &head;
                  while(temp2 != null && temp2->id < temp->id)
                  {
                     temp3 = &temp2->next;
                     temp2 = temp2->next;
                  }
                  *temp3 = temp;
                  temp->next = temp2;
                  

                  解释:'temp3' 指针指向'temp' 需要去的地方.将 temp2 初始化为 head,并继续循环直到我们到达列表的末尾,或者直到 temp2 的 id >= 比 temp 的 id.在循环的每次迭代中,同时推进 temp3 和 temp2.

                  Explanation: The 'temp3' pointer points to where 'temp' would need to go. Initialize temp2 to head, and keep looping until we reach the end of the list, or until temp2's id is >= than temp's id. In each iteration of the loop, advance both temp3 and temp2.

                  在循环结束时,'temp3' 将保存 temp 应该在的指针的地址.因此,将 *temp3 分配给指向 temp,并将 temp->next 分配给 temp2(此时它要么为空,要么指向 id 大于 temp->id 的项).

                  At the end of the loop, 'temp3' will hold the address of the pointer where temp should be. So assign *temp3 to point to temp, and assign temp->next to point to temp2 (which at this point would either be null, or would point to the item that has larger id than temp->id).

                  这篇关于C++ 按排序顺序添加到链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                2. <tfoot id='Yl8pK'></tfoot>

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

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

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

                            <bdo id='Yl8pK'></bdo><ul id='Yl8pK'></ul>
                              <tbody id='Yl8pK'></tbody>