欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

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

时间:2021-06-13 08:40:19|栏目: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; 
          for(;index<position;index++) 
              p=p->next ; 
          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<T> *head; 
  
}; 
template<typename T> void DLinkList<T>:: InsertElement (T data,size_t position) 
{ 
 DNode<T> *p=head->next; 
 size_t index=1; 
 for(;index<position;index++) 
     p=p->next; 
 if(p==NULL) 
     return; 
 //DNode<T> *s=new DNode<T>[sizeof(DNode<T>)]; 
 DNode<T> *s=new DNode<T>; 
 s->data=data; 
 s->next=p; 
 s->prior=p->prior; 
 p->prior->next=s; 
 p->prior=s; 
} 
template<typename T> void DLinkList<T>::CreateDLinkList(T a[],int n) 
{ 
 DNode<T>*p=head; 
 DNode<T>*s=NULL; 
 int i=0; 
 for(;i<n;i++) 
 { 
   // s=new DNode<T>[sizeof(DNode)]; 
     s=new DNode<T>; 
       s->data=a[i]; 
       p->next=s; 
       s->prior=p; 
       p=s; 
 } 
 s->next=NULL; 
} 
template<typename T>void DLinkList<T>::PrintDLinkList () 
{ 
   DNode<T> *p=head->next; 
   while(p) 
   { 
       cout<<p->data<<endl; 
       p=p->next; 
   } 
} 
int main() 
{ 
  int a[10]={1,2,3,4,5,6,7,8,9,10}; 
   DLinkList<int>Dlist; 
   Dlist.CreateDLinkList(a,10); 
   Dlist.DeleteElement (3); 
   Dlist.InsertElement(3,3); 
   Dlist.PrintDLinkList(); 
   return 0; 
} 
//double LinkList implement with C++ Class 
//************************************************************ 
/*#include<iostream> 
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"<<endl; 
       head=new Node[sizeof(Node)]; 
   }; 
   ~List() 
   { 
   if(head->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"<<endl; 
   } 
   void CreateDoubleLink(int a[],int n); 
   void DeleteElemData(int position); 
   void InsertElement(int data,int position); 
   void PrintDList(); 
private: 
   Node *head; 
}; 
void List::CreateDoubleLink (int a[],int n) 
{ 
    
  head->next =NULL; 
   Node *s,*p=head; 
   int i=0; 
   for(;i<n;i++) 
   { 
    s=new Node[sizeof(Node)]; 
       s->data =a[i]; 
       p->next =s; 
       s->prior =p; 
       p=s; 
   } 
   s->next =NULL; 
} 
void List::PrintDList() 
{ 
   Node *p=head->next ; 
   while(p) 
   { 
       cout<<p->data <<endl; 
       p=p->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(;i<position;i++) 
       p=p->next ; 
   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(;i<position;i++) 
       p=p->next ; 
   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; 
}*/ 
//************************************************************************************* 


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

上一篇:Visual Studio Code配置C/C++开发环境的教程图解

栏    目:C代码

下一篇:C++开发的Redis数据导入工具优化

本文标题:C++ 实现双向链表的实例

本文地址:http://www.codeinn.net/misctech/140766.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有