试编写一个在循环双向链表中进行删除操作的算法,要求删除的结点是指定结点p的前趋结点(自己创建链表) 10
3个回答
展开全部
要删除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;
}
}
};
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的呗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询