c++ - 如何在链表中搜索特定字符串并返回该值?

标签 c++ class linked-list

我有一个程序接受带有 5 个参数的输入。输入是视频标题、url、评论、长度和评级。然后根据标题对它们进行排序。用户需要指定insert(输入视频信息),lookup(按标题查找视频并仅打印该视频及其相关信息),或打印(只需打印所有内容)。

例如

输入:

insert
Arthur Benjamin: Lightning calculation and other "Mathemagic"
http://www.youtube.com/watch?v=M4vqr3_ROIk
Hard to believe.
15.25  
4  
lookup  
Arthur Benjamin: Lightning calculation and other "Mathemagic"

输出:

Arthur Benjamin: Lightning calculation and other "Mathemagic" , http://www.youtube.com/watch?v=M4vqr3_ROIk, Hard to believe., 15.25, 4

我的问题是处理 main 中的lookup

if(user == "lookup")
        {
            getline(cin, title);
            if(vlistObj -> lookup(videoObj))
            {
                vlistObj->print();
            }
        }

还有在我的链接列表中查找

bool Vlist::lookup(Video *other)
{
  Node *node = m_head;
  return node->m_next -> m_video->GetTitle() == other-> GetTitle();
}

老实说,我很迷茫如何查找搜索特定标题(假设已经给出了很多视频标题/信息)并且只打印我要求的内容(假设它在列表中) .

完整代码如下:

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

class Video {

  public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    const string& GetTitle() const { return title; }

  private:

    std::string title;
    string link;
    string comment;
    double length;
    int rating;

};


Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
  : title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
}

void Video::print(){

  cout << title << ", " << link << ", " << comment << ", " << length << ", " << rating << endl;

}


class Vlist {
 public:
  Vlist() {m_head = nullptr; }
  bool lookup(Video *other);
  void Insert(Video *video);
  void print();
 private:
 class Node {
        public:
                Node(Video *video, Node *next) {m_video = video; m_next = next; }
                Video *m_video;
                Node *m_next;
            };
            Node *m_head;
 };


void Vlist::Insert(Video* video)
{
    if (m_head == NULL || m_head->m_video -> GetTitle() > video->GetTitle())
    {
        m_head = new Node(video, m_head);

    }
    else
    {
        Node *node = m_head;
        while (node->m_next != NULL && node->m_next -> m_video->GetTitle() < video->GetTitle())
        {
            node = node->m_next;
        }
        node->m_next = new Node(video, node->m_next);
    }
 }

bool Vlist::lookup(Video *other)
{
  Node *node = m_head;
  return node->m_next -> m_video->GetTitle() == other-> GetTitle();
}

 void Vlist::print()
 {
     Video *video;
     Node *node = m_head;

     while(node != NULL)
     {
        node -> m_video-> Video::print();
        node = node->m_next;
     }
 }


int main()
{
  string sort_type, url, comment, title, user;
    int rating;
    double length;
    int initial = 0, last = 0, number;

    Vlist *vlistObj= new Vlist();
    Video *videoObj;

    while (getline(cin,user)) {

        if(user == "insert")
        {
        getline(cin,title);
        getline(cin, url);
        getline(cin, comment);
        cin >> length;
        cin >> rating;
        cin.ignore();

        videoObj = new Video(title,url, comment, length, rating);
        vlistObj->Insert(videoObj);
        }

        if(user == "lookup")
        {
            getline(cin, title);
            if(vlistObj -> lookup(videoObj))
            {
                vlistObj->print();
            }
        }
        if(user == "print")
        {
            vlistObj->print();
        }
    }

}

Also I do want to note that I am receiving a segmentation fault. But I do know that it is because of my code in lookup. The program runs and output correctly if I do not type lookup

最佳答案

错误在 Vlist::lookup 函数中,当前节点指针指向 m_next,然后指向 m_video: m_next不是必需的,m_head应该直接指向m_video

在完整的工作代码下方,我还在这里和那里更改了一些内容以消除编译器的所有警告

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;

class Video {

  public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    const string& GetTitle() const { return title; }

  private:

    string title;
    string link;
    string comment;
    double length;
    int rating;

};


Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
  : title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
}

void Video::print(){

  cout << title << ", " << link << ", " << comment << ", " << length << ", " << rating << endl;

}


class Vlist {
 public:
  Vlist():m_head(nullptr) {}     // init_list
  bool lookup(const string& title);  // gets user input directly
  void Insert(Video *video);
  void print();
  Video* get(const string& title);   // new:returns pointer in list with given title
 private:
 class Node {
        public:
                Node(Video *video, Node *next):m_video(video), m_next(next) {}   // init_list
                Video *m_video;
                Node *m_next;
            } *m_head;    // declared directly together with class definition
 };


void Vlist::Insert(Video* video)
{
    if (m_head == NULL || m_head->m_video -> GetTitle() > video->GetTitle())
    {
        m_head = new Node(video, m_head);
    }
    else
    {
        Node *node = m_head;
        while (node->m_next != NULL && node->m_next -> m_video->GetTitle() < video->GetTitle())
        {
            node = node->m_next;
        }
        node->m_next = new Node(video, node->m_next);
    }
 }

bool Vlist::lookup(const string& title)
{
  Node *node = m_head;
  while (node->m_next != NULL && node-> m_video->GetTitle() != title)
  {
    node = node->m_next;
  }

  return node-> m_video->GetTitle() == title; // there was one pointer too many here
}

 void Vlist::print()
 {
     Node *node = m_head;

     while(node != NULL)
     {
        node -> m_video-> Video::print();
        node = node->m_next;
     }
 }

Video* Vlist::get(const string& title) // returns required item from list
{
    Node *node = m_head;
    while (node != NULL) {
        if (node->m_video->GetTitle() == title)
            return node->m_video;
        node = node->m_next;
    }

return nullptr;
}


int main()
{
  string sort_type, url, comment, title, user;
    int rating;
    double length;

    Vlist *vlistObj= new Vlist;
    Video *videoObj;

    while (getline(cin,user)) {

        if(user == "insert")
        {
        getline(cin,title);
        getline(cin, url);
        getline(cin, comment);
        cin >> length;
        cin >> rating;
        cin.ignore();

        videoObj = new Video(title, url, comment, length, rating);
        vlistObj->Insert(videoObj);
        }

        if(user == "lookup")   // more than a few changes here
        {
            getline(cin, title);

            if (vlistObj -> lookup(title))
            {
                videoObj = vlistObj->get(title);
                videoObj->print();
            } else {
                cout << "not found!\n";
            }
        }
        if(user == "print")
        {
            vlistObj->print();
        }
    }

}

大编辑

以前的版本没有正确遍历 Vlist

现在 lookup 命令正确搜索了 Vlist,从而最终打印出正确的 Video

关于c++ - 如何在链表中搜索特定字符串并返回该值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64190137/

相关文章:

c++ - 从堆栈中删除多余的变量 - C++

objective-c 头文件中的 C++ 类声明

c++ - 在 C++ 中检查 double (或 float )是否为 NaN

c++ - 什么是静态库,什么是动态库,有什么区别,什么更好用,为什么?

java - 为什么 VisualVM float 实例似乎不包含其他类中的 float ?

c# - 在 C# 中将派生类向上转换为基类

c++ - 方法 ('Vector' 的命名空间和模板类 vector 声明未命名类型;你是说 'perror' 吗?)

c - LinkedList 打印相同的值

c - 使用指向结构的指针在 C 中创建链表和内存分配

c - 链表斐波那契数列运行时错误