0
我写了这个函数,将另一个链表插入到现有链表中。当我输出函数中的“this”对象的值时,输出是正确的。但是,程序在最后调用析构函数时会遇到运行时错误。我认为运行时错误是由于有2个指针指向相同的地址造成的;因此当一个人被分配时,另一个人成为悬挂指针。在链表中插入一个链表
有什么办法可以插入另一个链接列表到现有的链接列表(在中间),而不会导致这个问题?
void List::insert(const List& otherList, const int &index)
{
Node* insertion = head;
int x = index;
while (x > 0){
insertion = insertion->next;
x--;
}
if (index == 0){ //this works fine
otherList.tail->next = insertion;
*this = otherList; /*I implemented a copy ctor
that performs deep copy
so this is also fine */
}
else{ // this block causes problems
Node* tmp = insertion->next;
insertion->next = otherList.head;
otherList.tail->next = tmp;
}
cout << "after the copy\n" << (*this) << endl;
}
插入后,这两个列表引用相同的节点,这就是为什么你的代码崩溃。一个析构函数释放共享节点,然后另一个析构函数尝试再次释放它们。您需要从源列表中物理删除节点,以使它们仅存在于目标列表中,否则您需要深度复制节点的* data *并且不要复制节点指针本身。 –
@Remy,我如何删除源列表,因为它是通过常量引用传递的...我无法修改它 –
因此,在这最后,你应该有两个列表 - 一个列表是两个列表的连接,第二个列表没有修改?这在你的问题中并不清楚。如果第二个列表没有被修改,为什么不写一个简单的循环,为传入的列表中的每个节点调用(this hope to you coded it)'this-> insertNode()'或类似的函数? – PaulMcKenzie