您的位置:首页 > 其它

哈希表(只存Key值)简易版

2016-03-20 13:44 204 查看
哈希表,存储key节点,用一个状态数组对应标识数据有效性(如果将标识和目标值放在结构体中不利于管理,暴露太多内部设计),该程序实现了查找表中是否包含目标值的功能。
#include<iostream>
using namespace std;
enum Status
{
EXIST,
EMPTY,
DELETE
};
template<class K>
class  HashTable
{
public:
HashTable(int capacity)
:_table(new K[capacity])
, _capacity(capacity)
, _size(0)
, _statu(new Status[capacity])
{
memset(_table, 0, sizeof(K)*capacity);//按字节初始化,不能这样初始化_statu
for (int i = 0; i < capacity; ++i)
{
_statu[i] = EMPTY;
}
}
HashTable(const HashTable<K>& hb)
:_table(new K[hb._capacity])
, _capacity(hb._capacity)
, _size(0)
, _statu(new Status[_capacity])
{
memset(_table, 0, sizeof(K)*_capacity);
for (int i = 0; i < _capacity; ++i)
{
_statu[i] = EMPTY;
}

for (int i = 0; i < _capacity; ++i)
{
if (hb._statu[i] == EXIST)
{
this->Insert(hb._table[i]);
}
}
}
HashTable<K>& operator=(HashTable<K> ht)
{
this->_Swap(ht);
return *this;
}
~HashTable()
{
if (_table)
delete[] _table;
if (_statu)
delete[] _statu;
_size = 0;
_capacity = 0;
}

bool Insert(const K& key)
{
if (_size * 10 / _capacity >= 7)//负载因子最好在0.7到0.8
{
size_t newCapacity = _capacity * 2;
HashTable<K> tmp(newCapacity);
for (int i = 0; i < _capacity; ++i)
{
if (_statu[i] == EXIST)
{
tmp.Insert(_table[i]);
}
}
this->_Swap(tmp);//自定义
}
size_t index = _HashFunc(key);
size_t begin = index;
do
{
if (_statu[index] == EMPTY||_statu[index]==DELETE)
break;
if (_statu[index] == EXIST&&_table[index] == key)
return true;
++index;
if (index == _capacity)
index = 0;
} while (index != begin);
if (_statu[index]==EXIST)
return false;
_table[index] = key;
_statu[index] = EXIST;
++_size;
return true;
}
bool Find(const K& key)
{
size_t index = _HashFunc(key);
size_t begin = index;
do
{
if (_statu[index] == EMPTY)
break;
if (_statu[index] == EXIST&&_table[index] == key)
return true;
++index;
if (index == _capacity)
index = 0;
} while (index != begin);
return false;
}
//懒删除
bool  Remove(const K& key)
{
size_t index = _HashFunc(key);
size_t begin = index;
do
{
if (_statu[index] == EMPTY)
break;
if (_statu[index] == EXIST&&_table[index] == key)
{
_statu[index] = DELETE;
--_size;
return true;
}
++index;
if (index == _capacity)
index = 0;
} while (index != begin);
return false;
}
void Print()
{
for (int i = 0; i < _capacity; ++i)
{
printf("%d:%d-->", _table[i], _statu[i]);
}
cout << endl;
}
protected:
size_t _HashFunc(const K& key)
{
return key%_capacity;//除留余数法
}
void _Swap(HashTable<K>& hs)
{
swap(_table, hs._table);
swap(_capacity, hs._capacity);
swap(_size, hs._size);
swap(_statu, hs._statu);
}
protected:
K* _table;
size_t _capacity;
size_t _size;
Status* _statu;
};
void Test1()
{
HashTable<int> hs(10);
hs.Insert(1);
hs.Insert(11);
hs.Insert(2);
hs.Insert(3);
hs.Insert(6);
hs.Insert(9);
hs.Insert(12);
hs.Insert(8);
hs.Insert(1);
hs.Print();

cout << "Find(1)" << hs.Find(1) << "Find(7)" << hs.Find(7)
<< "Find(11)" << hs.Find(11) << endl;

cout << "Remove(11)" << hs.Remove(11) << "Remove(0)" << hs.Remove(0) << endl;
cout << "Find(11)" << hs.Find(11) << endl;
}

void Test2()
{
HashTable<int> ht(10);
ht.Insert(89);
ht.Insert(18);
ht.Insert(49);
ht.Insert(58);
ht.Insert(9);
ht.Print();

HashTable<int> hb(10);
hb.Insert(1);
hb.Insert(2);
hb.Insert(3);
hb.Insert(4);
hb.Insert(9);
hb.Print();

HashTable<int> h(ht);
h.Print();

h = hb;
h.Print();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  哈希表