调试代码比自己写代码累多了。。。
先说我找出来的问题吧
void hugeNumber::reset_self(unsigned int tar)
{
cleanList();
hgNumNode* newNode= new hgNumNode(tar);
this->hgHead = newNode;
this->hgLast = newNode;
this->length++;
}
void hugeNumber::remove_zero_end()
{
if(this->hgHead == nullptr)
return;
remove_zero_end(this->hgHead);
}
删除后,尾节点的next指针没有置nullptr,且hgLast指针没有修改,
最小的修改方案:void hugeNumber::remove_zero_end()
{
if (this->hgHead == nullptr)
return;
remove_zero_end(this->hgHead,this->hgLast);
this->hgLast->next = nullptr;
}
bool hugeNumber::remove_zero_end(hgNumNode* parent,hgNumNode*& lastpointer)
{
if (parent == nullptr) {
lastpointer = nullptr;
return true;
}
else {
if (remove_zero_end(parent->next, lastpointer)) {
lastpointer = parent;
}
}
if (parent->value32 == 0)
{
delete parent;
parent = nullptr;
this->length--;
return true;
}
return false;
}
还有void hugeNumber::cleanList()
{
hgNumNode* delNode = this->hgHead;
while(delNode != nullptr)//不能再对象析构中调用delete删除相同对象,这样会导致无限调用析构的死循环递归
{
delete delNode;
delNode = delNode->next;
}
this->length = 0;
this->hgHead = nullptr;
this->hgLast = nullptr;
}
delete delNode后,再调用delNode会出现错误,建议改成void hugeNumber::cleanList()
{
hgNumNode* delNode = this->hgHead;
while (delNode != nullptr)//不能再对象析构中调用delete删除相同对象,这样会导致无限调用析构的死循环递归
{
hgNumNode* next = delNode->next;
delete delNode;
delNode = next;
}
this->length = 0;
this->hgHead = nullptr;
this->hgLast = nullptr;
}
仍然有错误,我重新实现个大数类吧0 0, |