خرید بک لینک

Vote count: 0

I've been writing a code for my linked list project using C++. this is my code so far

Header file:

 #include <iostream>
 #include <string>
using namespace std;
struct song1
{
    string song;
    string title;
    song1* next;
};
class song_list
{
protected:
    song1* head;
    int length;
public:
    song_list();
    bool insertSong (song1* newSong, int track);
    //bool removeSong (int track);
    void printSong();
    ~song_list();
};
song_list::song_list()
{
    head->song = "No data";
    head->title = "No data";
    head->next = NULL;
    length = 0;
}
bool song_list::insertSong (song1* newSong, int track)
{
    int count=0;
    if ((track<=0) || (track>length+1))
    {
        cerr<<"nThe given track is out of range";
        retu false;
    }
    if (head->next == NULL)
    {
        head->next= newSong;
        length++;
        retu true;
    }
    count =0;
    song1* p = head;
    song1* q = head;
    while (q)
    {
        if (count==track)
        {
            p->next = newSong;
            newSong-> next = q;
            length++;
            retu true;
        }
        p=q;
        q=p->next;
        count++;
    }
    if (count==track)
    {
        p->next = newSong;
        newSong-> next = q;
        length++;
        retu true;
    }
    cerr<<"Song was not added in the list";
    retu false;
}
void song_list::printSong()
{
    int count = 0;
    song1* p=head;
    song1* q=head;
    cout<<"n------------------n";
    cout<<"Song playlistn";
    while (q)
    {
        p = q;
        cout<<"n------------------n";
        cout<<"tPosition "<<count<<endl;
        cout<<"tsong "<<p->title<<endl;
        cout<<"tArtist "<<p->song<<endl;
        q= p->next;
        count++;
    }
}
song_list::~song_list()
{
    song1* p= head;
    song1* q=head;
    while (q)
    {
        p = q;
        q = p->next;
        if (q) delete p;
    }
}

Main file:

#include <iostream>
#include <string>
#include "LinkedListh.h"
using namespace std;
int main ()
{
    int choice,repeat,trc;
    song1* info;
    song_list func;
    do
    {
    cout<<"1. Add song "<<endl;
    cout<<"2. Delete song "<<endl;
    cout<<"3. Show song "<<endl;
    cout<<"4. Search song "<<endl;
    cin>>choice;
    switch (choice)
    {
    case 1:
            cout<<endl<<"Artist: ";
            getline (cin,info->song);
            //getline (cin,info->song);
            cout<<endl<<"Song Title: ";
            getline (cin,info->title);
            cout<<"Song number: "<<endl;//ask the user to put the song number
            //if artist doesnt exist the user should put 1.
            //else user should put what number the song is.
            cin>>trc;
            func.insertSong(info,trc);
        break;
    case 3:
        func.printSong();
        break;
    }
    cout<<"Repeat? 1.Yes 2.No"<<endl;
    cin>>repeat;
    }while (repeat == 1);
    retu 0;
}

The code can be build successfully but when i run the program, it show the "has stopped working" message. I figure out the error is in my constructor. Is that the correct way to initialize string? because if I erase the constructor, a message saying "song and title need to be initialize first" came out. I am still new in C++ so i am leaing linked list bit by bit. By the way, am I doing the linked list correctly?. Thanks!

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 22 فروردين 1395 ساعت: 17:44

صفحه بندی