c++ - C++中的循环链表的析构函数?

标签 c++ linked-list destructor

当为此循环单链表调用“类LL”〜LL()的析构函数时,程序崩溃,而不是释放指针的堆空间。我怎么解决这个问题?

class Node {
public:
  int data;
  Node *next;
};

class LL {
private:
  Node *head, *tail;

public:
  LL() {
    head = NULL;
    tail = NULL;
  }
  // destructor
  ~LL() {
    Node *p = head;
    while (p->next != head) {
      p = p->next;
    }

    while (p != head) {
      p->next = head->next;
      delete head;
      head = p->next;
    }

    if (p == head) {
      delete head;
      head = nullptr;
    }
  }

  // circular singly Linked list

  void createLL() {
    int n, x;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> x;

      Node *t = new Node;
      t->data = x;
      t->next = NULL;

      if (head == NULL) {
        head = tail = t;
      } else {
        tail->next = t;
        tail = t;
      }
    }
    tail->next = head;
  }

最佳答案

链接列表存在一些问题。

  • 链表的析构函数假设head不为null(如果有可能)。在尝试清理内存之前,请确保检查head不为null。一旦完成,看起来您的原始析构函数应该可以工作。
  • 如果用户输入的大小小于或等于0,则函数createLL将调用未定义的行为。
    具体来说这行tail->next = head;
  • TreateLL用词不当,因为它实际上并未按预期意义“创建”新列表。内容不会被清除,因此n元素将附加到当前列表的末尾。
  • 另外,仅使用一个尾指针就可以创建循环链接列表。

  • 但是,使循环链接列表的实现生效的过程看起来像这样
    #include <iostream>
    using namespace std;
    
    class Node {
    public:
        int data;
        Node* next;
    };
    class LL {
    private:
        Node* head, * tail;
    public:
        LL() : head(nullptr),
            tail(nullptr) {
        }
        ~LL() {
            if (head) {
                Node* p = tail;
    
                while (p != head) {
                    p->next = head->next;
                    delete head;
                    head = p->next;
                }
                if (p == head) {
                    delete head;
                    head = nullptr;
                }
            }
        }
    
        void storeUserInput() {
            int n, x;
            cin >> n;
            if (n <= 0) {
                return; //no input to retrieve.
            }
            for (int i = 0; i < n; i++) {
                cin >> x;
                Node* t = new Node;
                t->data = x;
                t->next = nullptr;
    
                if (head == nullptr) {
                    head = tail = t;
                }
                else {
                    tail->next = t;
                    tail = t;
                }
            }
            tail->next = head;
        }
    
    };
    int main() {
        LL l;
        l.storeUserInput();
    
        char response;
        std::cin >> response;
    }
    

    看来您可以访问C++ 11或更高版本的编译器,如果是这样,则应使用nullptr代替NULL,因为它是确定的指针类型。查看更多here

    关于c++ - C++中的循环链表的析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62435720/

    相关文章:

    c++ - 在 Linux 环境中使用 C++ 的 BLE 服务器端实现

    C++ 使用递归函数创建链表

    c++ - lua - 类析构函数的延迟调用

    c++ - 为什么在删除涉及多重(虚拟)继承的对象时会出现无效 block 崩溃?

    c++ - 如何将多个操作数添加到 MDNode 或 LLVM 中的元数据?

    c++ - 使用带链表的复制构造函数

    c++ - 需要帮助理解列表容器

    c++ - 在 C++ 中以相反的顺序打印我的链表

    python - 为什么 Python 在销毁对象之前销毁类变量?

    c++ - 可以假设 0 为假而 1 为真吗?