【发布时间】:2012-02-23 04:25:35
【问题描述】:
我不知道我的问题出在哪里,但我无法清除这个单链表。我已经尝试了我能想到的一切。我正在用一个包含一个元素的列表(实际上是一个链表的哈希表)对其进行测试,但我无法让我的“erase()”函数工作(它会清理整个列表并删除每个节点)。如果你能看看这个并指出我正确的方向。
节点结构
struct Node
{
string m_str;
Node *m_pNext;
Node(void) {m_pNext = NULL;}
};
Node *m_pHead;
擦除功能
Void LLString::erase (void){
if (!m_pHead)
{
return;
}
Node *temp = m_pHead;
while (temp)
{
temp = m_pHead; // The error allways shoes up around her
if (temp->m_pNext) // It has moved around a little as I have tried
{ // different things. It is an unhanded exception
m_pHead = temp->m_pNext;
}
temp->m_pNext = NULL;
delete temp;
}
}
我的添加功能
void LLString::add (string str)
{
Node *nNode = new Node;
nNode -> m_str = str;
nNode ->m_pNext = m_pHead;
m_pHead = nNode;
}
我目前与该程序一起使用的唯一其他功能是此功能将所有内容发送到文件。 (在擦除功能之前使用)
void LLString::toFile (void)
{
ofstream fout;
fout.open ("stringData.txt",ios::app);
Node* temp = m_pHead;
while (temp)
{
fout << temp->m_str << endl;
temp = temp->m_pNext;
}
fout.close();
}
再次,如果您知道为什么删除不起作用,请指出来。
谢谢
【问题讨论】:
标签: c++ struct linked-list erase