<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++ 按排序顺序添加到链表

        时间:2023-10-18
        <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++ 按排序顺序添加到链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:反转链表? 下一篇:你如何插入一个排序的链表?

                  相关文章

                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>