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

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-04-28 10:57:33

LinearList.h

点击(此处)折叠或打开

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

  7. template<class T>
  8. class LinearList{
  9. public:
  10.     LinearList(int MaxListSize = 10);
  11.     ~LinearList() {delete [] element;}
  12.     bool IsEmpty() const { return length == 0;}
  13.     int Length() const { return length;}
  14.     bool Find(int k, T & x) const;
  15.     int Search(const T & x) const;
  16.     LinearList<T> & Delete(int k, T & x);
  17.     LinearList<T> & Insert(int k ,const T & x);
  18.     void Output(ostream & out) const;
  19. private:
  20.     int length;
  21.     int MaxSize;
  22.     T * element;
  23. };

  24. template<class T>
  25. LinearList<T>::LinearList(int MaxListSize)
  26. {
  27.     MaxSize = MaxListSize;
  28.     element = new T[MaxSize];
  29.     length = 0;
  30. }

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

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


  45. template<class T>
  46. LinearList<T>& LinearList<T>::Delete(int k ,T &x)
  47. {
  48.     if (Find(k,x))
  49.     {
  50.         for (int i = k; i < length;i++)
  51.             element[i-1]= element[i];
  52.         length--;
  53.         return *this;
  54.     }
  55.     else
  56.         cout<<"OutOfBounds"<<endl;
  57.         //throw OutOfBounds();
  58. }
  59. template<class T>
  60. LinearList<T> & LinearList<T>::Insert(int k, const T & x)
  61. {
  62.     if (k<0|| k > length)
  63.         //throw OutOfBounds();
  64.         cout<<"OutOfBounds"<<endl;
  65.     if (length == MaxSize)
  66.         //throw NoMem();
  67.         cout<<"NoMem"<<endl;
  68.     for(int i = length - 1; i >= k; i--)
  69.         element[i+1] = element[i];
  70.     element[k] = x;
  71.     length++;
  72.     return *this;
  73. }
  74. template<class T>
  75. void LinearList<T>::Output(ostream & out) const
  76. {
  77.     for(int i = 0; i < length; i++)
  78.         out<<element[i]<<" ";
  79. }
  80. template<class T>
  81. ostream& operator<<(ostream & out, const LinearList<T> & x)
  82. {
  83.     x.Output(out);
  84.     return out;
  85. }

  86. #endif

main.cpp

点击(此处)折叠或打开

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

  3. using namespace std;

  4. int main()
  5. {
  6.     LinearList<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. }


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