要删除p节点的前驱,先定义一个节点q为p的前驱节点。有如下关系:
q->pre->next=p;
p->pre=q->pre;
然后删除q节点就可以了。
# include <iostream.h>
# include <assert.h>
template <class Type>
struct DbNode
{
Type data;
DbNode<Type> *next,*pre;
DbNode(DbNode<Type> *ptr1 = NULL,DbNode<Type> *ptr2 = NULL):next(ptr1),pre(ptr2)
{}
DbNode(Type x,DbNode<Type> *ptr1 = NULL,DbNode<Type> *ptr2 = NULL):data(x),next(ptr1),pre(ptr2)
{}
};
template <class Type>
class DbList
{
public:
DbList()
{
head = new DbNode<Type>;
head->pre = head->next = head;
}
DbList(const DbList<Type> &dl)
{ }
~DbList()
{
Destory();
}
public:
void DeleteNode(DBNode<Type> *p)
{
DBNode<Type> *q=p->pre;
q->pre->next=p;
p->pre=q->pre;
delete q;
}
private:
DbNode<Type> *head;
void Destory()
{
DbNode<Type> *p = head->next;
while(p != head)
{
head->next = p->next;
p->next->pre = head;
delete p;
p = head->next;
}
}
};
追问帮忙编个c的呗