简单链表类的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后还是会报错?求大神回答

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

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

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

你将指针指向了另外的地址,和已经不一样了,所以删除时报错

大概看了下,你的析构函数调用了delete,delete调用了length,而length丢出了异常。
应该是你delete有问题,没有正确处理链表中仅有头节点的情况下的删除。

 #include "stdafx.h"

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


int main()
{
    LI<int> aa;
    int v;
    for(int i=1;i<=10;i++)
    {
        aa.INSERT(1,i+2);
    }
    for(i=1;i<=10;i++)
    {
        aa.GETV(i,v);
        cout<<v<<endl;
    }
    return 0;
}

template
KNOTE::~KNOTE()
{
//delete next;
}

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