Search code examples
c++functionclasslinked-listsortedlist

How do you access another class using a link list insert function?


I have an existing program that takes in a input with 5 parameters. The inputs are a video title, url, comment, length, and rating. All contained in video.cpp and video.h. My new task is to insert each video into a linked list in sorted order, according to the video title.

My problem is that I have no idea how to access class Video, and it's parameters, from my void Vlist::Insert() function...? Here is what I have so far...

vlist.cpp:

#include <iostream>
#include <algorithm>
using namespace std;

#include "vlist.h"
#include "video.h"


void Vlist::Insert(Video)
  {

    if (m_head == NULL || m_head->m_video > video) {
      // Insert before head...
      m_head = new Node(video, m_head);
    } else {
      // Insert after existing node...
      Node *node = m_head;
      while (node->m_video != NULL && node->m_next->m_video < video) {
        node = node->m_next;
      }
      node->m_next = new Node(video, node->m_next);
    }

 }

vlist.h:

#ifndef VLIST_H
#define VLIST_H

using namespace std;

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




#endif

video.h:

#ifndef VIDEO_H
#define VIDEO_H

using namespace std;

class Video {

  public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    bool Title(Video *spare);




  private:


    string name;
    string web;
    string note;
    double duration;
    int score;

    string title;
   
};


#endif

video.cpp:

#include <iostream>
#include <algorithm>
using namespace std;

#include "video.h"

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)
{
  name = title;
}


bool Video::Title(Video *spare)
{
  return name > spare-> name;

}
void Video::print(){

  string hierarchy;
  switch(rating){

    case 1:
      hierarchy = "*";
      break;
    case 2:
      hierarchy = "**";
      break;
    case 3:
      hierarchy = "***";
      break;
    case 4:
      hierarchy = "****";
      break;
    case 5:
      hierarchy = "*****";
      break;

  }



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

}

I have tried void Vlist::Insert(Video) , void Vlist::Insert(Video *video), void Vlist::Insert(m_video) but honestly I have no idea what I am doing. This is all very new to me. Any help would be appreciated.

Note: I realize I have not completed the linked list, I am merely trying to work in parts.


Solution

  • The members of Video are private. You cannot access them unless the class exposes public accessor functions.

    class Video
    {
    public:
        const std::string& GetTitle() const { return title; }
    
        // ...
    
    private:
        std::string title;
    };
    

    Then in your list:

    void Vlist::Insert(Video* video)
    {
        if (m_head == NULL || m_head->m_video->GetTitle() > video->GetTitle())
        {
            // Insert before head...
            m_head = new Node(video, m_head);
        }
        else
        {
            // Insert after existing node...
            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);
        }    
     }