我得到一个分段错误尝试的节点添加到链表在C结束时(核心转储)错误++

AG8:

所以,我创建了一个新的链表,我无法在链接列表的末尾插入一个新的节点。我试图通过列表遍历的不同迭代,但我认为问题出在哪里,我试图插入节点结束。

#include <iostream>
using namespace std;
//Make a basic linked list and understand how it works -- displaying -- insert at end 

class Node {
public: 
    string m_name;
    Node *m_next;
};

class LinkedList {
public:
    Node *m_head;
    int m_size;
    LinkedList() { //constructor
        m_head = nullptr;
        m_size = 0;
    }
    void InsertAtEnd(Node *ptr) { //we must traverse to the end
        Node *temp = m_head;
        while (temp != nullptr) {
            temp = temp->m_next;
        }
        temp->m_next = ptr->m_next;
    }
    void Display() {
        Node *temp = m_head;
        if (temp == nullptr) {
            cout << "The linked list is empty!" << endl;
        }
        else {
            while (temp->m_next != nullptr) {
                cout << temp->m_name << " ";
                temp = temp->m_next;
            }
        }
    }

};

int main() {
    //creates the pointers
    Node *first = nullptr;
    Node *second = nullptr;

    //create nodes using pointers
    first = new Node();
    second = new Node();

    //add names to nodes
    first->m_name = "Mike";
    second->m_name = "Ethan";

    //insert these pointers into a newly constructed linked list
    LinkedList MyList;
    MyList.InsertAtEnd(first);
    MyList.InsertAtEnd(second);
    MyList.Display();
    return 0;
}
cigien:

你应该通过您的代码使用调试器一步。在你的函数

 void InsertAtEnd(Node *ptr) { //we must traverse to the end
        Node *temp = m_head;
        while (temp != nullptr) {
            temp = temp->m_next;
        }
        temp->m_next = ptr->m_next; // but temp is nullptr. BOOM
    }

你迭代,直到tempnullptr但在这一点上,做的temp->m_next是UB。您需要只是停止。此外,你应该联系起来ptr,而不是ptr->m_next

 void InsertAtEnd(Node *ptr) { //we must traverse to the end
        Node *temp = m_head;
        while (temp->m_next != nullptr) { // look ahead
            temp = temp->m_next;
        }
        temp->m_next = ptr;  // just ptr
    }

当然,你也必须做额外的检查,以防链表是空的

 void InsertAtEnd(Node *ptr) { //we must traverse to the end
    if (m_head == nullptr)
         m_head = ptr;
    else {    
    Node *temp = m_head;
        while (temp != nullptr) {
            temp = temp->m_next;
        }
        temp->m_next = ptr->m_next;
    }
}

你似乎是在做相反的事在你的显示功能。在那里,你应该迭代,直到tempIS nullptr否则,你将无法打印的最后一个元素。

另外,请不要做 using namespace std;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

删除通用双端队列的第一个节点时出现分段错误(核心转储)

将节点添加到链表时出现分段错误

尝试将浮点数读入 C++ 中的链表时出现分段错误(核心转储)

创建链表会出现分段错误(核心转储)错误

C 中的分段错误(核心转储) - 使用 PTHREADS 时

使用 fscanf 时 C 中的分段错误(核心转储)

C - 获取分段错误(核心转储)

C ++:分段错误(核心转储)问题

分段错误(核心转储)C ++-指针

分段错误(核心转储)C++

C 中的分段错误(核心转储)

分段错误(核心转储)C

当我在 eclipse ubuntu 上运行 opencv 面部识别代码时,我得到了分段错误核心转储

核心转储分段错误

当我使用 sprintf 将一个大小为 3 的字符串和一个字符输出到一个大小为 5 的字符串时,出现分段错误(核心转储)

尝试从数组复制时出现分段错误(核心转储)错误

带有链表基本实现的 C++ 中的分段错误(核心转储)

分段错误(核心转储)我如何摆脱这个错误?

创建线程时出现分段错误(核心转储)错误

执行代码时出现分段错误(核心转储)错误

如何修复C中的分段错误(核心转储)错误

C 程序错误 - 分段错误(核心转储)

分段错误:C中的核心转储错误

C编程分段错误(核心转储)错误

分段错误(核心转储) - C++ 错误

错误:分段错误,核心转储,在 c 编程中

尝试使用 (char**)malloc() 分配内存时出现分段错误(核心转储)

信号:分段错误(核心转储)错误

如何在读取文件时修复 C++ 上的分段错误(核心转储)错误?