Hash-table(用除法散列法实现)
关键字只支持int型,初学者版本
用链表解决冲突问题
#ifndef C11LEARN_HASHDIVISION_H
#define C11LEARN_HASHDIVISION_H
#include "Chain.h"
template<typename T>
class HashDivision
{
protected:
int capacity;
Chain<T>**array;
public:
HashDivision(int capacity = 701);
HashDivision(const HashDivision<T> &hashDivision);
const HashDivision<T> & operator =(const HashDivision<T> &hashDivision);
T& operator[](int key);
const T operator[](int key) const;
Chain<T>* search(int key);
bool insert(Chain<T>* node);
bool remove(Chain<T>* node);
virtual ~HashDivision();
protected:
virtual int hashing(int index);
void clear();
void copy(const HashDivision<T> &hashDivision);
};
template<typename T>
HashDivision<T>::HashDivision(int capacity):capacity(capacity){
array = new Chain<T>* [this->capacity];
}
template<typename T>
HashDivision<T>::~HashDivision(){
clear();
delete [] array;
array = nullptr;
}
template<typename T>
void HashDivision<T>::clear()
{
Chain<T>* current;
Chain<T>* next;
for (int i = 0; i < capacity; ++i) {
current = array[i];
while (current!= nullptr)
{
next = current->next;
delete current;
current = next;
}
}
}
template<typename T>
HashDivision<T>::HashDivision(const HashDivision<T> &hashDivision){
capacity = hashDivision.capacity;
array = new Chain<T>* [capacity];
copy(hashDivision);
}
template<typename T>
const HashDivision<T> & HashDivision<T>::operator =(const HashDivision<T> &hashDivision){
if(this == &hashDivision) return *this;
clear();
delete [] array;
capacity = hashDivision.capacity;
array = new Chain<T>* [capacity];
copy(hashDivision);
return *this;
}
template<typename T>
void HashDivision<T>::copy(const HashDivision<T> &hashDivision){
Chain<T>* current;
Chain<T>* prev;
for (int i = 0; i < capacity; ++i) {
prev = nullptr;
current = hashDivision.array[i];
while (current!= nullptr)
{
prev = current;
current = current->next;
}
while (prev!= nullptr)
{
Chain<T>* node = new Chain<T>();
node->key = prev->key;
node->value = prev->value;
insert(node);
prev = prev->prev;
}
}
}
template<typename T>
T& HashDivision<T>::operator[](int key){
Chain<T>* node = search(key);
if(node == nullptr)
{
node = new Chain<T>();
node->key = key;
insert(node);
}
return node->value;
}
template<typename T>
const T HashDivision<T>::operator[](int key) const{
Chain<T>* node = search(key);
if(node == nullptr)
{
throw "no find this key";
}
return node->value;
}
template<typename T>
Chain<T>* HashDivision<T>::search(int key)
{
int hash_code = hashing(key);
Chain<T>* current = array[hash_code];
while (current!= nullptr && current->key != key)
{
current =current->next;
}
return current;
}
template<typename T>
int HashDivision<T>::hashing(int index){
return index % capacity;
}
template<typename T>
bool HashDivision<T>::insert(Chain<T>* node){
if(node == nullptr) return false;
int hash_code = hashing(node->key);
Chain<T>* current = array[hash_code];
if(current == nullptr)
{
array[hash_code] = node;
}
else
{
node->next = current;
current->prev = node;
array[hash_code] = node;
}
return true;
}
template<typename T>
bool HashDivision<T>::remove(Chain<T>* node){
if(node == nullptr) return false;
if(node->prev == nullptr)
{
int hash_code = hashing(node->key);
array[hash_code] = node->next;
}
else
{
node->prev->next = node->next;
if(node->next!= nullptr)
node->next->prev = node->prev;
}
delete node;
node = nullptr;
return true;
}
#endif //C11LEARN_HASHDIVISION_H
辅助类
Chain代码链接
测试代码
HashDivision<string> hashDivision;
hashDivision[2] = "hello";
hashDivision[5] = "world";
cout<<hashDivision[2]<<endl;
cout<<hashDivision[5]<<endl;