繁体   English   中英

C ++链接列表打印崩溃

[英]C++ Linked List printing Crash

我不是编程新手,而是学习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. 在构造函数中:您正在创建一个局部变量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; } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM