【问题标题】:Expection Unhandled Error when implementing a LinkedList in c++在 C++ 中实现链接列表时出现异常未处理错误
【发布时间】:2020-07-10 23:42:27
【问题描述】:

好的,所以我正在尝试实现 LinkedList 数据结构,但是当我尝试遍历我的列表(printNodes 和插入函数)时,我遇到了一个错误,上面写着:“抛出未处理的异常:读取访问冲突。tmpNode 是 0xCDCDCDCD。 "我觉得这与我的指针没有按照我认为应该的方式运行有关,但我不确定。非常感谢您的帮助。

#include<iostream>;
using namespace std;
struct Node {
    int data;
    Node* next;

    Node(int el) {data = el; } //constructor
    Node(int el, Node* ptr) { data = el; next = ptr; } //constructor
};

class LinkedList {

public:
    Node* head = NULL, * tail = NULL;
    void addToHead(int el) {
        head = new Node(el, head);
    }

    void insert(int el) {
        Node* newNode = new Node(el);
        if (head == nullptr) {
            head = newNode;
        }
        else {
            Node* tmpNode = head;
            while (tmpNode->next != nullptr) {
                tmpNode = tmpNode->next;
            }tmpNode->next = newNode;
        }
    }
    void printNodes() {
        Node* tmpNode = head;
        cout << tmpNode->data;
        while (tmpNode->next != nullptr) {
            std::cout << tmpNode->data;
            tmpNode = tmpNode->next;
        }

    }
};

int main() {
    LinkedList myList = LinkedList();
    myList.insert(10);
    myList.addToHead(20);
    myList.insert(10);
    myList.printNodes();
}

【问题讨论】:

  • 我觉得这与我的指针没有按照我认为应该的方式运行有关,但我不确定。 -- 所以我认为你没有'不使用调试器?
  • 在构造函数中将next设置为nullptr:至少Node(int el) {data = el; next = nullptr; }
  • 你有一个tail 指针——使用它,不要迭代插入到末尾。您不需要insert,只需一个add 函数,例如void add (int el) { Node *newNode = new Node(el); if (head == nullptr) head = tail = newNode; else { tail-&gt;next = newNode; tail = newNode; } 您的 Node(int el) {data = el; } 构造函数必须初始化 next = nullptr;,例如Node(int el) {data = el; next = nullptr; } 如上面的@Manuel 所示。
  • @DavidC.Rankin 谢谢我这样做了,但我仍然需要知道如何迭代才能对列表进行线性搜索。
  • 旁注:您正在使用 new 动态分配内存,而没有匹配的 delete 调用,这就是为什么您应该使用 std::unique_ptr!

标签: c++ data-structures linked-list


【解决方案1】:

您的迭代是正确的,但是您的 printNodes 函数存在问题。它取消引用tmpNode 而不检查null

void printNodes() {
    Node* tmpNode = head;
    cout << tmpNode->data; // <-- here
    while (tmpNode->next != nullptr) {
        std::cout << tmpNode->data;
        tmpNode = tmpNode->next;
    }

}

我会将其更改为以下内容:

void printNodes() {
    Node* tmpNode = head;

    while (tmpNode != nullptr) {
        std::cout << tmpNode->data << ", ";
        tmpNode = tmpNode->next;
    }
}

除此之外,正如 cmets 中所说,如果您在 Node 构造函数中将 next 成员设置为 null,它应该可以正常工作。

搜索是一样的,只是要检查数据:

Node* findNode(int el) {
    Node* tmpNode = head;
    Node* ret = nullptr;

    while (tmpNode != nullptr) {
        if (tmpNode->data == el) {
            ret = tmpNode;
            break;
        }
        tmpNode = tmpNode->next;
    }
    return ret;
}

main:

Node* n = myList.findNode(10);
if (n)
    std::cout << "N 10: " << n->data << "\n";

n = myList.findNode(30);
if (n)
    std::cout << "N 30: " << n->data << "\n";
else
    std::cout << "There is no N 30\n";

还存在内存泄漏问题,正如 cmets 中的 @RikusHoney 所指出的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多