Ich habe einen Code für mein Linked-List-Projekt mit C++ geschrieben. dies ist mein Code so weitVerknüpfte Liste und der Konstruktor
Header-Datei:
#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";
return false;
}
if (head->next == NULL)
{
head->next= newSong;
length++;
return true;
}
count =0;
song1* p = head;
song1* q = head;
while (q)
{
if (count==track)
{
p->next = newSong;
newSong-> next = q;
length++;
return true;
}
p=q;
q=p->next;
count++;
}
if (count==track)
{
p->next = newSong;
newSong-> next = q;
length++;
return true;
}
cerr<<"Song was not added in the list";
return false;
}
void song_list::printSong()
{
int count = 0;
song1* p=head;
song1* q=head;
cout<<"\n------------------\n";
cout<<"Song playlist\n";
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;
}
}
Hauptdatei:
#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);
return 0;
}
Der Code kann erfolgreich sein bauen, aber wenn ich das Programm ausführen, zeigen sie die „funktioniert nicht mehr " Botschaft. Ich finde heraus, dass der Fehler in meinem Konstruktor ist. Ist das der richtige Weg, um die Zeichenfolge zu initialisieren? denn wenn ich den Konstruktor lösche, kam eine Meldung heraus, dass "Titel und Titel zuerst initialisiert werden müssen". Ich bin immer noch neu in C++ und lerne Stück für Stück die verlinkte Liste. Übrigens, mache ich die verknüpfte Liste korrekt ?. Vielen Dank!
'song_list' Mitglied' head' hat ein ähnliches Problem – makadev
ich versuchte Erinnerung an song1 zuzuteilen den Konstruktor verwenden. aber ich bin mir nicht sicher, ob ich es richtig mache. –
Der Konstruktor wird aufgerufen, wenn Sie "new ClassName()" oder etwas ähnliches sagen. song1 * info = neuer Song1(); würde den Konstruktor aufrufen, Speicher zu alloate. Allerdings, Smart-Zeigern sind in der Regel zu bevorzugen die Allocation yourlsef (std :: make_unique();) –
xaxxon