首页 > 解决方案 > c++单链表分段错误t

问题描述

我正在尝试编写一个制作单个链表的代码。我想将所有数组元素放入每个节点并链接它们。但是当我运行我的代码时,我不断收到分段错误错误。我不明白为什么会收到此错误。有人可以帮忙吗??谢谢!!

linked_list_main.cc

#include <iostream>
#include "linked_list.h"

int main() {
    int array[5];
    List<int> list(array, 5);
    std::cout << list;
    return 0;

} 
  
template <class T>                                                                                    
class Node {                                                                                    
    public:                                                                           
          T data;                                                                                       
          Node<T>* next;                                                                                                                                                                                    
};
这是我的linked_list.h 文件。

                                                                                                                                                                       
class List {                                                                                          
    private:                                                                                          
        Node<T> *head;                                                                                
    public:                                                                                           
        List() : head(NULL) {};                                                                       
        ~List() {                                                                                     
            Node<T>* ptr;                                                                             
            for(ptr = head; ptr == NULL; ptr = head->next)                                            
                delete ptr;                                                                           
            }                                                                                             
        List(T* arr, int n_nodes){                                                                    
            Node<T>* tmp = head;                                                                      
                for(int i = 0; i < n_nodes; i++ ) {                                                       
                    Node<T>* node = new Node<T>;                                                          
                    node->data = arr[i];                                                                  
                        if(tmp != NULL) {                                                                     
                            node->next = tmp;                                                                 
                            tmp = node;                                                                       
                        }                                                                                     
                  }                                                                                         
        }                                                                                             
                                                                                                                                                      
        friend std::ostream& operator<<(std::ostream& out, List<T>& rhs) {                              
            Node<T>* cur = rhs.head;                                                                                                                                                                     
            while(cur != NULL) {                                                                                                                                                                            
                if(cur->next == NULL)                                                                                                                                                                        
                    out << cur->data << " ";                                                                                                                                                                
                else                                                                                                                                                                                        
                    out << cur->data << ", ";                                                                                                                                                                 
                cur = cur->next;                                                                                                                                                                               
            }                                                                                         
        }                                                                                             
};

标签: c++singly-linked-list

解决方案


你需要改变这个

List(T* arr, int n_nodes){
    Node<T>* tmp = head;
    ...
}

对此

List(T* arr, int n_nodes){
    Node<T>* tmp = NULL;
    ...
    head = tmp;
}

指针很棘手,学习使用调试器。这将是您学习编程时度过的最美好的时光。


推荐阅读