C++ 實(shí)現(xiàn)雙向鏈表的實(shí)例
雙向鏈表C++ 的實(shí)現(xiàn)
本文是通過(guò)C++ 的知識(shí)實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的雙向鏈表,這里不多說(shuō) 了,代碼注釋很清楚,
實(shí)現(xiàn)代碼:
//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>//此處必須為實(shí)現(xiàn)的類型,當(dāng)然以派生類的模板類型也可以,但是不能是Type { public: void Append(const T &);//這邊也需要是當(dāng)前類的類型,不能為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) {//可以通過(guò)重載delete運(yùn)算符來(lái)達(dá)到這個(gè)效果,則直接用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運(yùn)算符來(lái)達(dá)到這個(gè)效果,則直接用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; }*/ //*************************************************************************************
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐
本文主要介紹了C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐,根據(jù)節(jié)點(diǎn)插入位置的不同,AVL樹的旋轉(zhuǎn)分為四種,下面就來(lái)介紹一下,感興趣的可以了解一下2024-03-03C++ OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C++和OpenCV實(shí)現(xiàn)灰度圖蒙版GrayMask,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定參考價(jià)值,需要的可以參考一下2022-05-05C語(yǔ)言實(shí)現(xiàn)數(shù)獨(dú)小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)數(shù)獨(dú)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Java?C++?算法題解leetcode669修剪二叉搜索樹示例
這篇文章主要為大家介紹了Java?C++?算法題解leetcode669修剪二叉搜索樹示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09C++實(shí)現(xiàn)LeetCode(132.拆分回文串之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(132.拆分回文串之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07c語(yǔ)言中用字符串?dāng)?shù)組顯示菜單的解決方法
本篇文章是對(duì)c語(yǔ)言中用字符串?dāng)?shù)組顯示菜單的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++實(shí)現(xiàn)LeetCode(91.解碼方法)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(91.解碼方法),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07深入解析C++編程中__alignof 與__uuidof運(yùn)算符的使用
這篇文章主要介紹了C++編程中__alignof 與__uuidof運(yùn)算符的使用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01