删除完整链表时出现分段错误
我正在尝试删除整个链接列表,但出现分段错误并且无法检测真正的问题是什么。当我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为在 listNode d'tor 中它正在删除下一个节点。因此,当它要删除列表中的第二个节点时,它已经被删除了。
将其更改为...
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.
Change it to...
您删除了
head->next
两次;一次在listNode
析构函数中,一次在循环中。You're deleting
head->next
twice; once in thelistNode
destructor, and once in the loop.