线性探查法实现的散列表(哈希表)类
2009-02-20 15:02
537 查看
const int DefaultSize = 100;
enum KindofStatus {Active, Empty, Deleted};
template<class E, class K>
class HashTable
{
public:
HashTable(int d, int sz = DefaultSize);
~HashTable() {delete []ht_; delete []info;}
HashTable<E, K>& operator = (const HashTable<E, K>& rhs);
bool Search(const K k1, const E& el) const;
bool Insert(const E& el);
bool Remove(const K k1, const E& el);
void makeEmpty();
private:
int divitor_;
int current_size_;
int table_size_;
E* ht_;
KindofStatus* info;
int find_pos(const K k1) const;
int operator == (E& el) {return *this == el;}
int operator != (E& el) {return *this != el;}
};
template<class E, class K>
int HashTable<E, K>::find_pos(const K k1) const
{
int i = k1%divitor_;
int j = i;
do
{
if(info[j] == Empty || info[j] == Active && ht_[j] == k1)
return j;
j = (j + 1) % table_size_;
}while(j !=i);
return j;
}
template<class E, class K>
bool HashTable<E, K>::Search(const K k1, const E& el) const
{
int i = find_pos(k1);
if(info[i] != Active || ht_[i] != k1)
return false;
el = ht_[i];
return true;
}
template<class E, class K>
void HashTable<E, K>::makeEmpty()
{
for(int i=0; i<table_size_; i++)
{
info[i] = Empty;
current_size_ = 0;
}
}
template<class E, class K>
bool HashTable<E, K>::Insert(const E& el)
{
K k1 = el; // overload
int i = find_pos(k1);
if(info[i] != Active)
{
ht_[i] = el;
current_size_ ++;
info[i]++;
return true;
}
if(info[i] == Active && ht_[i] == k1)
{
cout<<"this key has been contained"<<endl;
return false;
}
cout<<"the table is full and can not be insert!"<<endl;
return false;
}
template<class E, class K>
HashTable<E, K>& HashTable<E, K>::operator = (const HashTable<E, K>& rhs)
{
if( *this != rhs)
{
delete []ht_;
delete []info;
table_size_ = rhs.table_size_;
divitor_ = rhs.divitor_;
current_size_ = rhs.current_size_;
ht_ = new E[table_size_];
info = new KindofStatus[table_size_];
for(int i=0; i<table_size_; i++)
{
ht_[i] = rhs.ht_[i];
info[i] = rhs.info[i];
}
}
return *this;
}
template<class E, class K>
bool HashTable<E, K>::Remove(const K k1, const E& el)
{
int i = find_pos(k1);
if(info[i] == Active && ht_[i] == k1)
{
info[i] = Deleted;
current_size_--;
return true;
}
return false;
}
enum KindofStatus {Active, Empty, Deleted};
template<class E, class K>
class HashTable
{
public:
HashTable(int d, int sz = DefaultSize);
~HashTable() {delete []ht_; delete []info;}
HashTable<E, K>& operator = (const HashTable<E, K>& rhs);
bool Search(const K k1, const E& el) const;
bool Insert(const E& el);
bool Remove(const K k1, const E& el);
void makeEmpty();
private:
int divitor_;
int current_size_;
int table_size_;
E* ht_;
KindofStatus* info;
int find_pos(const K k1) const;
int operator == (E& el) {return *this == el;}
int operator != (E& el) {return *this != el;}
};
template<class E, class K>
int HashTable<E, K>::find_pos(const K k1) const
{
int i = k1%divitor_;
int j = i;
do
{
if(info[j] == Empty || info[j] == Active && ht_[j] == k1)
return j;
j = (j + 1) % table_size_;
}while(j !=i);
return j;
}
template<class E, class K>
bool HashTable<E, K>::Search(const K k1, const E& el) const
{
int i = find_pos(k1);
if(info[i] != Active || ht_[i] != k1)
return false;
el = ht_[i];
return true;
}
template<class E, class K>
void HashTable<E, K>::makeEmpty()
{
for(int i=0; i<table_size_; i++)
{
info[i] = Empty;
current_size_ = 0;
}
}
template<class E, class K>
bool HashTable<E, K>::Insert(const E& el)
{
K k1 = el; // overload
int i = find_pos(k1);
if(info[i] != Active)
{
ht_[i] = el;
current_size_ ++;
info[i]++;
return true;
}
if(info[i] == Active && ht_[i] == k1)
{
cout<<"this key has been contained"<<endl;
return false;
}
cout<<"the table is full and can not be insert!"<<endl;
return false;
}
template<class E, class K>
HashTable<E, K>& HashTable<E, K>::operator = (const HashTable<E, K>& rhs)
{
if( *this != rhs)
{
delete []ht_;
delete []info;
table_size_ = rhs.table_size_;
divitor_ = rhs.divitor_;
current_size_ = rhs.current_size_;
ht_ = new E[table_size_];
info = new KindofStatus[table_size_];
for(int i=0; i<table_size_; i++)
{
ht_[i] = rhs.ht_[i];
info[i] = rhs.info[i];
}
}
return *this;
}
template<class E, class K>
bool HashTable<E, K>::Remove(const K k1, const E& el)
{
int i = find_pos(k1);
if(info[i] == Active && ht_[i] == k1)
{
info[i] = Deleted;
current_size_--;
return true;
}
return false;
}
相关文章推荐
- 第13题用线性探查法实现的散列表 POWERBY KTL
- 用开放地址法中的线性探查法解决冲突实现哈希表的运算
- 散列(哈希)表的C++简单操作和实现(线性探查)
- 散列查找:C语言线性探查与拉链存储的实现
- 【哈希表】线性探测再散列的相关知识与计算
- 哈希表(线性探测再散列)
- 散列表(哈希表)的实现
- c语言实现最简单的哈希表(开放地址线性探测法)
- 【哈希表】线性探测再散列的相关知识与计算
- 哈希表---开散列实现
- 【数据结构】通讯录查询系统的设计与实现(散列表(哈希表))
- 哈希表(散列)HashTable实现
- 实验 哈希表线性探测再散列
- 散列表 哈希表 原理 python实现
- 详细图解什么叫平方探查法即二次探测再散列和线性探测再散列(数据结构 哈希函数 哈希冲突)
- 哈希表(散列)HashTable实现
- 散列表(三):冲突处理的方法之开地址法(线性探测再散列的实现)
- 散列表(三):冲突处理的方法之开地址法(线性探测再散列的实现)
- 开放定址散列表(线性探测法)实现文件C语言
- [数据结构]散列表-链接法和开放寻址法 线性探查