]#include “stdafx.h”
#include<iostream>
using namespace std;
class Node
{
public:
int Value;
Node *pNext;
};
class Linklist
{
private:
Node *pHead;
public:
int Push(int IntValue);
Linklist();
~Linklist();
void display();
void link_list(Linklist li1,Linklist li2);//新加入的连接两个链表的函数
};
void Linklist::link_list(Linklist li1, Linklist li2)
{
int i, j, temp,count=0,a[15];
Node *head1=li1.pHead, *head2=li2.pHead;
for (i = 0; head1 != NULL; i++) { a[i] = head1->Value; head1 = head1->pNext; }
for (j = i+1; head2 != NULL; j++) { a[j] = head2->Value; head2 = head2->pNext; }
count = j-1;
for (i = 1; i<count; i++)
for (j = 0; j<count; j++)
{
if (a[j]<a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
pHead = li1.pHead;
Node *record =pHead;
Node *re = record;
for (i = 0; i < count; i++)
{
pHead->Value = a[i];
record->Value = pHead->Value;
if (i == 2) { pHead->pNext = li2.pHead; pHead = pHead->pNext; pHead->Value = a[i];}
else { pHead = pHead->pNext; }
record->pNext = pHead;
record = record->pNext;
}
pHead=re;
while (pHead != NULL) {
cout << pHead << ” “<<pHead->Value<<endl;
pHead=pHead->pNext;
}
}
Linklist::Linklist()
{
pHead = NULL;
}
Linklist::~Linklist()
{
while (pHead != NULL)
{
Node *pTmp = pHead->pNext;
cout << pHead <<” ”
<<pHead->pNext<<” ”
<<pHead->Value <<endl;
delete pHead;
pHead = pTmp;
}
}
void Linklist::display()
{
Node*ptr = pHead;
while (ptr != NULL)
{
cout << ptr->Value << endl;
ptr = ptr->pNext;
}
}
int Linklist::Push(int IntValue)
{
Node*NewNode = new Node;
NewNode->Value = IntValue;
NewNode->pNext = NULL;
Node*pTmp = pHead;
pHead = NewNode;
pHead->pNext = pTmp;
return IntValue;
}
int main()
{
Linklist linkA,linkB;
linkA.Push(9); linkA.Push(6); linkA.Push(1);
linkB.Push(7); linkB.Push(4); linkB.Push(2);
Linklist net;
net.link_list(linkA, linkB);
system(“pause”);
return 0;
}
在第一个红色部分的地方循环结束然后转向析构函数,析构函数执行完后调试发现直接运行到第一红色部分的末尾,然后又跳进了析构函数,这个时候 出现了错误。
#include<iostream>
using namespace std;
class Node
{
public:
int Value;
Node *pNext;
};
class Linklist
{
private:
Node *pHead;
public:
int Push(int IntValue);
Linklist();
~Linklist();
void display();
void link_list(Linklist li1,Linklist li2);//新加入的连接两个链表的函数
};
void Linklist::link_list(Linklist li1, Linklist li2)
{
int i, j, temp,count=0,a[15];
Node *head1=li1.pHead, *head2=li2.pHead;
for (i = 0; head1 != NULL; i++) { a[i] = head1->Value; head1 = head1->pNext; }
for (j = i+1; head2 != NULL; j++) { a[j] = head2->Value; head2 = head2->pNext; }
count = j-1;
for (i = 1; i<count; i++)
for (j = 0; j<count; j++)
{
if (a[j]<a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
pHead = li1.pHead;
Node *record =pHead;
Node *re = record;
for (i = 0; i < count; i++)
{
pHead->Value = a[i];
record->Value = pHead->Value;
if (i == 2) { pHead->pNext = li2.pHead; pHead = pHead->pNext; pHead->Value = a[i];}
else { pHead = pHead->pNext; }
record->pNext = pHead;
record = record->pNext;
}
pHead=re;
while (pHead != NULL) {
cout << pHead << ” “<<pHead->Value<<endl;
pHead=pHead->pNext;
}
}
Linklist::Linklist()
{
pHead = NULL;
}
Linklist::~Linklist()
{
while (pHead != NULL)
{
Node *pTmp = pHead->pNext;
cout << pHead <<” ”
<<pHead->pNext<<” ”
<<pHead->Value <<endl;
delete pHead;
pHead = pTmp;
}
}
void Linklist::display()
{
Node*ptr = pHead;
while (ptr != NULL)
{
cout << ptr->Value << endl;
ptr = ptr->pNext;
}
}
int Linklist::Push(int IntValue)
{
Node*NewNode = new Node;
NewNode->Value = IntValue;
NewNode->pNext = NULL;
Node*pTmp = pHead;
pHead = NewNode;
pHead->pNext = pTmp;
return IntValue;
}
int main()
{
Linklist linkA,linkB;
linkA.Push(9); linkA.Push(6); linkA.Push(1);
linkB.Push(7); linkB.Push(4); linkB.Push(2);
Linklist net;
net.link_list(linkA, linkB);
system(“pause”);
return 0;
}
在第一个红色部分的地方循环结束然后转向析构函数,析构函数执行完后调试发现直接运行到第一红色部分的末尾,然后又跳进了析构函数,这个时候 出现了错误。
解决方案
80
检查了能否释放了不是动态分配的内存