C ++链接列表打印崩溃

时间:2017-02-01 16:03:47

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

我不是编程新手,而是学习C ++。为此,我实施了"标准" C ++语言中的数据结构。我从链接列表开始。我理解它们是如何工作的。但是,当我尝试打印出列表时,它不应该在它应该停止时停止。我将最后一个指针设置为nullptr以及所有这些,并且已经在互联网上大量研究了这个问题,但是我无法找到与其他人不同的东西。这是代码:

template<typename T>
void LinkedList<T>::print_list(){

    list_node<T> *pos = this->start;

    while(pos != nullptr){
       cout << "PRInting" <<pos->data<<endl <<pos->next;

        pos = pos->next;
    }
}

这里是完整代码:

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED

#include <iostream>

using std::cout;
using std::endl;
template <class T>
struct list_node{
    T data;
    list_node<T> *next;
};
template <class T>
class LinkedList{
private:
    list_node<T> *start;

public:
    LinkedList();
    LinkedList(T firstData);
    ~LinkedList();
    void insert_item(T item);
    void delete_item(T item);
    list_node<T>* search_list();
    void print_list();
};



//constructors and destructor
template <typename T>
LinkedList<T>::LinkedList(){
    this->start = nullptr;
}
template <typename T>
LinkedList<T>::LinkedList(T firstData){
    list_node<T> newNode = {
        firstData,
        nullptr
    };
    this->start = &newNode;
    cout <<"Constructor" <<this->start->data<<endl;
}
template <typename T>
LinkedList<T>::~LinkedList(){
    this->start = nullptr;
}

//Debugging print function
template<typename T>
void LinkedList<T>::print_list(){
    list_node<T> *pos = this->start;
    while(pos != nullptr){
        cout << "PRInting" <<pos->data<<endl <<pos->next;
        pos = pos->next;
    }
    //cout << pos->data;
}


//Operations on Linked Lists
template <typename T>
void LinkedList<T>::insert_item(T item){
    list_node<T> *insertNode;
    insertNode->data = item;
    insertNode->next = this->start;
    this->start = insertNode;
    cout << "After insert " <<this->start->data << '\n' << this->start->next->data<<endl;
}

#endif // LINKEDLIST_H_INCLUDED

1 个答案:

答案 0 :(得分:4)

您的代码中存在2个与节点插入有关的不同问题。

  1. 在构造函数中:您正在创建局部变量newNode,并将其内存地址存储在this->start中。但是,newNode对象在离开构造函数的范围时将被销毁,并且尝试取消引用它将导致UB(未定义的行为)。您应该动态分配节点,因此一旦离开作用域就不会被销毁:

    LinkedList<T>::LinkedList(T firstData){
        this->start = new list_node<T>;
        this->start->data = firstData;
        this->start->next = nullptr;
        cout <<"Constructor" <<this->start->data<<endl;
    }
    
  2. insert_item过程中:您正在取消引用本地指针insertNode,即使没有为其分配实际内存,并且取消引用它也会导致UB。正确的版本看起来像:

    template <typename T>
    void LinkedList<T>::insert_item(T item){
        list_node<T> *insertNode = new list_node<T>;
        insertNode->data = item;
        insertNode->next = this->start;
        this->start = insertNode;
        cout << "After insert " <<this->start->data << '\n' << this->start->next->data<<endl;
        }
    
  3. 现在,由于我们正在进行动态内存分配,我们需要在析构函数中释放它(C ++没有垃圾收集),所以简单地将start分配给nullptr不会够了:

    template <typename T>
        LinkedList<T>::~LinkedList(){
            list_node<T> *pos = this->start;
            while (pos != nullptr){
                list_node<T>* nextPos = pos->next;
                delete pos;
                pos = nextPos;
            }
        }