How do you search for a specific string in a linked list and return that value?

I have a program that takes in input with 5 parameters. The inputs are video title, url, comment, length, and rating. Then sorts them based on title. The user will need to specify insert (to enter the video information), lookup (look up a video by title and print ONLY that video and its information associated with it), or print (just simply print everything).

for example

input:

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"

output:

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

my problem is dealing with lookup in main

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

and also lookup in my linked list

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

I am honestly very lost on how to make lookup search for a specific title (assuming lots of video title/info has been given) and only print what I ask (assuming it's in the list).

Here is the complete code:

#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



Read more here: https://stackoverflow.com/questions/64190137/how-do-you-search-for-a-specific-string-in-a-linked-list-and-return-that-value

Content Attribution

This content was originally published by SeanPixel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: