c++ - 删除函数c++的读取访问冲突异常

标签 c++ exception linked-list

#include <iostream>
#include <string>
using namespace std;

class Person{
public:
    string name;
    int age, height, weight;

    Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
        this->name = name;
        this->age = age;
        this->height = height;
        this->weight = weight;
    }
    Person operator = (const Person &P) {
        name = P.name;
        age = P.age;
        height = P.height;
        weight = P.weight;

        return *this;
    }

    friend ostream& operator<<(ostream& os, const Person& p);
};

ostream& operator<<(ostream& os, Person& p) {
    os << "Name: " << p.name << "   " << "Age: " << p.age << "     " << "Height: " << p.height << " " << "Weight: " << p.weight << "\n";
    return os;
};

class Node {
public:
    Person* data;
    Node* next;
    Node(Person*A) {
        data = A;
        next = nullptr;
    }
};

class LinkedList {
public:
    Node * head;
    LinkedList() {
        head = nullptr;
    }

    void InsertAtHead(Person*A) {
        Node* node = new Node(A);
        node->next = head;
        head = node;
    }
    void InsertAtEnd(Person*A) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = node;
        }
    }
    void InsertAtPosition(Person*A, int pos) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            for (int i = 1; i < pos - 1; i++) { temp = temp->next; }
            node->next = temp->next;
            temp->next = node;
        }
    }
    void DeleteByValue(string search_name) {
        Node* temp = head;
        while (temp != nullptr) {
            if (temp->data->name == search_name) {
                delete(temp);
            }
            else {
                temp = temp->next;
            }
        }
        cout << "No person with that name was in the list" << endl;
    }

    void DeleteFromHead() {
        if (head != nullptr) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
    void DeleteFromEnd() {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (head->next == nullptr) { DeleteFromHead(); }
       else {
            while (temp->next != nullptr) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = nullptr;
            delete temp;
        }
    }
    void DeleteAtPosition(int pos) {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (pos == 1) { DeleteFromHead(); }
        else {
            for (int i = 1; i < pos; i++) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = temp->next;
            delete temp;
        }
    }
    void UpdateAtPosition(Person*A, int pos) {
        if (head == nullptr) { cout << "No element in the list"; return; }
        if (pos == 1) { head->data = A; }
        else {
            Node* temp = head;
            for (int i = 1; i < pos; i++) {
                temp = temp->next;
            }
            temp->data = A;
        }
    }

    void Print() {
        Node* temp = head;
        while (temp != nullptr) {
            cout << *(temp->data);
            temp = temp->next;
        }
        cout << endl;
    }
};
int main() {
    LinkedList* list = new LinkedList();
    Stack* stack = new Stack(3);
    DynamicStack* dstack = new DynamicStack();


    cout << "Linked List" << endl;
    cout << "-----------" << endl;
    list->InsertAtHead(new Person("Jeremy", 22, 70, 145));                  list->Print();
    list->InsertAtHead(new Person("Samantha", 20, 63, 115));                list->Print();
    list->InsertAtEnd(new Person("Chris", 19, 70, 200));                    list->Print();
    list->DeleteByValue("Chris");                                           list->Print();
    list->InsertAtPosition(new Person("Grace", 15, 64, 150), 3);            list->Print();
    list->InsertAtPosition(new Person("Robert", 15, 67, 160), 4);           list->Print();
    list->DeleteFromHead();                                                 list->Print();
    list->DeleteFromEnd();                                                  list->Print();
    list->DeleteAtPosition(2);                                              list->Print();
    list->UpdateAtPosition(new Person("Jeremy", 23, 70, 155), 1);           list->Print();
    cout << endl;
    cout << endl;
    system("pause");
}

我是 C++ 的新手,我正在尝试为我的链表类创建一个函数,该函数将按 Persons 名称删除 Person 对象。我知道这并没有显示类(class)的其余部分,但我知道一切正常,错误就出在这个函数中。当我尝试运行该程序时,在“delete(temp);”行上抛出一个异常,提示“Read Access Violation”。我确信我需要将其余节点移回并创建另一个节点(例如“prev”)以在删除它之前存储临时文件,但我已经尝试了很多,正如我所说的那样,我是新手。任何人都可以告诉我我需要添加什么才能使其正常工作,并请解释原因,以便我从中学习。提前致谢!

最佳答案

在你的

void DeleteByValue(string search_name) {
    Node* temp = head;
    while (temp != nullptr) {
        if (temp->data->name == search_name) {
            delete(temp);
        }
        else {
            temp = temp->next;
        }
    }
    cout << "No person with that name was in the list" << endl;
}

一旦找到匹配项,您只需删除内存但还有更多内容,如果在第一个节点中找到该名称,则需要处理前一个节点和头节点。

像这样

    Node* temp = head;
    Node* prev = nullptr;
    while (temp != nullptr) {
        if (temp->data->name == search_name) {
            if (prev != nullptr) {
              prev->next = temp->next;
            }
            else {
               head = temp->next;
            }
            delete temp;
            temp = nullptr;  
        }
        else {
            prev = temp;
            temp = temp->next;
        }
    }

关于c++ - 删除函数c++的读取访问冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49186986/

相关文章:

c++ - 如何使用 boost::spirit::x3 测试字符串的有效双重内容?

spring - NoSuchBeanDefinitionException 至少 1 个 bean 有资格作为此依赖项的 Autowiring 候选者

c++ - 读取 txt 文件并将值放入列表中 (c++)

c - 使用链表将数据插入堆栈

php - 如何在您的扩展程序中抛出异常?

C++ 为什么指针值改变了?

c++11 使用 std::swap 与 operator=(T&&) 清除容器

c++ - 32 位与 64 位 : Massive Runtime Difference

C++ 前向声明和 header 包含

java - 控制异常是否被静态 boolean 值吞没