求帮忙解决出现的堆栈溢出的错误,比较急

C++语言 码拜 7年前 (2017-04-19) 1014次浏览
]#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;
}
求帮忙解决出现的堆栈溢出的错误,比较急求帮忙解决出现的堆栈溢出的错误,比较急
在第一个红色部分的地方循环结束然后转向析构函数,析构函数执行完后调试发现直接运行到第一红色部分的末尾,然后又跳进了析构函数,这个时候 出现了错误。
解决方案

80

检查了能否释放了不是动态分配的内存

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求帮忙解决出现的堆栈溢出的错误,比较急
喜欢 (0)
[1034331897@qq.com]
分享 (0)