I am having a runtime error happen when I run my program and can't seem to figure out why. It is happenning on my insertion sort function. I also am going to have to do a selection sort with this and aany help would be greatly appreciated. Here is my code:
#include <iostream>
#include <string>
using namespace std;
class node
{
public:
int account_number;
string name;
double balance;
node * next;
public:
node(int account_number, string name, double balance);
};
node::node(int account_number, string name, double balance)
{
this->account_number = account_number;
this->name =name;
this->balance= balance;
this->next = NULL;
}
class Account_List
{
private:
node * head;
public:
Account_List();
~Account_List();
void insert_to_head(int v, string n, double b);
void list_all();
bool search(string n);
void insert_in_order_account(int v, string n, double b);
void insert_in_order_balance(int v, string n, double b);
void remove(string n);
void insertion_sort_by_account();
void selection_sort_by_balance();
};
Account_List::Account_List()
{
head = NULL;
}
Account_List::~Account_List()
{
while(head != NULL)
{
node * temp = head;
head = head->next;
delete temp;
}
}
void Account_List::insert_to_head(int v, string n, double b)
{
node * temp = new node(v, n, b);
temp->next = head;
head = temp;
}
void Account_List::list_all()
{
node * temp= head;
while(temp!=NULL)
{
cout << temp-> account_number<< "\t" ;
cout << temp-> name<<"\t" ;
cout << temp-> balance<<endl ;
temp = temp->next;
}
}
bool Account_List::search(string n)
{
node * temp= head;
while(temp!=NULL)
{
if(temp->name==n) return true;
else temp = temp->next;
}
return false;
}
void Account_List::insert_in_order_account(int v, string n, double b)
{
node * temp = new node( v, n, b);
if(head== NULL) head=temp;
else if (temp->account_number <= head->account_number)
{
temp->next = head;
head= temp;
}
else
{
node * t1 = head;
node * t2 = t1->next;
while (t2 != NULL && temp->account_number > t2->account_number)
{
t1= t2;
t2= t2->next;
}
temp->next = t2;
t1->next = temp;
}
}
void Account_List::insert_in_order_balance(int v, string n, double b)
{
node * temp = new node( v, n, b);
if(head== NULL) head=temp;
else if (temp->balance > head->balance)
{
temp->next = head;
head= temp;
return;
}
else
{
node * t1 = head;
node * t2 = t1->next;
while (t2 != NULL && temp->balance < t2->balance)
{
t1= t2;
t2= t2->next;
}
temp->next = t2;
t1->next = temp;
}
}
void Account_List::remove(string n)
{
while(head!= NULL && head->name == n)
{
node * temp = head;
head = head->next;
delete temp;
}
if (head ==NULL)
return;
else
{
node * temp1 = head;
node * temp2 = head->next;
while (temp2 !=NULL)
{
if (temp2->name == n)
{
node * temp = temp2;
temp2 = temp2->next;
temp1->next = temp2;
delete temp;
}
else
{
temp1 = temp2;
temp2 = temp2->next;
}
}
}
}
void Account_List::insertion_sort_by_account()
{
node * temp = head;
head = NULL;
while(temp!=NULL)
{
node * temp1= head;
head = temp;
temp = temp->next;
if(head->account_number < temp1->account_number)
{
head->next = temp1;
}
else
{
node * t1 = temp1;
node * t2 = t1->next;
while (t2 != NULL && temp1->account_number < t2->account_number)
{
if (t1->account_number <= head->account_number && t2->account_number > head->account_number)
{
t1 = head;
head->next = t2;
head = temp1;
}
else
{
t1=t2;
t2=t2->next;
}
}
delete temp1;
}
}
}
int main()
{
Account_List List;
List.insert_in_order_account(2002, "Janet Smith", 100.99);
List.insert_in_order_account(1001, "Alex Bush", 99.88);
List.insert_in_order_account(3003, "John Rosa", 5.55);
cout << "List is in increasing order of account number\n";
List.list_all();
cout <<endl;
List.remove("Janet Smith");
List.remove("Alex Bush");
cout << "Two nodes are removed\n";
List.list_all();
cout <<endl;
List.insert_in_order_balance(2002, "Janet Smith", 100.99);
List.insert_in_order_balance(1001, "Alex Bush", 99.88);
cout << "List is in decreasing order of balance\n";
List.list_all();
cout <<endl;
List.insertion_sort_by_account();
cout << "List is in increasing order of account number\n";
List.list_all();
cout <<endl;
/*List.selection_sort_by_balance();
cout << "List is in increasing order of balance\n";
List.list_all();
cout <<endl; */
return 1;
}