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; 
}

In line 180 you set head to NULL
Then in line 183 you set temp1 to be head
In line 186 you are making a comparison to the temp1 which is, NULL pointer

Thanks so much! That did help, but now it is only only keeping the last node it sorts when it displays. I know the display function works correctly, so it has to be this part of my code.

void Account_List::insertion_sort_by_account()
{
    node * temp = head->next;
    head->next = NULL;

    while(temp!=NULL)
    {
        node * temp1= head;
        head= temp;
        temp = temp->next;
        if(head->account_number < temp1->account_number)
        {
            head->next = temp1;

            delete temp1;
        }

Again, any help would be appreciated!

temp = head->next;

head = temp;

You keep moving the head of your list down the list. Perhaps if you referred to the current place in the list you're examining by a different name, you wouldn't mess up the head pointer.

Thanks for the reply. I am kind of new to this and am having problems with the linked lists. I am writing diagrams out and on paper this looks like it should work. I will try out what you recommended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.