node *temp = new node; // create empty node
temp = head;
Не связано с проблемой, но вы выделяете память для нового указателя temp
, затем temp
, назначенного другому указателю.Это ничего не дает, кроме утечки памяти.Измените на:
node *temp = head;
В функции insertionSort
у вас есть
if(sortNode == nullptr || strcmp(temp->firstName.c_str(), nextNode->firstName.c_str()) > 0)
Вы не проверяете, является ли nextNode
nullptr
.Бывает, что в некоторых случаях nextNode
равен NULL
, а затем вы пытаетесь получить доступ к nextNode.firstName
.Вот почему программа вылетает.
Вы должны научиться отлаживать программу.В Visual Studio нажмите кнопку отладки.Когда программа аварийно завершает работу, перейдите в «окно стека» (Control + Alt + C), это покажет вам строку кода в коде.
Во-вторых, у вас уже есть список, затем вы пытаетесь вставитьв новый список.Вы должны добавить новую функцию для вставки отсортированных в первом запуске:
struct node
{
node()
{
next = nullptr;
}
node(const string& fn, const string& ln, const string& cn, const string& ph,
double contrib, int i)
{
next = nullptr;
firstName = fn;
lastName = ln;
country = cn;
phone = ph;
contribution = contrib;
id = i;
}
string firstName;
string lastName;
string country;
string phone;
double contribution;
int id;
node *next;
friend ostream& operator<<(ostream& os, const node& n)
{
os << n.lastName << ", " << n.firstName << '\t' << n.country << '\t'
<< n.phone << '\t' << n.contribution << '\t' << n.id << endl;
return os;
}
};
class list
{
private:
node *head, *tail;
public:
list()
{
head = nullptr;
tail = nullptr;
}
void append(const string& fn, const string& ln, const string& cn,
const string& ph, double contrib, int i)
{
node *ptr = new node(fn, ln, cn, ph, contrib, i);
if(head == nullptr)
{
head = ptr;
tail = ptr;
}
else
{
tail->next = ptr;
tail = ptr;
}
}
void insert_sorted(const string& fn, const string& ln, const string& cn,
const string& ph, double contrib, int i)
{
node *ptr = new node(fn, ln, cn, ph, contrib, i);
if(head == nullptr)
{
head = ptr;
tail = ptr;
}
else
{
bool inserted = false;
node *walk = head;
node *prev = nullptr;
while(walk)
{
if(ptr->firstName < walk->firstName)
{
//2 cases, inserting at the start or middle
if(walk == head)
{
//start
ptr->next = head;
head = ptr;
}
else
{
//middle
prev->next = ptr;
ptr->next = walk;
}
inserted = true;
break;
}
prev = walk;
walk = walk->next;
}
if(!inserted)
{
tail->next = ptr;
tail = ptr;
}
}
}
void print()
{
node *ptr = head;
while(ptr)
{
cout << *ptr;
ptr = ptr->next;
}
}
};
int main()
{
string firstName;
string lastName;
string country;
string phone;
double contribution;
int id;
string temp;
string line;
list List;
ifstream inFile("contributors.csv");
if(!inFile)
cerr << "File failed to open.\n";
// read data from file by line
while(getline(inFile, line))
{
stringstream ss(line); // parse line
getline(ss, firstName, ',');
getline(ss, lastName, ',');
getline(ss, country, ',');
getline(ss, phone, ',');
getline(ss, temp, ',');
contribution = stod(temp);
getline(ss, temp, ',');
id = stoi(temp);
//List.append(firstName, lastName, country, phone, contribution, id);
List.insert_sorted(firstName, lastName, country, phone, contribution, id);
}
List.print();
return 0;
}
В качестве альтернативы вы можете создать несортированный список с помощью createNode
, а затем использовать функцию сортировки для сортировки списка.
Чтобы сделать это по-другому, создайте новый список temp_list
, затем вставьте отсортированный temp_list.insert_sorted(...)
, затем удалите элементы в List
, скопируйте temp_list
в List
(но это не очень эффективно)