ssdut_209 2015-07-22 12:29 采纳率: 50%
浏览 2069
已采纳

简单链表类的DELETE函数

这是头文件:
#include
using namespace std;
template
class KNOTE
{
public:
A data;
KNOTE * next;
KNOTE(A data,KNOTE
* next);
virtual ~KNOTE();
};
template
KNOTE
::KNOTE(A data,KNOTE * next)
{
this->data=data;
this->next=next;
}
template
KNOTE
::~KNOTE()
{
delete next;
}
template
class LI
{
private:
KNOTE
* headp;
public:
LI();
~LI();
KNOTE
* GETP(int pos);
int length();
bool CLEAR();
bool SETV(int pos,A v);
bool GETV(int pos,A & v);
bool INSERT(int pos,A v);
bool DELETE(int pos,A & v);
};
template
LI
::LI()
{
headp=new KNOTE
(0,NULL);
}
template
LI
::~LI()
{
A temp;
while(length()>0)
{
DELETE(1,temp);
}
}
template
int LI
::length()
{
int len=0;
KNOTE
* tempp=headp->next;
while(tempp!=NULL)
{
len++;
tempp=tempp->next;
}
return len;
}
template
KNOTE
* LI::GETP(int pos)
{
KNOTE
* tempp=headp;
if(poslength())
return NULL;
else
{
int cur=0;
while(cur!=pos)
{
tempp=tempp->next;
cur++;
}
return tempp;
}
}
template
bool LI
::SETV(int pos,A v)
{
KNOTE
* tempp;
if(poslength())
return false;
else
{
tempp=GETP(pos);
tempp->data=v;
return true;
}
}
template
bool LI
::GETV(int pos, A & v)
{
KNOTE
* tempp;
if(poslength())
return false;
else
{
tempp=GETP(pos);
v=tempp->data;
return true;
}
}
template
bool LI
::CLEAR()
{
KNOTE
* tempp1=headp->next;
KNOTE
* tempp2;
while(tempp1!=NULL)
{
tempp2=tempp1->next;
delete temp1;
temp1=temp2;
}
}
template
bool LI
::INSERT(int pos,A v)
{
if(poslength()+1)
return false;
else
{
KNOTE
* pre_p;
KNOTE
* new_p;
pre_p=GETP(pos-1);
new_p=new KNOTE
(v,pre_p->next);
pre_p->next=new_p;
return true;
}
}
template
bool LI
::DELETE(int pos,A &v)
{
if(poslength())
return false ;
else
{
KNOTE
* pre_p;
KNOTE
* temp_p;
pre_p=GETP(pos-1);
//KNOTE
* pre_p1=headp->next;
temp_p=pre_p->next;
pre_p->next=temp_p->next;
v=temp_p->data;
//KNOTE
* pre_p2=headp->next;
delete temp_p;
//KNOTE
* pre_p3=headp->next;
return true;
}
}

以下是主函数
#include "head.h"
int main()
{
LI aa;
int v;
for(int i=1;i<=10;i++)
{
aa.INSERT(1,i+2);
}
for(int i=1;i<=10;i++)
{
aa.GETV(i,v);
cout<<v<<endl;
}
return 0;
}

程序可以运行并输出结果,但是结束时程序报错“简单链表.exe 中的 0x01281a47 处未处理的异常: 0xC0000005: 读取位置 0xfeeefef6 时发生访问冲突”。疑惑:为什么在 DELETE中 temp_p=pre_p->next;已经对头指针指针域改变,但deletetemp_p后还是会报错?求大神回答

  • 写回答

5条回答

  • threenewbee 2015-07-22 14:36
    关注

    晕,看了半天,总算知道了

     template<class  A>
    KNOTE<A>::~KNOTE()
    {
        //delete next;
    }
    

    这里你把next释放了,不应该释放。你注释掉,就好了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?