双链表

  今天,我们一起用C++写一个双链表,具体代码如下:

DoubleList.h具体内容如下:

[cpp]  view plain copy
  1. #include "NodeList.h"  
  2.   
  3. template<typename Type> class DoublyList{  
  4. public:  
  5.     DoublyList() :head(new ListNode<Type>()){    //the head node point to itself  
  6.         head->m_pprior = head;  
  7.         head->m_pnext = head;  
  8.     }  
  9.     ~DoublyList(){  
  10.         MakeEmpty();  
  11.         delete head;  
  12.     }  
  13.   
  14. public:  
  15.     void MakeEmpty();   //make the list empty  
  16.     int Length();       //get the length of the list  
  17.     ListNode<Type> *Find(int n = 0);  //find the nth data  
  18.     ListNode<Type> * FindData(Type item);   //find the data which is equal to item  
  19.     bool Insert(Type item, int n = 0);     //insert item in the nth data  
  20.     Type Remove(int n = 0);   //delete the nth data  
  21.     Type Get(int n = 0);      //get the nth data  
  22.     void Print();           //print the list  
  23.   
  24. private:  
  25.     ListNode<Type> *head;  
  26. };  
  27.   
  28. template<typename Type> void DoublyList<Type>::MakeEmpty(){  
  29.     ListNode<Type> *pmove = head->m_pnext, *pdel;  
  30.     while (pmove != head){  
  31.         pdel = pmove;  
  32.         pmove = pdel->m_pnext;  
  33.         delete pdel;  
  34.     }  
  35.     head->m_pnext = head;  
  36.     head->m_pprior = head;  
  37. }  
  38.   
  39. template<typename Type> int DoublyList<Type>::Length(){  
  40.     ListNode<Type> *pprior = head->m_pprior, *pnext = head->m_pnext;  
  41.     int count = 0;  
  42.     while (1){  
  43.         if (pprior->m_pnext == pnext){  
  44.             break;  
  45.         }  
  46.         if (pprior == pnext&&pprior != head){  
  47.             count++;  
  48.             break;  
  49.         }  
  50.         count += 2;  
  51.         pprior = pprior->m_pprior;  
  52.         pnext = pnext->m_pnext;  
  53.     }  
  54.     return count;  
  55. }  
  56.   
  57. template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n = 0){  
  58.     if (n < 0){  
  59.         cout << "The n is out of boundary" << endl;  
  60.         return NULL;  
  61.     }  
  62.     ListNode<Type> *pmove = head->m_pnext;  
  63.     for (int i = 0; i < n; i++){  
  64.         pmove = pmove->m_pnext;  
  65.         if (pmove == head){  
  66.             cout << "The n is out of boundary" << endl;  
  67.             return NULL;  
  68.         }  
  69.     }  
  70.     return pmove;  
  71. }  
  72.   
  73. template<typename Type> bool DoublyList<Type>::Insert(Type item, int n){  
  74.     if (n < 0){  
  75.         cout << "The n is out of boundary" << endl;  
  76.         return 0;  
  77.     }  
  78.     ListNode<Type> *newnode = new ListNode<Type>(item), *pmove = head;  
  79.     if (newnode == NULL){  
  80.         cout << "Application Erorr!" << endl;  
  81.         exit(1);  
  82.     }  
  83.     for (int i = 0; i < n; i++){   //find the position for insert  
  84.         pmove = pmove->m_pnext;  
  85.         if (pmove == head){  
  86.             cout << "The n is out of boundary" << endl;  
  87.             return 0;  
  88.         }  
  89.     }  
  90.   
  91.     //insert the data  
  92.     newnode->m_pnext = pmove->m_pnext;  
  93.     newnode->m_pprior = pmove;  
  94.     pmove->m_pnext = newnode;  
  95.     newnode->m_pnext->m_pprior = newnode;  
  96.     return 1;  
  97. }  
  98.   
  99. template<typename Type> Type DoublyList<Type>::Remove(int n = 0){  
  100.     if (n < 0){  
  101.         cout << "The n is out of boundary" << endl;  
  102.         exit(1);  
  103.     }  
  104.     ListNode<Type> *pmove = head, *pdel;  
  105.     for (int i = 0; i < n; i++){   //find the position for delete  
  106.         pmove = pmove->m_pnext;  
  107.         if (pmove == head){  
  108.             cout << "The n is out of boundary" << endl;  
  109.             exit(1);  
  110.         }  
  111.     }  
  112.   
  113.     //delete the data  
  114.     pdel = pmove;  
  115.     pmove->m_pprior->m_pnext = pdel->m_pnext;  
  116.     pmove->m_pnext->m_pprior = pdel->m_pprior;  
  117.     Type temp = pdel->m_data;  
  118.     delete pdel;  
  119.     return temp;  
  120. }  
  121.   
  122. template<typename Type> Type DoublyList<Type>::Get(int n = 0){  
  123.     if (n < 0){  
  124.         cout << "The n is out of boundary" << endl;  
  125.         exit(1);  
  126.     }  
  127.     ListNode<Type> *pmove = head;  
  128.     for (int i = 0; i < n; i++){  
  129.         pmove = pmove->m_pnext;  
  130.         if (pmove == head){  
  131.             cout << "The n is out of boundary" << endl;  
  132.             exit(1);  
  133.         }  
  134.     }  
  135.     return pmove->m_data;  
  136. }  
  137.   
  138. template<typename Type> void DoublyList<Type>::Print(){  
  139.     ListNode<Type> *pmove = head->m_pnext;  
  140.     cout << "head";  
  141.     while (pmove != head){  
  142.         cout << "--->" << pmove->m_data;  
  143.         pmove = pmove->m_pnext;  
  144.     }  
  145.     cout << "--->over" << endl << endl << endl;  
  146.   
  147. }  
  148.   
  149. template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item){  
  150.     ListNode<Type> *pprior = head->m_pprior, *pnext = head->m_pnext;  
  151.     while (pprior->m_pnext != pnext && pprior != pnext){ //find the data in the two direction  
  152.         if (pprior->m_data == item){  
  153.             return pprior;  
  154.         }  
  155.         if (pnext->m_data == item){  
  156.             return pnext;  
  157.         }  
  158.         pprior = pprior->m_pprior;  
  159.         pnext = pnext->m_pnext;  
  160.     }  
  161.     cout << "can't find the element" << endl;  
  162.     return NULL;  
  163. }  
NodeList.h具体内容如下:

[cpp]  view plain copy
  1. template<typename Type> class DoublyList;  
  2.   
  3. template<typename Type> class ListNode{  
  4. private:  
  5.     friend class DoublyList < Type > ;  
  6.     ListNode() :m_pprior(NULL), m_pnext(NULL){}  
  7.     ListNode(const Type item, ListNode<Type> *prior = NULL, ListNode<Type> *next = NULL)  
  8.         :m_data(item), m_pprior(prior), m_pnext(next){}  
  9.     ~ListNode(){  
  10.         m_pprior = NULL;  
  11.         m_pnext = NULL;  
  12.     }  
  13. public:  
  14.     Type GetData();  
  15. private:  
  16.     Type m_data;  
  17.     ListNode *m_pprior;  
  18.     ListNode *m_pnext;  
  19. };  
  20.   
  21. template<typename Type> Type ListNode<Type>::GetData(){  
  22.     return this->m_data;  
  23. }  
main.cpp的内容如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include "DoubleList.h"  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     DoublyList<int> list;  
  9.     for (int i = 0; i < 20; i++){  
  10.         list.Insert(i * 3, i);  
  11.     }  
  12.     cout << "the Length of the list is " << list.Length() << endl;  
  13.     list.Print();  
  14.     for (int i = 0; i < 5; i++){  
  15.         list.Insert(3, i * 3);  
  16.     }  
  17.     cout << "the Length of the list is " << list.Length() << endl;  
  18.     list.Print();  
  19.   
  20.     list.Remove(5);  
  21.     cout << "the Length of the list is " << list.Length() << endl;  
  22.     list.Print();  
  23.   
  24.     cout << list.FindData(54)->GetData() << endl;  
  25.   
  26.     cout << "The third element is " << list.Get(3) << endl;  
  27.   
  28.     list.MakeEmpty();  
  29.     cout << "the Length of the list is " << list.Length() << endl;  
  30.     list.Print();  
  31.   
  32.     cin.get();  
  33.     return 0;  
  34. }  
运行效果如图1所示:

图1 运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值