Я получаю ошибку сегментации (ядро сброшено) при реализации связанных списков - PullRequest
0 голосов
/ 21 сентября 2019

Это код, с которым у меня возникла проблема.Я считаю, что проблема связана с функцией Insert_at_end, но я не уверен, в чем проблема.Если кто-нибудь знает, что является причиной ошибки сегментации, это будет очень цениться.Проблема связана с main.cpp Main.cpp

#include <iostream>
using namespace std;
#include "list.h"

int main()
{
        // instantiate a List class (the constructor takes NO arguments)
        class List{
                public:
                        int length;

                        List(){
                                this->length = 0;
                                this->m_head = NULL;
                        }

                        ~List(){
                                cout << "deleted" << endl;
                        }
                        void insert_at_end(int value){
                                Node *node = new Node;
                                node->m_value = value;
                                node->m_next = new Node;;
                                Node *ptr = m_head;
                                if (m_head == NULL){
                                        m_head = node;
                                }
                                while (node->m_next != NULL){
                                        ptr = ptr->m_next;
                                }
                                ptr->m_next = node;
                        }

                        void print(){
                                Node* m_head = this->m_head;
                                while (m_head){
                                        cout << "\n" << m_head->m_value;
                                        m_head = m_head->m_next;
                                }
                                cout << endl;
                        }
                private:
                        class Node{
                                public:
                                        Node(){
                                                m_value;
                                                m_next;
                                        }
                                        Node(int value, Node *next){
                                                m_value = value;
                                                m_next = next;
                                        }
                                        int m_value;
                                        Node *m_next;
                        };
                        Node *m_head;
        };


        int x = 0;
        List *list = new List();
        while (!cin.eof()){
                cin >> x;
                list->insert_at_end(x);
        }
        list->print();
        list->sum();
        return 0;
}
...