哈希查找解决地址冲突的两种最常见方法(线性探测再散列,链地址法)C++实现

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线性探测散列: ```c++ #include <iostream> #define MAXSIZE 13 using namespace std; int hashTable[MAXSIZE] = {0}; void insert(int key) { int pos = key % MAXSIZE; if (hashTable[pos] == 0) { hashTable[pos] = key; } else { int i = 1; while (hashTable[(pos + i) % MAXSIZE] != 0) { i++; } hashTable[(pos + i) % MAXSIZE] = key; } } int search(int key) { int pos = key % MAXSIZE; int i = 0; while (hashTable[(pos + i) % MAXSIZE] != key) { i++; if (hashTable[(pos + i) % MAXSIZE] == 0 || i == MAXSIZE) { return -1; } } return (pos + i) % MAXSIZE; } int main() { int arr[10] = {5,88,12,56,71,28,33,43,93,17}; for (int i = 0; i < 10; i++) { insert(arr[i]); } for (int i = 0; i < MAXSIZE; i++) { cout << hashTable[i] << " "; } cout << endl; int pos = search(33); if (pos != -1) { cout << "Find key 33 at position " << pos << endl; } else { cout << "Key 33 not found" << endl; } return 0; } ``` 二次线性探测散列: ```c++ #include <iostream> #define MAXSIZE 13 using namespace std; int hashTable[MAXSIZE] = {0}; void insert(int key) { int pos = key % MAXSIZE; if (hashTable[pos] == 0) { hashTable[pos] = key; } else { int i = 1; while (hashTable[(pos + i*i) % MAXSIZE] != 0) { i++; } hashTable[(pos + i*i) % MAXSIZE] = key; } } int search(int key) { int pos = key % MAXSIZE; int i = 0; while (hashTable[(pos + i*i) % MAXSIZE] != key) { i++; if (hashTable[(pos + i*i) % MAXSIZE] == 0 || i == MAXSIZE) { return -1; } } return (pos + i*i) % MAXSIZE; } int main() { int arr[10] = {5,88,12,56,71,28,33,43,93,17}; for (int i = 0; i < 10; i++) { insert(arr[i]); } for (int i = 0; i < MAXSIZE; i++) { cout << hashTable[i] << " "; } cout << endl; int pos = search(33); if (pos != -1) { cout << "Find key 33 at position " << pos << endl; } else { cout << "Key 33 not found" << endl; } return 0; } ``` 地址: ```c++ #include <iostream> #include <vector> #define MAXSIZE 13 using namespace std; struct Node { int val; Node* next; Node(int x) : val(x), next(NULL) {} }; vector<Node*> hashTable(MAXSIZE, NULL); void insert(int key) { int pos = key % MAXSIZE; if (hashTable[pos] == NULL) { hashTable[pos] = new Node(key); } else { Node* p = hashTable[pos]; while (p->next != NULL) { p = p->next; } p->next = new Node(key); } } int search(int key) { int pos = key % MAXSIZE; Node* p = hashTable[pos]; int cnt = 0; while (p != NULL) { if (p->val == key) { return cnt; } p = p->next; cnt++; } return -1; } int main() { int arr[10] = {5,88,12,56,71,28,33,43,93,17}; for (int i = 0; i < 10; i++) { insert(arr[i]); } for (int i = 0; i < MAXSIZE; i++) { cout << i << ": "; Node* p = hashTable[i]; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; } int cnt = search(33); if (cnt != -1) { cout << "Find key 33, search " << cnt+1 << " times" << endl; } else { cout << "Key 33 not found" << endl; } return 0; } ``` 其中,查找成功时的平均查找长度为指定关键字的查找长度之和与关键字总数的商。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值