c++的内存释放有关问题结合一道链表算法的代码校对望群主及们能帮忙
c++的内存释放问题结合一道链表算法的代码校对望群主及大虾们能帮忙
/*
问题主要还是内存释放的内容,不明白为什么偶这个程序只要用析构函数释放链表结点,运行时就出现了断言错误.
错误的线索我找到了是关于析构函数的问题,
可是至今还看不透,望各位能帮帮忙.
*/
#include <iostream>
using namespace std;
struct Node{
int ceof, exp;
Node* next;
Node* rear;
};
class Linklist {
public:
Linklist(int ceof[],int exp[],int n);
// ~Linklist();
void print();
Node* Getfirst() { return first; }
private:
Node* first;
};
Linklist::Linklist(int ceof[],int exp[],int n) { //创建头节点为空的链表
Node *m;//新元素指针
Node *pre; //跟尾指针
pre=first=new Node;
pre->rear=NULL;
for(int i=0;i<n;i++)
{
m=new Node;
m->ceof = ceof[i];
m->exp = exp[i];
m->rear = pre;
m->next = pre->next;
pre->next = m;
pre = m;
}
pre->next=NULL;
}
// 这里为什么加了清除内存残余的析构函数后就不能正常运行呢?
//
/*
Linklist::~Linklist(){
Node *p,*q;
p=first;
while(p) {
q=p;
p=p->next;
delete q;
}
}
//
*/
void Linklist::print(){
Node *m=first->next;
cout<<"\n系数:\t";
while(m){
cout<<m->ceof<<'\t';
m=m->next;
}
cout<<endl;
m=first->next;
cout<<"指数:\t";
while(m){
cout<<m->exp<<'\t';
m=m->next;
}
cout<<endl;
}
void sum(Linklist &a,Linklist &b) {
Node *p=a.Getfirst()->next;
Node *q=b.Getfirst()->next;
while(q && p)
{
if(p->exp<q->exp)
{ p=p->next; }
else if(p->exp>q->exp)
{
Node *v;
v = q->next;
q->next = p;
p->rear->next = v;
q = v;
}
else
{
p->ceof = q->ceof + p-> ceof;
if(p->ceof==0)
{
Node* ptmp=p;
p->rear->next = p->next;
p = p->next;
delete(ptmp);
}
else { p = p->next; }
Node* qtmp=q;
//就是这里的尾巴指针怀疑是对空指针进行了调用成员操作
if(q->rear) //故在这里加入判断语句
q->rear->next = q->next;
q = q->next;
delete(qtmp);
}
}
if(q) { p->rear->next = q; }
// if(b.Getfirst()->next) delete b.Getfirst()->next;
}
int main()
{
const int Na=5,Nb=5;
int a[Na] = {1,2,3,4,5};
int exp[Na] = {1,2,3,4,5};
int b[Nb] = {1,2,4,5};
int exp2[Nb] = {1,2,4,5};
Linklist demo(a,exp,5);
Linklist demo2(b,exp2,4);
demo.print();
demo2.print();
sum(demo,demo2);
demo.print();
return 0;
}
------解决方案--------------------
与析构无关,内存问题。
当你删除一个结点时,需要做两个工作,
前结点的后结点 = 后结点;
后结点的前结点 = 前结点;
你只做了前者。
/*
问题主要还是内存释放的内容,不明白为什么偶这个程序只要用析构函数释放链表结点,运行时就出现了断言错误.
错误的线索我找到了是关于析构函数的问题,
可是至今还看不透,望各位能帮帮忙.
*/
#include <iostream>
using namespace std;
struct Node{
int ceof, exp;
Node* next;
Node* rear;
};
class Linklist {
public:
Linklist(int ceof[],int exp[],int n);
// ~Linklist();
void print();
Node* Getfirst() { return first; }
private:
Node* first;
};
Linklist::Linklist(int ceof[],int exp[],int n) { //创建头节点为空的链表
Node *m;//新元素指针
Node *pre; //跟尾指针
pre=first=new Node;
pre->rear=NULL;
for(int i=0;i<n;i++)
{
m=new Node;
m->ceof = ceof[i];
m->exp = exp[i];
m->rear = pre;
m->next = pre->next;
pre->next = m;
pre = m;
}
pre->next=NULL;
}
// 这里为什么加了清除内存残余的析构函数后就不能正常运行呢?
//
/*
Linklist::~Linklist(){
Node *p,*q;
p=first;
while(p) {
q=p;
p=p->next;
delete q;
}
}
//
*/
void Linklist::print(){
Node *m=first->next;
cout<<"\n系数:\t";
while(m){
cout<<m->ceof<<'\t';
m=m->next;
}
cout<<endl;
m=first->next;
cout<<"指数:\t";
while(m){
cout<<m->exp<<'\t';
m=m->next;
}
cout<<endl;
}
void sum(Linklist &a,Linklist &b) {
Node *p=a.Getfirst()->next;
Node *q=b.Getfirst()->next;
while(q && p)
{
if(p->exp<q->exp)
{ p=p->next; }
else if(p->exp>q->exp)
{
Node *v;
v = q->next;
q->next = p;
p->rear->next = v;
q = v;
}
else
{
p->ceof = q->ceof + p-> ceof;
if(p->ceof==0)
{
Node* ptmp=p;
p->rear->next = p->next;
p = p->next;
delete(ptmp);
}
else { p = p->next; }
Node* qtmp=q;
//就是这里的尾巴指针怀疑是对空指针进行了调用成员操作
if(q->rear) //故在这里加入判断语句
q->rear->next = q->next;
q = q->next;
delete(qtmp);
}
}
if(q) { p->rear->next = q; }
// if(b.Getfirst()->next) delete b.Getfirst()->next;
}
int main()
{
const int Na=5,Nb=5;
int a[Na] = {1,2,3,4,5};
int exp[Na] = {1,2,3,4,5};
int b[Nb] = {1,2,4,5};
int exp2[Nb] = {1,2,4,5};
Linklist demo(a,exp,5);
Linklist demo2(b,exp2,4);
demo.print();
demo2.print();
sum(demo,demo2);
demo.print();
return 0;
}
------解决方案--------------------
与析构无关,内存问题。
当你删除一个结点时,需要做两个工作,
前结点的后结点 = 后结点;
后结点的前结点 = 前结点;
你只做了前者。
- C/C++ code
if(q->rear) //故在这里加入判断语句 { q->rear->next = q->next; if ( NULL != q->next) { q->next->rear = q->rear; } }