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

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-04-28 17:00:48

间接寻址(indirect addressing)是公式化描述和链表描述的组合。采用这种描述方法,可以保留公式化描述方法的许多优点——可以根据索引在O(1)的时间内访问每个元素、可采用二叉搜索方法在对数时间内对一个有序表进行搜索等等。与此同时,也可以获得链表描述方法的重要特色——在诸如插入和删除操作期间不必对元素进行实际的移动。因此,大多数间接寻址链表操作的时间复杂性都与元素的总数无关。
IndirectAdd.h

点击(此处)折叠或打开

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

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

  9. template <typename T>
  10. ostream & operator << (ostream & out, const IndirectList<T> & Inl);

  11. template<typename T>
  12. class IndirectList{
  13.     friend ostream & operator << <T> (ostream & out,const IndirectList<T> & InL);
  14. public:
  15.     IndirectList(int MaxListSize = 10);
  16.     ~IndirectList();
  17.     bool IsEmpty() const {return length == 0;}
  18.     int Length() const {return length;}
  19.     bool Find(int k, T & x) const;
  20.     int Search(const T & x) const;
  21.     IndirectList<T> & Delete(int k,T & x);
  22.     IndirectList<T> & Insert(int k, const T & x);
  23.     void Output(ostream & out) const;
  24. private:
  25.     T ** table;
  26.     int length;
  27.     int MaxSize;
  28. };

  29. template<typename T>
  30. IndirectList<T>::IndirectList(int MaxListSize)
  31. {
  32.     MaxSize = MaxListSize;
  33.     table = new T * [MaxSize];
  34.     length = 0;
  35. }
  36. template<typename T>
  37. IndirectList<T>::~IndirectList()
  38. {
  39.     for (int i = 0; i < length;i++)
  40.         delete table[i];
  41.     delete [] table;
  42. }
  43. template<typename T>
  44. bool IndirectList<T>::Find(int k, T & x) const
  45. {
  46.     if (k < 0 || k >= length) return false;
  47.     x = *table[k];
  48.     return true;
  49. }
  50. template<typename T>
  51. int IndirectList<T>::Search(const T &x) const
  52. {
  53.     for(int i=0; i < length;i++)
  54.     {
  55.         if (*table[i] == x)
  56.             return i;
  57.     }
  58.     return -1;
  59. }
  60. template<typename T>
  61. IndirectList<T> & IndirectList<T>::Delete(int k, T &x)
  62. {
  63.     if (Find(k,x))
  64.     {
  65.         for (int i = k ; i < length;i++)
  66.             table[i-1] = table[i];
  67.         length--;
  68.         return *this;
  69.     }
  70.     else
  71.         cout<<"OutOfBounds"<<endl;
  72. }

  73. template<typename T>
  74. IndirectList<T> & IndirectList<T>::Insert(int k, const T &x)
  75. {
  76.     if (k < 0 || k > length)
  77.         cout<<"OutOfBounds"<<endl;
  78.     if (length == MaxSize)
  79.         cout<<"NoMemory"<<endl;
  80.     for(int i = length-1; i >= k; i--)
  81.     {
  82.         table[i+1] = table[i];
  83.     }
  84.     table[k] = new T;
  85.     *table[k] = x;
  86.     length++;
  87.     return *this;
  88. }
  89. template<typename T>
  90. void IndirectList<T>:: Output(ostream & out) const
  91. {
  92.     for (int i = 0; i < length;i++)
  93.         out << *table[i]<<" ";
  94. }
  95. template<typename T>
  96. ostream & operator << (ostream & out, const IndirectList<T> & Inl)
  97. {
  98.     Inl.Output(out);
  99.     return out;
  100. }
  101. #endif

AppEx.cpp

点击(此处)折叠或打开

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

  3. using namespace std;

  4. int main()
  5. {
  6.     IndirectList<int> bob(10);
  7.     cout<<bob.IsEmpty()<<endl;
  8.     bob.Insert(0,24);
  9.     cout<<bob<<endl;
  10.     return 0;
  11. }


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