650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/B7/wKioL1bui1bCKavhAACbVgd711g427.png "title=" QQ picture 20160320193220.png "alt=" Wkiol1bui1bckavhaacbvgd711g427.png "/>
Therefore, the data store is labeled
Pseudo code:
1.//index=_hashfunc (key);
++i;
index+= (2*i-1);
size_t _hashfunc (const k& key)
{
return key%_capacity;
}
2.//index=_hashfunc (Key,++i);
size_t _hashfunc (const k& key,size_t i)
{
Return (key+i*i)%_capacity;
}
#pragma once#include<iostream>using namespace std;enum Status{EXIST,EMPTY,DELETE}; Template<class k>class hashtable{public:hashtable (int capacity): _table (new K[ Capacity]), _size (0), _capacity (capacity), _statu (New status[capacity]) {memset (_table, 0, sizeof (K) *capacity);for (int i = 0; i < capacity; ++i ) {_statu[i] = empty;}} HashTable (CONST&NBSP;HASHTABLE&&NBSP;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& operator= (HASHTABLE&NBSP;HT) {This->_swap (HT); return *this;} ~hashtable () {if (_table) delete[] _table;if (_statu) delete[] _statu;_capacity = _ size = 0;} Bool insert (Const k& key) {if (_size * 10 / _capacity >= &NBSP;7)//load factor preferably at 0.7 to 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);//Custom}int i = 0;size_t index = _hashfunc (key,i);size_t begin = index;do{if (_statu[index] == empty| | _statu[index]==delete) break;if (_statu[index] == exist&&_table[index] == key) {return false;} Index = _hashfunc (key, ++i);if (index == _capacity) index = 0;} while (Begin!=index);if (_statu[index] == exist) Return false;_table[index] = key;_statu[index] = exist;++_size;return true;} Bool find (Const k& key) {Int i = 0;size_t index = _hashfunc ( Key,i);while (_statu[index] != empty) {if (_statu[index] == exist&&_table [Index] == key) {return true;} Index=_hashfunc (key,++i);if (++index == _capacity) index = 0;} Return false;} Lazy Delete Bool remove (const k& key) {int i = 0;size_t index = _hashfunc (key,0);while (_statu[index] != empty) {if (_statu[index] == exist &&_table[index] == key) {_statu[index] = delete;--_size;return true;} Index = _hashfunc (key,++i);if (++index == _capacity) index = 0;} Return false;} Void print () {for (Int i = 0; i < _capacIty; ++i) {printf ("%d:%d-->", _table[i], _statu[i]);} Cout << endl;} Protected:void _swap (HASHTABLE&&NBSP;HT) {swap (_table, ht._table); swap (_size, ht._size); swap (_capacity, ht._capacity); swap (_statu, ht._statu);} Size_t _hashfunc (const k& key,size_t i) {return (key+i*i)%_capacity;} protected:k* _table;size_t _size;size_t _capacity; status* _statu;}; Void test2 () {hashtable<int> ht (); Ht. Insert (in); Ht. Insert (+); Ht. Insert (+); Ht. Insert (+); Ht. Insert (9); Ht. Print (); HASHTABLE<INT>&NBSP;HB (Ten); 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 ();}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1753186
Hash table conflict resolution method (two-time probe)