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

时间:2018-03-09 05:13:36

标签: 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对象。我知道这不是显示课程的其余部分,但我知道它一切正常,错误在于此功能。当我尝试运行该程序时,会抛出一个异常,说&#34;读取访问冲突&#34;在线#34;删除(临时);&#34;。我相信我需要将其余的节点移回并创建另一个节点,例如&#34; prev&#34;在删除它之前存储temp,但我已经尝试了很多,因为我说我是新的。任何人都可以告诉我需要添加什么才能使其工作,并请解释为什么所以我从中学习。提前谢谢!

2 个答案:

答案 0 :(得分:2)

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

要修复它,要么在找到匹配项后中断循环,要么在删除temp之前跟踪下一个节点。

此外,你不应该在delete函数中有一个输出语句,因为另一个人或你将来不会期望一个删除元素的函数来将内容打印到屏幕上。

答案 1 :(得分:2)

在你的

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