链表appendNode方法“读取访问冲突”。

时间:2018-11-08 01:13:49

标签: c++

如果我在链接列表中添加了多个节点,则我的appendNode方法将返回读取访问冲突。我发现几乎所有具有while (node->next != null)的函数在运行程序时也会显示读取访问冲突。我不确定为什么,并且我已经对照其他人的附件检查了我的程序,他们以完全相同的方式进行操作。这是我的代码(首先是标头类):

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

}

1 个答案:

答案 0 :(得分:0)

node->next从未初始化,因此访问它是未定义行为。

ListNode创建并使用构造函数,其中所有成员都被赋予值。