(C++)Process returned -1073741819 (0xC0000005) when working with a linked list, but the algorithm works

0

The goal of this program is to remove any elements from the list that are greater than the following element. I've managed to make this program perform this task, however it crashes right after doing so and does not print the list afterwards, outputting an 0xC0000005 error code.

#include <iostream>
using namespace std;
class List{
    struct node{
        int data;
        node* next;
    };
    typedef struct node* nodePtr;
    nodePtr head;
    nodePtr curr;
    nodePtr temp;

public:
    List()
    {
        head=NULL;
        curr=NULL;
        temp=NULL;
    }
    void AddNode(int addData)
    {
        nodePtr n = new node;
        n->next = NULL;
        n->data = addData;
        if(head != NULL) {
            curr = head;
            while(curr->next != NULL) {
                curr = curr->next;
            }
            curr->next = n;
        }
        else
        {
            head = n;
        }
    };
    void DeleteNode(int delData)
    {
        nodePtr delPtr = NULL;
        temp = head;
        curr = head;
        while(curr != NULL && curr->data != delData) {
            temp = curr;
            curr = curr->next;
        }
        if(curr == NULL) {
            cout << delData << " is not in the list" << endl;
            delete delPtr;
        }
        else {
            delPtr = curr;
            curr = curr->next;
            temp->next = curr;
            if(delPtr == head) {
                head = head->next;
                temp = NULL;
            }
            delete delPtr;
            cout << delData << " was removed from the list" << endl;
        }
    };
    void PrintList()
    {   cout << "List: " << endl;
        curr = head;
        while (curr != NULL) {
            cout << curr->data << endl;
            curr = curr->next;
        }
    }
    void toss()
    {
        curr = head;
        while (curr != NULL) {

            if(curr->data>curr->next->data){
                    DeleteNode(curr->data);
                    curr = head;
            }
            else curr = curr->next;
            if(curr == NULL) break;
        }

    }
};
int main() {
    List list;
    int i;
    cout << "Input list values, stop input by inputting 0: " << endl;
    cin >> i;
    while(i != 0){
        list.AddNode(i);
        cin >> i; // 18 9 3 4 0 
}
    list.PrintList(); // 18 9 3 4
    list.toss(); // tosses 18 and 9
    list.PrintList(); // should output 3 4 
};

This is the test example from the comments at the bottom of the code, it shows that the correct elements are removed, but doesn't print out the list the second time.

Input list values, stop input by inputting 0:
18
9
3
4
0
List:
18
9
3
4
18 was removed from the list
9 was removed from the list

Process returned -1073741819 (0xC0000005)   execution time : 6.699 s
Press any key to continue.

Any input or help on this problem would be greatly appreciated, thanks!

c++
list
asked on Stack Overflow May 8, 2020 by Pulis

1 Answer

1

When it reaches the last node in the list, aka curr->next equals null you are trying to access curr->next->data but next is null. Change your if statement to this:

    while (curr != NULL) {

        if(curr->next != NULL && curr->data > curr->next->data){
                DeleteNode(curr->data);
                curr = head;
        }
        else curr = curr->next;
        if(curr == NULL) break;
    }

or something equivalent. You can also check for this in the while loop's condition.

So basically you have to ensure that curr->next is not NULL before you access
curr->next->data

answered on Stack Overflow May 8, 2020 by Phillip Schulze

User contributions licensed under CC BY-SA 3.0