"Data Structure" C + + implementation Hashtable (open chain method)

Source: Internet
Author: User

#include <iostream> #include <vector>using namespace Std;template <class K, class v>struct Hashtablenode{k _key; V _value; Hashtablenode<k, v>* _next; Hashtablenode (const K&key, const V&value): _key (Key), _value (value), _next (NULL) {}};template <class k> struct defaulthashfunc{size_t operator () (const k&key) {return key;}}; Template <class K, class V, class Hashfun = Defaulthashfunc<k>>class hashtablebucket{typedef hashtablenode& Lt K, V> node;public:hashtablebucket (): _size (0) {}hashtablebucket (size_t size): _size (0) {_tables.resize (size);} BOOL Insert (const K&key, const v&value) {_checkexpand (); size_t index = Hashfunc (key, _tables.size ()); node* begin = _tables[index];while (BEGIN) {if (Begin->_key = = key) return false;begin = Begin->_next;} node* tmp = new Node (key, value); tmp->_next = _tables[index];_tables[index] = Tmp;++_size;return true;} size_t Hashfunc (const K&key, size_t capacity) {Hashfun Hf;return HF (key)% capacity;} VoID Printtables () {for (size_t i = 0; i < _tables.size (); ++i) {printf ("tables[%d]->", I); node* cur = _tables[i];while (cur) {cout << "[" << cur->_key << ":" << cur->_value << " ] "<<"; cur = cur->_next;} cout << "NULL" &LT;&LT;ENDL;} cout << Endl;} protected:size_t _getnextprime (size_t size) {static const int _primesize = 28;static Const unsigned long _primelist[_prime Size] ={53ul, 97ul, 193ul, 389ul, 769ul,1543ul, 3079ul, 6151ul, 12289ul, 24593ul,49157ul, 98317ul, 196613ul, 393241ul, 786 433ul,1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,1610612741ul, 3221225473ul, 4294967291ul};for (size_t i = 0; i < _primesize; ++i) {if (_primelist[i] > Si Ze) {return _primelist[i];}} return _primelist[_primesize-1];} void _checkexpand () {//load factor to 1 o'clock for increased capacity, guaranteed efficiency if (_size = = _tables.size ()) {size_t newSize = _getnextprime (_size); if (_size = = NE wsize) return;vector<node*> Newtables;newtables.resize (newSize); for (size_t i = 0; i < _tables.size (); ++i) {node* cur = _tables[i];while (cur) {// FETCH node node* tmp = Cur;cur = cur->_next;//Head plug size_t newindex = Hashfunc (Tmp->_key, Newtables.size ()); tmp->_next = new Tables[newindex];newtables[newindex] = tmp;} _tables[i] = NULL;} _tables.swap (Newtables);}} Protected:vector

"Data Structure" C + + implementation Hashtable (open chain method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.