C++实现双向链表的实例

双向链表C++ 的实现    

创新互联公司长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为光明企业提供专业的网站建设、网站设计,光明网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

               本文是通过C++ 的知识实现数据结构中的双向链表,这里不多说 了,代码注释很清楚,

实现代码:

//double LinkList implement with C++ template 
#include 
using namespace std; 
/*template 
class DBListADT 
{ 
   public: 
       virtual void Append(const Type &)=0; 
       virtual void Insert(const Type &)=0; 
       virtual void Remove(const Type &)=0; 
}; 
template 
class DLinkListNode:public DBListADT//此处必须为实现的类型,当然以派生类的模板类型也可以,但是不能是Type 
{ 
   public: 
       void Append(const T &);//这边也需要是当前类的类型,不能为Type 
       void Insert(const T &); 
       void Remove(const T &); 
 
 
};*/ 
templateclass DLinkList; 
template 
class DNode 
{ 
       friend class DLinkList;//指定前需声明 
   public: 
       DNode(){next=NULL;prior=NULL;} 
       ~DNode(){} 
   private: 
       DNode *next; 
     DNode *prior; 
       Type data; 
}; 
template 
class DLinkList 
{ 
  public: 
       DLinkList() 
       { 
       //   head=new DNode[sizeof(DNode)]; 
          head=new DNode; 
       } 
       ~DLinkList() 
       { 
          if(head->next==NULL) 
              delete head; 
          else 
          { 
              DNode *p=head->next; 
              DNode*s=NULL; 
              while(p) 
              { 
                 s=p->next ; 
                 delete p; 
                 p=s; 
              } 
          } 
       } 
       void DeleteElement(size_t position) 
       { 
          DNode *p=head->next; 
          size_t index=1; 
          for(;indexnext ; 
          if(p==NULL) 
              return ; 
          p->prior ->next =p->next ; 
          p->next ->prior =p->prior ; 
          delete p; 
       } 
       void InsertElement(T data,size_t position); 
       void CreateDLinkList(T a[],int n); 
       void PrintDLinkList(); 
   private: 
       DNode *head; 
  
}; 
template void DLinkList:: InsertElement (T data,size_t position) 
{ 
 DNode *p=head->next; 
 size_t index=1; 
 for(;indexnext; 
 if(p==NULL) 
     return; 
 //DNode *s=new DNode[sizeof(DNode)]; 
 DNode *s=new DNode; 
 s->data=data; 
 s->next=p; 
 s->prior=p->prior; 
 p->prior->next=s; 
 p->prior=s; 
} 
template void DLinkList::CreateDLinkList(T a[],int n) 
{ 
 DNode*p=head; 
 DNode*s=NULL; 
 int i=0; 
 for(;i[sizeof(DNode)]; 
     s=new DNode; 
       s->data=a[i]; 
       p->next=s; 
       s->prior=p; 
       p=s; 
 } 
 s->next=NULL; 
} 
templatevoid DLinkList::PrintDLinkList () 
{ 
   DNode *p=head->next; 
   while(p) 
   { 
       cout<data<next; 
   } 
} 
int main() 
{ 
  int a[10]={1,2,3,4,5,6,7,8,9,10}; 
   DLinkListDlist; 
   Dlist.CreateDLinkList(a,10); 
   Dlist.DeleteElement (3); 
   Dlist.InsertElement(3,3); 
   Dlist.PrintDLinkList(); 
   return 0; 
} 
//double LinkList implement with C++ Class 
//************************************************************ 
/*#include 
using namespace std; 
class Node 
{ 
   friend class List; 
public: 
  //Node(int a):next(NULL),prior(NULL),data(a){} 
   Node(){} 
private: 
  Node *next; 
   Node *prior; 
   int data; 
}; 
class List 
{ 
public: 
   List() 
   { 
       cout<<"create head DBLinkList"<next==NULL) 
     { 
        delete head; 
     } 
     else 
     { 
        Node *p=head->next; 
        Node *s; 
        delete head; 
        while(p) 
        { 
            s=p->next ; 
            delete p; 
            p=s; 
        } 
     } 
     cout<<"destructor called to clear DBLinkList"<next =NULL; 
   Node *s,*p=head; 
   int i=0; 
   for(;idata =a[i]; 
       p->next =s; 
       s->prior =p; 
       p=s; 
   } 
   s->next =NULL; 
} 
void List::PrintDList() 
{ 
   Node *p=head->next ; 
   while(p) 
   { 
       cout<data <next ; 
   } 
} 
void List::DeleteElemData(int position) 
{//可以通过重载delete运算符来达到这个效果,则直接用delete就OK了 
  Node *p=head->next ; 
   //while(p!=NULL&&p->data !=data) 
   //   p=p->next ; 
   int i=1; 
   for(;inext ; 
   if(p==NULL) 
       return ; 
   p->prior ->next =p->next ; 
   p->next ->prior =p->prior ; 
   delete p; 
} 
void List::InsertElement (int data,int position) 
{//可以重载new运算符来达到这个效果,则直接用new就OK了 
  Node *p=head->next ; 
   int i=1; 
   for(;inext ; 
   Node *s=new Node[sizeof(Node)]; 
   s->data =data; 
   s->prior =p->prior ; 
   s->next =p; 
   p->prior ->next =s; 
   p->prior =s; 
} 
int main() 
{ 
   List Dlist; 
   int a[10]={1,2,3,4,5,6,7,8,9,10}; 
   Dlist.CreateDoubleLink (a,10); 
   Dlist.DeleteElemData(3); 
   Dlist.InsertElement (3,3); 
   Dlist.PrintDList (); 
   return 0; 
}*/ 
//************************************************************************************* 


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


当前标题:C++实现双向链表的实例
URL链接:http://myzitong.com/article/joocpg.html