I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++

AG8 :

So I've created a new Linked list and I'm having trouble inserting a new Node at the end of the Linked List. I've tried different iterations of traversing through the list but I think the problem is at the end where I'm trying to insert the node.

#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 :

You should use a debugger to step through your code. In your function

 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
    }

you are iterating till temp is nullptr. But at that point, doing temp->m_next is UB. You need to stop just before that. Also, you should link up ptr, not 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
    }

Of course, you also have to do an additional check in case the linked list is empty

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

You appear to be doing the opposite thing in your Display function. There you should iterate until temp is nullptr. Otherwise you won't print the last element.

Also, please don't do using namespace std;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=375813&siteId=1