哈希表 除数余留 + 链地址 && 线性探测 && 平方探测
2017-01-17 21:02
423 查看
/***************************** author: yomi date: 17.1.17 mood: ps: *******************************/ /*** #include<iostream> using namespace std; const int maxn = 13; struct hash_node{ int data; hash_node *next; }; int hash(int num){ return num % maxn; } void collision(hash_node *vec[], int elem, hash_node *new1){ if(vec[elem] == NULL){ vec[elem] = new1; return; } new1->next = vec[elem]; vec[elem] = new1; } void hash_create(hash_node *vec[], int n){ int tmp; for(int i=0; i<n; i++){ hash_node *p = new hash_node; p->next = NULL; cin >> p->data; tmp = hash(p->data); collision(vec, tmp, p); } } void hash_print(hash_node *vec[]){ for(int i=0; i<maxn; i++){ hash_node *p; cout << "vec[" << i << "]:"; if(vec[i] == NULL){ cout << "NULL" << endl; continue; } p = vec[i]; while(p){ cout << p->data << "->"; p = p->next; } cout << "NULL" << endl; } } void delete_hash_node(hash_node *vec[], int num){ int elem = hash(num); int flag = 0; if(vec[elem] == NULL){ cout << "not found!!!" << endl; return; } hash_node *p; p = vec[elem]; if(p->data == num){ vec[elem] = p->next; delete p; } else{ hash_node *q; q = vec[elem]->next; while(q){ if(q->data == num){ p->next = q->next; flag = 1; delete q; break; } p = p->next; q = q->next; } if(flag == 0){ cout << "not found!!!" << endl; } } } int main(){ hash_node *vec[maxn]; for(int i=0; i<maxn; i++){ vec[i] = NULL; } int n; cin >> n; hash_create(vec, n); hash_print(vec); cout << "delete_hash_node" << endl; while(1){ int num; cin >> num; delete_hash_node(vec, num); hash_print(vec); } return 0; } **/ /** 12 19 14 23 01 68 20 84 27 55 11 10 79 vec[0]:NULL vec[1]:79->27->1->14->NULL vec[2]:NULL vec[3]:55->68->NULL vec[4]:NULL vec[5]:NULL vec[6]:84->19->NULL vec[7]:20->NULL vec[8]:NULL vec[9]:NULL vec[10]:10->23->NULL vec[11]:11->NULL vec[12]:NULL delete_hash_node 27 vec[0]:NULL vec[1]:79->1->14->NULL vec[2]:NULL vec[3]:55->68->NULL vec[4]:NULL vec[5]:NULL vec[6]:84->19->NULL vec[7]:20->NULL vec[8]:NULL vec[9]:NULL vec[10]:10->23->NULL vec[11]:11->NULL vec[12]:NULL 10 vec[0]:NULL vec[1]:79->1->14->NULL vec[2]:NULL vec[3]:55->68->NULL vec[4]:NULL vec[5]:NULL vec[6]:84->19->NULL vec[7]:20->NULL vec[8]:NULL vec[9]:NULL vec[10]:23->NULL vec[11]:11->NULL vec[12]:NULL 100 not found!!! vec[0]:NULL vec[1]:79->1->14->NULL vec[2]:NULL vec[3]:55->68->NULL vec[4]:NULL vec[5]:NULL vec[6]:84->19->NULL vec[7]:20->NULL vec[8]:NULL vec[9]:NULL vec[10]:23->NULL vec[11]:11->NULL vec[12]:NULL 10 not found!!! vec[0]:NULL vec[1]:79->1->14->NULL vec[2]:NULL vec[3]:55->68->NULL vec[4]:NULL vec[5]:NULL vec[6]:84->19->NULL vec[7]:20->NULL vec[8]:NULL vec[9]:NULL vec[10]:23->NULL vec[11]:11->NULL vec[12]:NULL **/ /** #include<iostream> using namespace std; const int maxn = 5; int a[maxn]; int _hash(int num){ return num % maxn; } void collision(int elem, int s){ if(elem == maxn){ elem = 0; } if(a[elem] == -10000){ a[elem] = s; } else{ collision(elem+1, s); } } void create(){ int n, s; int tmp; cin >> n; for(int i=0; i<n; i++){ cin >> s; tmp = _hash(s); collision(tmp, s); } } void print(){ for(int i=0; i<maxn; i++){ cout << "[" << i << "] :"; if(a[i] == -10000){ cout << "NULL"; } else{ cout << a[i]; } cout << endl; } } int main(){ for(int i=0; i<maxn; i++){ a[i] = -10000; } create(); print(); return 0; } **/ /** 3 8 13 18 [0] :18 [1] :NULL [2] :NULL [3] :8 [4] :13 Process returned 0 (0x0) execution time : 9.308 s Press any key to continue. **/ #include<iostream> using namespace std; const int maxn = 11; int a[maxn]; int _hash(int num){ return num % maxn; } void collision(int elem, int s){ int conflic = 1; int ii=0; while(1){ if(a[elem] == -10000){ a[elem] = s; break; } else { elem = elem+conflic*conflic-ii*ii; if((elem+conflic*conflic-ii*ii)>=maxn){ elem %= maxn; } ii++; conflic++; } } } void create(){ int n, s; int tmp; cin >> n; for(int i=0; i<n; i++){ cin >> s; tmp = _hash(s); collision(tmp, s); } } void print(){ for(int i=0; i<maxn; i++){ cout << "[" << i << "] :"; if(a[i] == -10000){ cout << "NULL"; } else{ cout << a[i]; } cout << endl; } } void init(){ for(int i=0; i<maxn; i++){ a[i] = -10000; } } int main(){ init(); create(); print(); return 0; } /** 9 47 7 29 11 16 92 22 8 3 [0] :11 [1] :22 [2] :NULL [3] :47 [4] :92 [5] :16 [6] :3 [7] :7 [8] :29 [9] :8 [10] :NULL Process returned 0 (0x0) execution time : 119.109 s Press any key to continue. **/
相关文章推荐
- C++数据结构--.哈希表线性探测开放寻址法与独立表链地址法
- 哈希表线性探测&二次探测
- c语言实现最简单的哈希表(开放地址线性探测法)
- 线性探测法构造哈希表(hash)
- 数据结构:哈希表(除留取余法--线性探测法)
- 哈希表,开放地址法之线性探测代码(JAVA)
- 散列表(三):冲突处理的方法之开地址法(线性探测再散列的实现)
- 哈希表(开放寻址,线性探测)
- 线性探测法构造哈希表(hash)
- 哈希表---线性探测再散列(hash)
- 哈希表-开放地址法之线性探测代码(JAVA)
- 使用线性探测法构造哈希表
- 线性探测再散列解决冲突的哈希表
- 线性探测法构造哈希表(hash)
- 线性探测法构造哈希表(hash)
- 平方探测哈希表
- 散列(开发地址法-线性探测法)
- 哈希表---线性探测法
- 哈希表,开放地址法之线性探测代码(JAVA)
- 线性探测法构造哈希表(hash)