码迷,mamicode.com
首页 > 其他好文 > 详细

哈希表

时间:2016-05-24 10:31:27      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:return

哈希表作为数据结构学习中比较重要的一部分,今天介绍的是解决哈希冲突的一种算法,哈希桶法,它的原理是:当两个数映射到哈希表中的位置相同的时候,就在这个位置处产生一个链表一样的结构,将这些数都放入这个位置的链表处,用next指针将它们相连,代码如下

#include<vector>

template<class K>

struct _HashFunc

{

size_t operator()(const K&key)

{

return key;

}

};

template<>

struct _HashFunc<string>

{

size_t operator()(const string&key)

{

size_t hash = 0;

for (int i = 0; i < key.size(); i++)

{

hash += key[i];

}

return hash;

}

};

template <class K,class V>

struct HashNode

{

K _key;

V _value;

HashNode<K, V> *_next;

HashNode(const K&key, const V&value)

:_key(key)

, _value(value)

, _next(NULL)

{}

};

template <class K, class V, class HashFunc = _HashFunc<K>>

class Hash

{

typedef HashNode<K, V> Node;

typedef Hash<K, V> H;

private:

vector<Node *>_tables;

size_t _size;

public:

Hash()

:_size(0)

{}

size_t _Getprime(int size)

{

int primesize = 2;

int primelist[2] = { 53, 97 };

for (int i = 0; i < primesize; i++)

{

if (_size < primelist[i])

{

return primelist[i];

}

}

return  primelist[primesize - 1];

}

size_t _HashFunc(const K&key,int size)

{

HashFunc hashfunc;

return  hashfunc(key)%size;

}

void _Checkcapacity()

{

if (_size == _tables.size())

{

size_t nextsize = _Getprime(_size);

vector<Node *>_newtables;

_newtables.resize(nextsize);

for (int i = 0; i < _tables.size(); i++)

{

Node *cur = _tables[i];

while (cur)

{

size_t index = _HashFunc(cur->_key, _newtables.size());

Node *prev = cur;

cur = cur->_next;

prev->_next = _newtables[index];

_newtables[index] = prev;

}

_tables[i] = NULL;

}

_tables.swap(_newtables);

}

}

~Hash()

{

if (_tables.size() != 0)

{

for (int i = 0; i < _tables.size(); i++)

{

Node *cur = _tables[i];

while (cur)

{

Node *prev = cur;

cur = cur->_next;

delete prev;

prev = NULL;

}

_tables[i] = NULL;

}

}

}

Node *find(const K&key)

{

size_t index = _HashFunc(key, _tables.size());

Node *cur = _tables[index];

while (cur)

{

if (cur->_key == key)

{

return _tables[index];

}

cur = cur->_next;

}

return NULL;

}

void Remove(const K&key)

{

size_t index = _HashFunc(key, _tables.size());

Node *cur = _tables[index];

Node *del = NULL;

if (cur == NULL)

{

return;

}

while (cur)

{

if (cur->_key == key)

{

delete cur;

_tables[index] = NULL;

return;

}

if (cur->_next != NULL)

{

if (cur->_next->_key == key)

{

del = cur->_next;

cur->_next = cur->_next->_next;

delete del;

return;

}

}

cur = cur->_next;

}

}

bool Insert(const K&key, const V&value)

{

_Checkcapacity();

size_t index = _HashFunc(key, _tables.size());

Node *cur = _tables[index];

while (cur != NULL)

{

if (cur->_key == key)

{

return false;

}

cur = cur->_next;

}

Node *tem=new Node(key, value);

tem->_next =_tables[index];

_tables[index] = tem;

   _size++;

}

Hash(const  Hash& ht)

:_tables(NULL)

, _size(ht._tables.size())

{

_tables.resize(ht._tables.size());

for (int i = 0; i < ht._tables.size(); i++)

{

Node *cur = ht._tables[i];

while (cur)

{

this->Insert(cur->_key, cur->_value);

cur = cur->_next;

}

}

}

H&operator=(H ht)

{

_tables.resize(ht._tables.size());

_tables.swap(ht._tables);

swap(_size, ht._size);

return *this;

}

void print()

{

for (int i = 0; i < _tables.size(); i++)

{

Node *cur = _tables[i];

while (cur)

{

cout << "(" << cur->_key << "," << cur->_value << ")" << " ";

cur = cur->_next;

}

cout << endl;

}

}

};

void test()

{

Hash<string, int> ht;

ht.Insert("1", 1);

ht.Insert("2", 2);

ht.Insert("3", 52);

ht.print();

}

int main()

{

test();

getchar();

return 0;

}


哈希表

标签:return

原文地址:http://10810512.blog.51cto.com/10800512/1782402

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!