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

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

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

      1. 链表中的搜索函数 - C++

        Search Function in Linked List - C++(链表中的搜索函数 - C++)

        <small id='69WRB'></small><noframes id='69WRB'>

            <tbody id='69WRB'></tbody>

          • <tfoot id='69WRB'></tfoot>

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

              1. <i id='69WRB'><tr id='69WRB'><dt id='69WRB'><q id='69WRB'><span id='69WRB'><b id='69WRB'><form id='69WRB'><ins id='69WRB'></ins><ul id='69WRB'></ul><sub id='69WRB'></sub></form><legend id='69WRB'></legend><bdo id='69WRB'><pre id='69WRB'><center id='69WRB'></center></pre></bdo></b><th id='69WRB'></th></span></q></dt></tr></i><div id='69WRB'><tfoot id='69WRB'></tfoot><dl id='69WRB'><fieldset id='69WRB'></fieldset></dl></div>
                1. 本文介绍了链表中的搜索函数 - C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的目标是生成一个函数来搜索列表中已有的数字并打印它已找到.
                  我最初的想法是按照我的删除功能搜索列表,直到找到一个数字(然后删除).
                  这似乎是对搜索功能进行编码的合乎逻辑的方式.如果这不正确,我将如何修改它以搜索我的列表并显示已找到一个号码?
                  我有节点 *head、*current 和 *temp 以及节点指针 nextnumber 作为 .h 文件上类中的数据类型.
                  谢谢.
                  注意 - 我在 search() 函数下使用了我的 remove() 函数.

                  My aim is to produce a function that searches for a number already in the list and print that it has been found.
                  My initial idea was to follow my remove function which searches through the list until it finds a number (to then delete).
                  This seemed the logical way to code the search function. If this isn't correct how would I modify it to search through my list and display that a number has been found?
                  I have nodes *head, *current and *temp as well as node pointer next and number as the data type in a class on a .h file.
                  Thank you.
                  NOTE - I used my remove() function under the search() function.

                  #include <iostream>                                                 
                  #include <string>                                                   
                  #include <fstream>                                                  
                  #include "LinkedList.h"
                  
                  using namespace SDI;
                  
                  int main()
                  {
                      LinkedList menu;
                  
                      menu.insert(5);                     
                      menu.insert(4);
                      menu.insert(2);
                      menu.insert(3);
                      menu.insert(8);
                      menu.remove(4);
                      menu.reverse();
                      menu.display();
                      menu.search(2);
                      system("pause");
                  
                  };
                  
                  
                  LinkedList::LinkedList()            
                  {
                      head = NULL;
                      current = NULL;
                      temp = NULL;
                  };
                  
                  
                  LinkedList::~LinkedList()           
                  {
                  
                  };
                  
                  
                  void LinkedList::insert(int add)                                    //insert function, data is stored in add from function body
                  {
                      Node* newnode = new Node;                                       //definition of add node, make new node and make node* point to it
                      newnode->next = NULL;                                           //point and set up to last node in the list (nothing)
                      newnode->number = add;                                          //adds data to list
                  
                      if (head != NULL)                                               //if head is pointing to object then we have list
                      {
                          current = head;                                             //make current pointer point to head
                          while (current->next != NULL)                               //check to see if end at list, is it the last node?
                          {
                              current = current->next;                                //advances current pointer to end of list
                          }
                          current->next = newnode;                                    //adds new node next to value already stored
                      }
                      else
                      {
                          head = newnode;                                             //if we don't have element in list
                      }
                  };
                  
                  
                  void LinkedList::remove(int remove)                                 //remove function, data is stored in remove from function body
                  {
                      Node* remove1 = NULL;                                           //searches through for same value in remove and deletes
                      temp = head;
                      current = head;
                      while (current != NULL && current->number != remove)            //check if current node is one we want to delete...if not advance current pointer to next one
                      {
                          temp = current;                                             //keep temp pointer one step behind
                          current = current->next;                                    //advance to next node, traverse list till at the end
                      }
                      if (current == NULL)                                            //pass through whole list and value not found
                      {
                          std::cout << "N/A
                  ";
                          delete remove1;                                             //removes spare number floating around in memory
                      }
                      else
                      {
                          remove1 = current;                                          //pointing to value we want to delete
                          current = current->next;                                    //advances current pointer to next node
                          temp->next = current;                                       //stops hole that occurs in list, patches this up
                          if (remove1 == head)                                        //if pointer is pointing to front of list
                          {
                              head = head->next;                                      //advance the head to next
                              temp = NULL;
                          }
                  
                          delete remove1;
                      }
                  };
                  
                  
                  void LinkedList::search(int searchNum)
                  {
                      Node* searchnumber = nullptr;
                      temp = head;
                      current = head;
                  
                      while (current != NULL && current->number != searchNum)
                      {
                          temp = current;
                          current = current->next;
                      }
                      if (current != NULL)
                      {
                          searchnumber = current;
                          current = current->next;
                          std::cout << "-" << searchnumber << " Found";
                      }
                      else
                      {
                          std::cout << "N/A";
                      }
                  };
                  
                  
                  void LinkedList::display()
                  {
                      current = head;                                                 //point to start of list
                  
                      while (current != NULL)                                         //while it points to something in list
                      {
                          std::cout << current->number;                               //display list starting from start
                          current = current->next;                                    //advance to next pointer
                      }
                  };
                  
                  
                  void LinkedList::reverse()
                  {
                      Node *new_head = nullptr;                                       //create new head as we want it to start from last element
                  
                      for (current = head; current;)                                  //same as display, ask it to go through list from head then outside loop assign to new head and switch sides
                      {
                          temp = current;                                             //keep temp pointer one step behind
                          current = current->next;                                    //goes through each element in the list
                          temp->next = new_head;                                      //scrolls through backwards from new head
                          new_head = temp;                                            
                      }
                  
                      head = new_head;                                                //assign head to new head
                  };
                  

                  推荐答案

                  您的搜索算法似乎有误.将其更改为:

                  Your search algorithm seems to be wrong. Change it to :

                  if (current != NULL) // (current == NULL) is wrong because it means the value wasn't found
                  {
                      searchnumber = current;
                      current = current->next;
                      std::cout << "-" << searchnumber->number << " Found"; // here searchnumber is the node's address. You need to print its value, so use searchnumber->number
                  }
                  

                  并且您不需要删除节点,直到找到所需的值.
                  您可以仅使用搜索算法来查找列表中是否已有数字.如果这就是你想要的.

                  And you don't need to remove nodes till you find the desired value.
                  You can just use your search algorithm to find if a number already in the list. If that's what you want.

                  这篇关于链表中的搜索函数 - 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 作为模板参数时出错)
                    1. <legend id='vjkCk'><style id='vjkCk'><dir id='vjkCk'><q id='vjkCk'></q></dir></style></legend>

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

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

                          <tfoot id='vjkCk'></tfoot>

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