C++ 实现双向链表的实例实现双向链表的实例
双向链表C++ 的实现
本文是通过C++ 的知识实现数据结构中的双向链表,这里不多说 了,代码注释很清楚,
实现代码:实现代码:
//double LinkList implement with C++ template
#include<iostream>
using namespace std;
/*template<typename Type>
class DBListADT
{
public:
virtual void Append(const Type &)=0;
virtual void Insert(const Type &)=0;
virtual void Remove(const Type &)=0;
};
template<typename T>
class DLinkListNode:public DBListADT<typename T>//此处必须为实现的类型,当然以派生类的模板类型也可以,但是不能是Type
{
public:
void Append(const T &);//这边也需要是当前类的类型,不能为Type
void Insert(const T &);
void Remove(const T &);
};*/
template<typename T>class DLinkList;
template<typename Type>
class DNode
{
friend class DLinkList<Type>;//指定前需声明
public:
DNode(){next=NULL;prior=NULL;}
~DNode(){}
private:
DNode *next;
DNode *prior;
Type data;
};
template<typename T>
class DLinkList
{
public:
DLinkList()
{
// head=new DNode<T>[sizeof(DNode<T>)];
head=new DNode<T>;
}
~DLinkList()
{
if(head->next==NULL)
delete head;
else
{
DNode<T> *p=head->next;
DNode<T>*s=NULL;
while(p)
{
s=p->next ;
delete p;
p=s;
}
}
}
void DeleteElement(size_t position)
{
DNode<T> *p=head->next;
size_t index=1;