删除完整链表时出现分段错误

发布于 2024-09-25 18:25:15 字数 1660 浏览 4 评论 0原文

我正在尝试删除整个链接列表,但出现分段错误并且无法检测真正的问题是什么。当我尝试使用 gdb 进行调试时,它能够删除第一个节点,但在删除第二个节点时抛出分段错误。请建议我可能的原因是什么。

#include <iostream>
using namespace std;
class listNode
{
public:

    listNode *next;
    char data;

    listNode ()
    {
        next = NULL;
        data = '\0';
    }

    listNode (char alphabet)
    {
        next = NULL;
        data = alphabet;
    }

    ~listNode ()
    {
        delete next;

    }
};


class linkedList
{
public:

    listNode *head;
    void insert (listNode * node);
    trieNode *search (char alphabet);

    linkedList ();
    ~linkedList ();
    void printList ();

private:
    void deleteCompleteList ();

};

int
main ()
{
    linkedList testList;
    for (int i = 0; i < 10; i++)
    {
      listNode *temp = new listNode ('a' + i);
      testList.insert (temp);
    }

  testList.printList ();


}


linkedList::linkedList ()
{
  linkedList::head = NULL;
}

linkedList::~linkedList ()
{
  linkedList::deleteCompleteList ();
}

void
linkedList::printList ()
{
  listNode *temp = head;
  while ( temp )
  {
          cout << temp->data << endl;
          temp = temp->next;
  }

}

void
linkedList::insert (listNode *node)
{
    node->next = head;
    head = node;
}

trieNode *
linkedList::search (char alphabet)
{
    listNode *temp = head;
    while (temp)
    {
        if (temp->data == alphabet)
            return temp->down;
        temp = temp->next;
    }

    return NULL;
}

void
linkedList::deleteCompleteList ()
{
    listNode *temp ;
    while ( head )
    {
        temp = head;
        head = head->next;
        delete temp;
    }
}

I'm trying to delete whole linked list but getting segmentation fault and not able to detect what is the real problem. When I tried debugging using gdb, it is able to remove the first node but throw segmentation fault when deleting second node. Please suggest me what can be the possible reason.

#include <iostream>
using namespace std;
class listNode
{
public:

    listNode *next;
    char data;

    listNode ()
    {
        next = NULL;
        data = '\0';
    }

    listNode (char alphabet)
    {
        next = NULL;
        data = alphabet;
    }

    ~listNode ()
    {
        delete next;

    }
};


class linkedList
{
public:

    listNode *head;
    void insert (listNode * node);
    trieNode *search (char alphabet);

    linkedList ();
    ~linkedList ();
    void printList ();

private:
    void deleteCompleteList ();

};

int
main ()
{
    linkedList testList;
    for (int i = 0; i < 10; i++)
    {
      listNode *temp = new listNode ('a' + i);
      testList.insert (temp);
    }

  testList.printList ();


}


linkedList::linkedList ()
{
  linkedList::head = NULL;
}

linkedList::~linkedList ()
{
  linkedList::deleteCompleteList ();
}

void
linkedList::printList ()
{
  listNode *temp = head;
  while ( temp )
  {
          cout << temp->data << endl;
          temp = temp->next;
  }

}

void
linkedList::insert (listNode *node)
{
    node->next = head;
    head = node;
}

trieNode *
linkedList::search (char alphabet)
{
    listNode *temp = head;
    while (temp)
    {
        if (temp->data == alphabet)
            return temp->down;
        temp = temp->next;
    }

    return NULL;
}

void
linkedList::deleteCompleteList ()
{
    listNode *temp ;
    while ( head )
    {
        temp = head;
        head = head->next;
        delete temp;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-10-02 18:25:15

因为在 listNode d'tor 中它正在删除下一个节点。因此,当它要删除列表中的第二个节点时,它已经被删除了。

~listNode ()
{
    delete next;
}

将其更改为...

~listNode ()
{
}

Because in the listNode d'tor it's deleting the next node. So, when it goes to delete the second node in the list, it's already deleted.

~listNode ()
{
    delete next;
}

Change it to...

~listNode ()
{
}
半窗疏影 2024-10-02 18:25:15

您删除了 head->next 两次;一次在 listNode 析构函数中,一次在循环中。

You're deleting head->next twice; once in the listNode destructor, and once in the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文