Linked List appendNode method “read access violation.”

Berty

New Member
#1
My appendNode method returns read access violation if I add more than 1 node to my linkedlist. I've found that almost every function that has the while (node->next != null) also says read access violation when I run the program. I am not sure why, and I have checked my program against other people's append, they do theirs the exact same way. Here is my code (first the header class):
Mã:
class StudentList
{
    private:

        struct ListNode {

            ListNode* next;
            string firstName;
            string lastName;
            double grade;

        };

        ListNode *head;

    public:

        StudentList();
        void insertNode(string fname, string lname, double grade);
        void deleteNode(string fname, string lname);
        int search(string fname, string lname);
        void displayList() const;
        void appendNode(string fname, string lname, double grade);
};


#include "pch.h"
#include "StudentList.h"

StudentList::StudentList()
{
    head = new ListNode;
    head = nullptr;
}

void StudentList::insertNode(string fname, string lname, double grade)
{
    ListNode *ptrNode = head;
    ListNode *Node = nullptr;
    if (ptrNode == NULL)
    {
        cout << "There are no nodes in the list";
    }
    else
    {
        while ((ptrNode->grade < Node->grade))
        {
            ptrNode = ptrNode->next;
        }

    }
}

void StudentList::deleteNode(string fname, string lname)
{
    ListNode *temp = head;
    ListNode *ptrNode = head;
    if (ptrNode == NULL)
    {
        cout << "There are no nodes in the list";
    }
    else
    {
        while ((ptrNode->firstName != fname) && (ptrNode->lastName != lname) && ptrNode != NULL)
        {
            if ((ptrNode->firstName == fname) && (ptrNode->lastName == lname))
            {
                temp->next = ptrNode->next;
                delete ptrNode;
            }

            temp = ptrNode;
            ptrNode = ptrNode->next;
        }
    }
}

int StudentList::search(string fname, string lname)
{
    int count = 0;
    ListNode *ptrNode = head;
    if (ptrNode == NULL)
    {
        cout << "There are no nodes in the list";
    }
    else
    {
        while ((ptrNode->firstName != fname) && (ptrNode->lastName != lname) && ptrNode != NULL)
        {

            if ((ptrNode->firstName == fname) && (ptrNode->lastName == lname))
            {
                return count;
            }
            count++;
            ptrNode = ptrNode->next;
        }
        cout << "Could not find this first and last name";
    }

}

void StudentList::displayList() const
{
    ListNode *ptrNode = head;
    if (ptrNode == NULL)
    {
        cout << "There are no nodes in the list";
    }
    else
    {
        while (ptrNode != NULL)
        {
            cout << ptrNode->firstName << " " << ptrNode->lastName << ptrNode->grade << "\n";
            ptrNode = ptrNode->next;
        }

    }
}

void StudentList::appendNode(string fname, string lname, double grade)
{
    ListNode *node = new ListNode;
    node->firstName = fname;
    node->lastName = lname;
    node->grade = grade;
    if (head == NULL) {
        head = node;
    }
    else {
        ListNode *temp = new ListNode;
        temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = node;
    }

}
 

Admin

Administrator
Thành viên BQT
#2
node->next is never initialized, so accessing it is Undefined Behavior.

Create and use a constructor for ListNode where all members are given values.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top