_CtrlsValidHeapPointer(Block)



我是c++的新手,最近我用c++学习数据结构。当错误发生时,我卡住了,我试图调试,但它仍然让我困惑。所以,这个错误是我试图实现两个操作连接和合并,我的终端显示的结果似乎在逻辑上是成功的。然而,错误就在下一秒出现。就像img一样:在此处输入图像描述调用堆栈

这是我的密码。希望有人能帮我解决这个问题,谢谢!

#pragma once
#include <iostream>
using namespace std;
class LinkedList;
class Node
{
private:
int data;
Node* next;
public:
friend class LinkedList;
};
class LinkedList
{
private:
Node* first;
Node* last;
Node* third;
public:
LinkedList()
{
first = nullptr;
last = nullptr;
third = nullptr;
}
LinkedList(int A[], int n);
~LinkedList();
void Display();
void RDisplay();//Recursive
void RDisplay(Node* p);
Node* getFirstNode();
int Count();
int RCount(Node* p);//Recursive
int Sum();
int RSum(Node* p);//Recursive
int Max();
int RMax(Node* p);//Recursive
int LSearch(int key); //linear search
int RSearch(int key);
int RSearch(Node* p, int key);
void Insert(int position, int x);
void InsertLast(int x);
void SortedInsert(int x);
int Delete(int position);
int isSorted();
void RemoveDuplicate();
void ReverseE();//Elements
void ReverseL();//Links
void RReverse();//Recursive
void RReverse(Node* q,Node *p);
void Concatenate(Node* second);
void Merge(Node *second);
int Mid();
int isLoop();
};
//cpp file
#include "Linkedlist.h"
#include <iostream>
using namespace std;
LinkedList::LinkedList(int A[], int n)
{
Node* t ;
first = new Node;
first->data = A[0];
first -> next = nullptr;
last = first;
for (int i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = nullptr;
last->next = t;
last = t;
}
}
LinkedList ::~LinkedList()
{
Node* p = first;
while (first)
{
first = first->next;
delete p;
p = first;
}
}
void LinkedList::Merge(Node* second)
{
Node* last;
if (first->data < second->data)
{
third =  first;
last = first;
first = first->next;
last->next = nullptr;
}
else
{
third = last = second;
second = second->next;
last->next = NULL;
}
while (first != NULL && second != NULL)
{
if (first->data < second->data)
{
last->next = first;
last = first;
first = first->next;
last->next = NULL;
}
else
{
last->next = second;
last = second;
second = second->next;
last->next = NULL;
}
}
if (first != NULL)
{
last->next = first;
first = third;
}
else
{
last->next = second;
first = third;
}
}
//concatenate
void LinkedList::Concatenate(Node* second)
{
Node* p = first;
while (p->next != NULL)
{
p = p->next;
}
p->next = second;
}
void LinkedList::Display()
{
Node* p = first;
while (p != nullptr)
{
cout << p->data << " -> ";
p = p->next;
}
}

Node* LinkedList::getFirstNode()
{
return first;
}
#include <iostream>
#include "Linkedlist.h"
#include "Circular.h"
#include "Doubly.h"
#include "CircularDoubly.h"
int main()
{
int A[] = { 1,2,3,4,6,7 };
int B[] = { 5,6,7,8 };
LinkedList c1(A,6);
LinkedList c2(B, 4);
c1.Concatenate(c2.getFirstNode());
c1.Display();
}

我应该提供更多细节吗?

堆栈调用堆栈调用2

是的,这是析构函数的问题,在Concatenate之后,c2列表被附加到c1列表中,元素被销毁两次,一次在c1的析构函数中,第二次在c2的析构器中。这是一个内存问题,可能会导致崩溃。

我建议这样修复:将所有权转移到c1,然后我们可以避免两次销毁

Node* LinkedList::ReleaseFirst() {
Node* tmp = first;
first = nullptr;
return tmp;
}
c1.Concatenate(c2.ReleaseFirst());

在线演示

相关内容

  • 没有找到相关文章

最新更新