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


#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++新手,我正在尝试为我的链表类创建一个函数,该函数将按人员名称删除 Person 对象。我知道这没有显示类的其余部分,但我知道它都在工作,错误在于这个函数。当我尝试运行该程序时,在"delete(temp(;"行上抛出了一个异常,说"读取访问冲突"。我相信我需要将其余节点移回并制作另一个节点(例如"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;
        }
    }

找到匹配项后,将其删除,循环将继续运行,因为您从未将temp设置为 nullptrtemp现在不再有效,因为它刚刚被删除,但是,您尝试在下一次迭代时访问它指向的数据,产生未定义的行为,然后将其删除,这可能会导致崩溃。

若要解决此问题,请在找到匹配项后中断循环,或者在删除 temp 之前跟踪下一个节点。

此外,您不应该在 delete 函数中使用输出语句,因为将来其他人或您不会期望删除元素以将内容打印到屏幕上的函数。

相关内容

  • 没有找到相关文章

最新更新