【问题标题】:Search Function in Linked List - C++ [closed]链表中的搜索功能 - C++ [关闭]
【发布时间】:2015-02-21 22:25:03
【问题描述】:

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

#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\n";
        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
};

【问题讨论】:

  • 问题是什么?
  • 如果我正确地编写了我的搜索功能来做我想做的事
  • 您可以遍历列表并搜索元素。最坏的情况,这是 O(n) ......不是很有效。您可能会考虑实现二叉搜索树(搜索最坏情况 O(ln(n)))。
  • @Ryan 那么你为什么不测试它而不是问我们呢?
  • 我想知道是否有一种更有效的方法来进行搜索,而不是遵循与删除功能相同的路径

标签: c++ linked-list


【解决方案1】:

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

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
}

在找到所需值之前,您无需删除节点。
您可以使用您的搜索算法来查找列表中是否已经存在一个数字。如果这就是你想要的。

【讨论】:

  • 它可以工作,但它显示一个随机数而不是我想找到的数字。例如。 000nbn45找到
  • if (current == NULL) 怎么会显示一个数字??它是空的......你知道我的意思......改变它,我认为它会显示实际数字
  • 如果您提供您的代码不起作用的输入以及您的完整代码,那就太好了
  • 我已经进行了更改并完成了您上面所说的操作,如果我在列表中搜索一个值并且它不存在,它就可以工作。如果我在列表中搜索一个值,它也可以工作,但不是显示 2,而是显示一个十六进制数字,例如 050b3ng44
  • 编辑您的问题并提供完整代码...包括添加值
【解决方案2】:

虽然列表是无序的,但搜索算法的比较没有任何意义。只需逐个迭代所有节点并应用匹配条件。

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多