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

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-04-24 10:08:08


点击(此处)折叠或打开

  1. //基于公式的类LinearList
  2. template<class T>
  3. class LinearList{
  4.     public:
  5.         LinearList(int MaxListSize = 10);//构造函数
  6.         ~LinearList() {delete [] element;}//析构函数
  7.         bool IsEmpty() const { return length == 0;}
  8.         int Length() const { return length;}
  9.         bool Find(int k, T & x) const;//返回第k个元素至x中
  10.         int Search(const T & x) const;//返回x所在位置
  11.         LinearList<T> & Delete(int k, T & x);//删除第k个元素至x中
  12.         LinearList<T> & Insert(int k ,const T & x);//在第k个元素后插入x
  13.         void Output(ostream & out) const;
  14.     private:
  15.         int length;
  16.         int MaxSize;
  17.         T * element;//一维动态数组
  18. };



  19. update by bob_hu on Apr 17,2012

  20. template<class T>
  21. LinearList<T>::LinearList(int MaxListSize)
  22. {
  23.     MaxSize = MaxListSize;
  24.     element = new T[MaxSize];
  25.     length = 0;
  26. }

  27. template<class T>
  28. bool LinearList<T>::Find(int k, T & x) const
  29. {
  30.     if ( k < 1 || k > length) return false;
  31.     x = element[k-1];
  32.     return true;
  33. }

  34. template<class T>
  35. int LinearList<T>::Search(const T & x) const
  36. {
  37.     for (int i = 0; i < length; i++)
  38.         if ( element[i] == x) return ++i;
  39.     return 0;
  40. }

  41. update by bob_hu on Apr 19,2012

  42. template<class T>
  43. LinearList<T>& LinearList<T>::Delete(int k ,T &x)
  44. {
  45.     if (Find(k,x))
  46.     {
  47.         for (int i = k; i < length;i++)
  48.             element[i-1]= element[i];
  49.         length--;
  50.         return *this;
  51.     }
  52.     else
  53.         //如果不存在第k个元素,则引发异常OutOfBounds
  54.         throw OutOfBounds();
  55. }
  56. template<class T>
  57. LinearList<T> & LinearList<T>::Insert(int k, const T & x)
  58. {
  59.   if (k<0|| k > length) throw OutOfBounds();
  60.   if (length == MaxSize) throw NoMem();
  61.   for(int i = length - 1; i >= k; i--)
  62.     element[i+1] = element[i];
  63.   element[k] = x;
  64.   length++;
  65.   return *this;
  66. }
  67. template<class T>
  68. void LinearList<T>::Output(ostream & out) const
  69. {
  70.   for(int i = 0; i < length; i++)
  71.     out<<element[i]<<" ";
  72. }
  73. template<class T>
  74. ostream& operator<<(ostream & out, const LinearList<T> & x)
  75. {
  76.   x.Output(out);
  77.   return out;
  78. }

  79. update by bob_hu on Apr 20,2012

  80. #include <iostream>
  81. using namespace std;
  82. int main()
  83. {
  84.     LinearList<int> L(5);
  85.     cout<<"Length = "<<L.Length()<<endl;
  86.     cout<<"IsEmpty = "<<L.IsEmpty()<<endl;
  87.     L.Insert(0,2).Insert(1,6);
  88.     cout<<"List is "<<L<<endl;
  89.     cout<<"IsEmpty = "<<L.IsEmpty()<<endl;
  90.     int z;
  91.     L.Find(1,z);
  92.     cout<<"First element is "<< z << endl;
  93.     cout<<"Length = "<<L.Length()<<endl;
  94.     L.Delete(1,z);
  95.     cout<<"Deleted element is "<< z <<endl;
  96.     cout<<"List is "<<L<<endl;
  97.     return 0;
  98. }



  99. update by bob_hu on Apr 21,2012

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