Hash线性探测法C++实现

Hash线性探测法C++实现,第1张

概述Hash线性探测法C++实现

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

    #include <iostream>      #include <iomanip>      #define DefaultSize 10            using namespace std;            enum KindOfStatus{Active,Empty,Deleted};      template<typename T>      class Hashtable      {      public:              Hashtable(int d,int sz=DefaultSize)              {                  _D = d;                  tableSize=sz;                  CurrentSize=0;                  _A = new T[tableSize];                  info = new KindOfStatus[tableSize];                  for(int _I=0;_I<tableSize;_I++)                  {                      info[_I]=Empty;                  }              }              Hashtable(const Hashtable& ht)              {                  _D=ht._D;                  tableSize=ht.tableSize;                   CurrentSize=ht.CurrentSize;                   _A=new T[tableSize];                  info = new KindOfStatus[tableSize];                  for(int _I=0;_I<tableSize;_I++)                  {                      info[_I]=ht.info[_I];                      _A[_I] = ht._A[_I];                  }              }              Hashtable& operator=(const Hashtable& ht)              {                     if(this!=&ht)                  {                      if(_A!=NulL && info!=NulL)                          {                              delete []_A;                              delete []info;                              _D=ht._D;                              tableSize=ht.tableSize;                              CurrentSize=ht.CurrentSize;                              _A=new T[tableSize];                              info = new KindOfStatus[tableSize];                              for(int _I=0;_I<tableSize;_I++)                              {                                  info[_I]=ht.info[_I];                                  _A[_I] = ht._A[_I];                              }                          }                  }              }              bool operator!=(const Hashtable& ht)              {                  if(_D!=ht._D || tableSize!=ht.tableSize || CurrentSize!=ht.CurrentSize)                  {                      return false;                  }                  for(int _I=0;_I<tableSize;_I++)                  {                      if(info[_I]!=ht.info[_I] || _A[_I]!=ht._A[_I])                      return false;                  }                  return true;              }      int Hash(int x)          {              int dex = x%_D;              int _I=dex;              if(info[_I]==Empty || info[_I]==Deleted)                  return _I;            do              {                  if(info[_I]==Active && _A[_I]!=x)                      _I=(_I+1)%tableSize;                  if(info[_I]==Empty || (info[_I]==Active && _A[_I]==x) || info[_I]==Deleted)                      return _I;              }while(_I!=dex);              return _I;          }      int FindPos(int x)      {          int dex = x%_D;          int _I=dex;          do          {              if(info[_I]==Active && _A[_I]==x)              {      #ifdef _YES                  cout<<"找到该元素,它的位置是:"<<(_I)%tableSize+1<<endl;      #endif                  return _I;              }              _I=(_I+1)%tableSize;          }while(dex!=_I);          if(dex==_I)              {cout<<"没有找到该元素"<<endl;return -1;};      }      voID Insert(int x)          {                  int dex=Hash(x);                  if(info[dex]==Active && _A[dex]==x)                  {cout<<"已经存在的值,不能插入!!"<<endl;return ;}                  if(info[dex]==Active && dex==(x%tableSize))                  {cout<<"表满!!"<<endl;return ;}                  if(info[dex]==Empty || info[dex]==Deleted)                      _A[dex] = x;                      info[dex]=Active;                      CurrentSize++;          }          ~Hashtable()          {              delete []_A;              delete []info;              for(int _I=0;_I<tableSize;_I++)              {                  info[_I]=Empty;              }          }      voID Show()          {              cout<<"状态:";              for(int _I=0;_I<tableSize;_I++)              {                  cout<<setw(4)<<info[_I];              }              cout<<endl;              cout<<"内容:";              for(int _J=0;_J<tableSize;_J++)              {                  cout<<setw(4)<<_A[_J];              }              cout<<endl;        }      voID Remove(int x)          {              int dex = FindPos(x)-1;              info[dex]=Deleted;              _A[dex]=0;          }           private:          int _D;          int CurrentSize;          int tableSize;          KindOfStatus *info;          T *_A;      };            int main()      {          Hashtable<int> ht(7,10);          ht.Insert(1);          ht.Insert(8);          ht.Insert(15);          ht.Insert(22);          ht.Insert(29);          ht.Insert(36);          ht.Insert(43);          ht.Insert(50);          ht.Insert(57);          ht.Insert(64);          Hashtable<int> hz(ht);          hz.Remove(8);          hz.Show();          return 0;      }  

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的Hash线性探测法C++实现全部内容,希望文章能够帮你解决Hash线性探测法C++实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1231909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存