循环链表 无法析构的问题
#include<iostream>class ListEntry //链表的结点
{
private:
double coef;
int expn;
ListEntry* preventry;
ListEntry* nextentry;
public:
ListEntry(double a_coef=0,int a_expn=0);
~ListEntry(){};
friend class List;
};
ListEntry::ListEntry(double a_coef,int a_expn)
{
coef=a_coef;
expn=a_expn;
preventry=0;
nextentry=0;
}
class List
{
private:
ListEntry* head;
int length;
public:
List();
~List(){};
void Delete(int i);
void InsFirst(ListEntry* q);
};
List::List()
{
head=new ListEntry;
head->preventry=head->nextentry=head;
length=0;
}
void List::InsFirst(ListEntry* q)
{
q->preventry=head;
q->nextentry=head->nextentry;
head->nextentry->preventry=q;
head->nextentry=q;
length++;
return;
}
void List::Delete(int i)
{
if(i>0 && i<=length)
{
ListEntry* p=head->nextentry;
while( --i)
{
p=p->nextentry;
}
p->preventry->nextentry=p->nextentry;
p->nextentry->preventry=p->preventry;
p=0;
delete p;
length--;
}
else
std::cerr<<"there is a error!"<<std::endl;
}
这是一个简单的循环链表的程序,谁能为我解释一下为什么只有加上红色部分才能正常运行呢???
[ 本帖最后由 wghost 于 2010-3-29 16:15 编辑 ]