Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264840
  • 博文数量: 45
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 553
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-22 17:53
文章分类

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-04-28 16:38:17

DoubleList.h

点击(此处)折叠或打开

  1. #ifndef DOUBLELIST_H
  2. #define DOUBLELIST_H
  3. #include <iostream>
  4. using std::cout;
  5. using std::endl;
  6. using std::ostream;

  7. template <typename T>
  8. class DoubleList;

  9. template <typename T>
  10. ostream & operator << (ostream & out, const DoubleList<T> & dl);
  11. template<class T>
  12. class DoubleNode{
  13.     friend DoubleList<T>;
  14. private:
  15.     T data;
  16.     DoubleNode<T> *left,*right;
  17. };

  18. template<class T>
  19. class DoubleList{
  20.     friend ostream & operator << <T> (ostream & out, const DoubleList<T> & dl);
  21. public:
  22.     DoubleList() {LeftEnd = RightEnd = 0;}
  23.     ~DoubleList();
  24.     int Length() const;
  25.     bool IsEmpty() const {return LeftEnd == 0;}
  26.     bool Find(int k, T & x) const;
  27.     int Search(const T & x) const;
  28.     DoubleList<T> & Delete(int k,const T & x);
  29.     DoubleList<T> & Insert(int k, const T & x);
  30.     void Output(ostream & out) const;
  31. private:
  32.     DoubleNode<T> *LeftEnd,*RightEnd;
  33. };

  34. template<typename T>
  35. DoubleList<T>::~DoubleList()
  36. {
  37.     DoubleNode<T> *next;
  38.     while(LeftEnd)
  39.     {
  40.         next = LeftEnd->right;
  41.         delete LeftEnd;
  42.         LeftEnd = next;
  43.     }
  44.     /*可替换部分
  45.     while(RightEnd)
  46.     {
  47.         next = RightEnd->left;
  48.         delete RightEnd;
  49.         RightEnd = next;
  50.     }
  51.     */
  52. }

  53. template<typename T>
  54. int DoubleList<T>::Length() const
  55. {
  56.     DoubleNode<T> * current = LeftEnd;
  57.     int len = 0;
  58.     while (current)
  59.     {
  60.         len++;
  61.         current = current->right;
  62.     }
  63.     /*可替换部分
  64.     DoubleNode<T> * current = RightEnd;
  65.     int len = 0;
  66.     while(current)
  67.     {
  68.         len++;
  69.         current = current->left;
  70.     }
  71.     */
  72.     return len;
  73. }

  74. template<typename T>
  75. bool DoubleList<T>::Find(int k, T & x) const
  76. {//找出第k个节点值,返回给x
  77.     if (k<0) return false;
  78.     DoubleNode<T> *current = LeftEnd;
  79.     int index = 0;
  80.     while (index < k && current)
  81.     {
  82.         current = current->right;
  83.         index++;
  84.     }
  85.     if (current)
  86.     {
  87.         x = current->data;
  88.         return true;
  89.     }
  90.     return false;
  91. }

  92. template <typename T>
  93. int DoubleList<T>::Search(const T & x) const
  94. {
  95.     DoubleNode<T> * current = LeftEnd;
  96.     int index = 0;
  97.     while (current && current->data != x)
  98.     {
  99.         current = current->right;
  100.         index++;
  101.     }

  102.     /*
  103.     DoubleNode<T> * current = RightEnd;
  104.     int index 0;
  105.     while (current && current->data != x)
  106.     {
  107.         current = current->left;
  108.         index++;
  109.     }
  110.     */
  111.     if(current) return index;
  112.     return -1;
  113. }

  114. template <typename T>
  115. void DoubleList<T>::Output(ostream & out) const
  116. {
  117.     DoubleNode<T> * current;
  118.     for (current = LeftEnd; current;current = current->right)
  119.         out << current->data<<" ";
  120.     /*for (current= RightEnd; current;current = current->left)
  121.         out<< current->data<<" ";*/
  122. }

  123. template <typename T>
  124. ostream & operator << (ostream & out, const DoubleList<T> & dl)
  125. {
  126.     dl.Output(out);
  127.     return out;
  128. }

  129. template <typename T>
  130. DoubleList<T> & DoubleList<T>::Delete(int k, const T & x)
  131. {
  132.     if (k < 0 || !LeftEnd)
  133.         cout<<"OutOfBounds"<<endl;
  134.     DoubleNode<T> * p = LeftEnd;
  135.     if(k == 0)
  136.         LeftEnd = LeftEnd->right;
  137.     else
  138.     {
  139.         DoubleNode<T> * q = LeftEnd;
  140.         for (int index = 0; index < k-1 && q; index++)
  141.             q = q->right;
  142.         if (!q || !q->right)
  143.         {
  144.             cout<<"OutOfBounds"<<endl;
  145.         }
  146.         p= q->right;
  147.         q->right = p->right;
  148.         if (!p->right)
  149.             RightEnd = q;
  150.         else
  151.             p->right->left = q;
  152.     }
  153.     x = p->data;
  154.     delete p;
  155.     return *this;
  156. }

  157. template <typename T>
  158. DoubleList<T> & DoubleList<T>::Insert(int k, const T & x)
  159. {
  160.     if (k < 0)
  161.         cout<<"OutOfBounds"<<endl;
  162.     DoubleNode<T> * p = LeftEnd;
  163.     
  164.     for (int index = 0; index < k && p; index++)
  165.         p = p->right;

  166.     if (k > 0 && !p)
  167.         cout<<"OutOfBounds"<<endl;
  168.     DoubleNode<T> * y = new DoubleNode<T>;
  169.     y->data = x;
  170.     if (k)
  171.     {
  172.         y->right = p->right;
  173.         p->right->left = y;
  174.         p->right = y;
  175.         y->left = p;
  176.         if (!( y->right))
  177.             RightEnd = y;
  178.     }
  179.     else
  180.     {
  181.         y->right = LeftEnd;
  182.         LeftEnd = y;
  183.     }
  184.     return *this;
  185. }

  186. #endif

main.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "DoubleList.h"

  3. using namespace std;

  4. int main()
  5. {
  6.     DoubleList<int> bob;
  7.     cout<<bob.IsEmpty()<<endl;
  8.     cout<<bob.Length()<<endl;
  9.     bob.Insert(0,33);
  10.     bob.Insert(0,22);
  11.     cout<<bob.Length()<<endl;
  12.     cout<<bob<<endl;
  13.     return 0;
  14. }


阅读(1608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~