【问题标题】:How to correctly delete nodes in singly-linked-list c++如何正确删除单链表c ++中的节点
【发布时间】:2019-08-10 06:02:37
【问题描述】:

我无法通过析构函数删除我的列表。我有一个类 List(它充当列表的标题)和类 Element(它实际上是一个节点)。如何删除 List 析构函数中的 Element 链?

这是 C++ 中用于面试的自定义实现。我正在尝试使用 delete_list() 函数在列表生存期到期后删除整个列表。我不确定如何完全解决这个问题。我还有一个重载的运算符

   /****************************************************************
   * Template Element class, these make up the chain for the list
   *****************************************************************/
   template<class T>
   class Element{
   public:
       T element;
       Element* next;

       Element() { element = 0; next = NULL; }
       Element(const Element<T>& other): element(other.element), next(other.next) {};
          ~Element() {  next = nullptr; }
   };

   /****************************************************************
   * Template List class
   *****************************************************************/
   template<class N>
   class List{
       Element<N>* first;
       unsigned size;

   public:

   /****************************************************************
   * Constructors and Destructors
   *****************************************************************/
   List() { size = 0; first = nullptr; };
   /* Constructor with input - for memory preallocation */
   List(Element<N>* mem_destination){ size = 0; first = mem_destination; };
   List(const List<N>& other): first(other.first), size(other.size) {};
   ~List(){ delete_list(); }
    void delete_list()
   {

       Element<N>* iter;
       size = 0;

       while(first != nullptr)
       {

           iter = first->next;
           delete first;
           first = iter;

           }
       if(iter != nullptr)
       {
           delete iter;
           iter = nullptr;
       }
       if(first != nullptr)
           {
           delete first;
           first = nullptr;
       }


   }
   friend std::ostream& operator<< (std::ostream& os, const List lista){
       Element<N>* iter = lista.first;
       os << "size: " << lista.size << std::endl;
       while(iter != NULL){
           os << iter->element << std::endl;
           iter = iter->next;
       }
       if(iter != nullptr)
           iter = nullptr;
       return os;
   }
...

【问题讨论】:

  • 您的循环会删除所有节点。为什么你认为你需要删除更多? (查看您最喜欢的书中有关指针的部分。)

标签: c++ memory-management linked-list


【解决方案1】:

代码太多,这个可行

void delete_list()
{
    size = 0;
    while (first != nullptr)
    {
        Element<N>* next = first->next;
        delete first;
        first = next;
    }
}

您的版本在第一个循环中是可以的,但由于某种原因,您决定必须删除 iter,即使它只是一个工作变量,而不是列表的一部分,然后您决定删除 first再次。我不知道你为什么觉得有必要这样做。

顺便说一句,这是一个严重的错误

List(const List<N>& other): first(other.first), size(other.size) {}

当您复制一个列表时,您需要分配一组新节点,否则您最终会得到两个列表共享同一组节点,并且无法确定何时可以安全删除节点。您可能需要阅读rule of three

【讨论】:

  • 非常感谢,我还是c++的新手。由于那个错误的复制构造函数,我遇到了很大的问题,我的 delete_list() 删除了原始节点链。再次感谢:D
猜你喜欢
  • 2014-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多