<bdo id='oyYpw'></bdo><ul id='oyYpw'></ul>
  • <small id='oyYpw'></small><noframes id='oyYpw'>

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

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

        从给定的 LinkedList 在 C++ 中创建一个反向 LinkedList

        Create a reverse LinkedList in C++ from a given LinkedList(从给定的 LinkedList 在 C++ 中创建一个反向 LinkedList)

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

                    <tbody id='BaYwz'></tbody>
                • <tfoot id='BaYwz'></tfoot>
                • <small id='BaYwz'></small><noframes id='BaYwz'>

                  本文介绍了从给定的 LinkedList 在 C++ 中创建一个反向 LinkedList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在从给定的链表以相反的顺序创建链表时遇到了一些问题.

                  I'm having some trouble create a linkedlist in reverse order from a given linkedlist.

                  我有 Java 背景,刚开始做一些 C++.

                  I come from a java background, and just started doing some C++.

                  你能看看我的代码,看看有什么问题吗?我猜我只是在操纵指针而不是创建任何新东西.

                  Can you check out my code and see what's wrong? I'm guessing I'm just manipulating pointer and not creating anything new.

                  //this is a method of linkedlist class, it creates a reverse linkedlist
                  //and prints it
                  
                  void LinkedList::reversedLinkedList()
                  {
                      Node* revHead;
                  
                      //check if the regular list is empty
                      if(head == NULL)
                         return;
                  
                      //else start reversing
                      Node* current = head;
                      while(current != NULL)
                      {
                          //check if it's the first one being added
                          if(revHead == NULL)
                             revHead = current;
                  
                          else
                          {
                              //just insert at the beginning
                              Node* tempHead = revHead;
                              current->next = tempHead;
                              revHead = current;
                          }
                          current = current->next;
                  
                       }//end while
                  
                       //now print it
                       cout << "Reversed LinkedList: " << endl;
                  
                       Node* temp = revHead;
                       while(temp != NULL)
                       {
                           cout << temp->firstName << endl;
                           cout << temp->lastName << endl;
                           cout << endl;
                  
                           temp = temp->next;
                        }
                  
                  }//end method
                  

                  推荐答案

                  更简单的一个:遍历你的链表,保存上一个和下一个节点,让当前节点指向上一个:

                  Easier one: Go through your linked list, save the previous and the next node and just let the current node point at the previous one:

                  void LinkedList::reversedLinkedList()
                  {
                      if(head == NULL) return;
                  
                      Node *prev = NULL, *current = NULL, *next = NULL;
                      current = head;
                      while(current != NULL){
                          next = current->next;
                          current->next = prev;
                          prev = current;
                          current = next;
                      }
                      // now let the head point at the last node (prev)
                      head = prev;
                  }
                  

                  这篇关于从给定的 LinkedList 在 C++ 中创建一个反向 LinkedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 作为模板参数时出错)
                • <i id='DFpKQ'><tr id='DFpKQ'><dt id='DFpKQ'><q id='DFpKQ'><span id='DFpKQ'><b id='DFpKQ'><form id='DFpKQ'><ins id='DFpKQ'></ins><ul id='DFpKQ'></ul><sub id='DFpKQ'></sub></form><legend id='DFpKQ'></legend><bdo id='DFpKQ'><pre id='DFpKQ'><center id='DFpKQ'></center></pre></bdo></b><th id='DFpKQ'></th></span></q></dt></tr></i><div id='DFpKQ'><tfoot id='DFpKQ'></tfoot><dl id='DFpKQ'><fieldset id='DFpKQ'></fieldset></dl></div>

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

                  • <small id='DFpKQ'></small><noframes id='DFpKQ'>

                    <tfoot id='DFpKQ'></tfoot>

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