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

    <bdo id='JZjHh'></bdo><ul id='JZjHh'></ul>

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

    1. <tfoot id='JZjHh'></tfoot>

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

      1. 链表,在末尾插入 C++

        Linked List, insert at the end C++(链表,在末尾插入 C++)

        • <bdo id='AhSp2'></bdo><ul id='AhSp2'></ul>

            • <tfoot id='AhSp2'></tfoot>
                <tbody id='AhSp2'></tbody>

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

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

                1. <legend id='AhSp2'><style id='AhSp2'><dir id='AhSp2'><q id='AhSp2'></q></dir></style></legend>
                  本文介绍了链表,在末尾插入 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个简单的函数来插入到 C++ 链表的末尾,但最后它只显示了第一个数据.我无法弄清楚出了什么问题.这是功能:

                  I was writing a simple function to insert at the end of a linked list on C++, but finally it only shows the first data. I can't figure what's wrong. This is the function:

                  void InsertAtEnd (node* &firstNode, string name){
                  
                          node* temp=firstNode;
                  
                          while(temp!=NULL) temp=temp->next;
                  
                              temp = new node;
                          temp->data=name;
                          temp->next=NULL;
                  
                          if(firstNode==NULL) firstNode=temp;
                  
                  }
                  

                  推荐答案

                  你写的是:

                  • 如果 firstNode 为空,它被替换为单个节点 temp没有下一个节点(没有人的 nexttemp)

                  • if firstNode is null, it's replaced with the single node temp which has no next node (and nobody's next is temp)

                  否则,如果 firstNode 不为 null,则什么都不会发生,除了 temp节点被分配和泄漏.

                  Else, if firstNode is not null, nothing happens, except that the temp node is allocated and leaked.

                  下面是更正确的代码:

                  void insertAtEnd(node* &first, string name) {
                      // create node
                      node* temp = new node;
                      temp->data = name;
                      temp->next = NULL;
                  
                      if(!first) { // empty list becomes the new node
                          first = temp;
                          return;
                      } else { // find last and link the new node
                          node* last = first;
                          while(last->next) last=last->next;
                          last->next = temp;
                      }
                  }
                  

                  另外,我建议向 node 添加一个构造函数:

                  Also, I would suggest adding a constructor to node:

                  struct node {
                      std::string data;
                      node* next;
                      node(const std::string & val, node* n = 0) : data(val), next(n) {}
                      node(node* n = 0) : next(n) {}
                  };
                  

                  它使您能够像这样创建 temp 节点:

                  Which enables you to create the temp node like this:

                  node* temp = new node(name);
                  

                  这篇关于链表,在末尾插入 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 作为模板参数时出错)

                    <small id='1AZle'></small><noframes id='1AZle'>

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

                        • <tfoot id='1AZle'></tfoot>
                        • <legend id='1AZle'><style id='1AZle'><dir id='1AZle'><q id='1AZle'></q></dir></style></legend>
                            <tbody id='1AZle'></tbody>