news 2026/5/9 16:18:57

数据结构 哈希表(链地址法)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
数据结构 哈希表(链地址法)

头文件

#pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #include<iostream> using namespace std; #define hashfactor 0.75 #define initcapacity 16 typedef struct hashnode{ char* key; int val; struct hashnode* next; }hashnode; typedef struct hashtable { hashnode** buckets; int size; int capacity;// 桶的数量 double factor; }hashtable; // 字符串哈希函数(djb2算法) static unsigned long hashString(const char* str); // 判断是否为质数 static int isPrime(int n); // 获取下一个质数 static int nextPrime(int n); // 创建新节点 static hashnode* createNode(const char* key, int value); // 释放节点 static void freeNode(hashnode* node); // 计算哈希索引 static int getHashIndex(hashtable* table, const char* key); // 重新哈希(扩容) static void rehash(hashtable* table); // 检查是否需要扩容 static void checkResize(hashtable* table); // 1. 创建哈希表 hashtable* createhashtable(); // 2. 销毁哈希表 void destroyhashtable(hashtable* table); // 3. 插入键值对 int hashInsert(hashtable* table, const char* key, int value); // 4. 查找键对应的值 int* hashSearch(hashtable* table, const char* key); // 5. 删除键值对 int hashDelete(hashtable* table, const char* key); // 6. 获取哈希表大小 int getSize(hashtable* table); // 7. 判断哈希表是否为空 int isEmpty(hashtable* table); // 8. 打印哈希表 void printhashtable(hashtable* table); // 9. 清空哈希表 void clearhashtable(hashtable* table); // 10. 获取当前负载因子 double getLoadFactor(hashtable* table);

源文件

#include"哈希.h" // 字符串哈希函数(djb2算法) static unsigned long hashString(const char* str) { unsigned long hash = 5381; int n = 0; while ((n=*str++)) { hash = ((hash << 5) + hash) + n; } return hash; } // 判断是否为质数 static int isPrime(int n) { if (n <= 1)return 0; if (n <= 3)return 1; if (n % 2 == 0 || n % 3 == 0)return 0; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return 0; } return 1; } // 获取下一个质数 static int nextPrime(int n) { while (!isPrime(n)) { n++; } return n; } // 创建新节点 static hashnode* createNode(const char* key, int value) { assert(key != nullptr); hashnode* node = (hashnode*)malloc(sizeof(hashnode)); if (node == nullptr) { cout << "init err" << endl; return nullptr; } node->key = strdup(key); if (node->key==nullptr) { free(node); cout << "init err" << endl; return nullptr; } node->val = value; node->next = nullptr; return node; } // 释放节点 static void freeNode(hashnode* node) { assert(node != nullptr); free(node->key); free(node); } // 计算哈希索引 static int getHashIndex(hashtable* table, const char* key) { assert(table != nullptr && key != nullptr); return hashString(key) % table->capacity; } // 重新哈希(扩容) static void rehash(hashtable* table) { assert(table != nullptr); int newcapacity = nextPrime(table->capacity*2); hashnode** newbuckets = (hashnode**)calloc(newcapacity, sizeof(hashnode*)); if (newbuckets == nullptr) { cout << "expand err" << endl; return ; } for (int i = 0; i < table->capacity; i++) { hashnode* node = table->buckets[i]; while (node) { hashnode* next = node->next; int newIndex = hashString(node->key) % newcapacity; node->next = newbuckets[newIndex]; newbuckets[newIndex] = node; node = next; } } free(table->buckets); table->buckets = newbuckets; table->capacity = newcapacity; } // 检查是否需要扩容 static void checkResize(hashtable* table) { assert(table != nullptr); double a =(double) table->size / table->capacity; if (a >= table->factor)rehash(table); } // 1. 创建哈希表 hashtable* createhashtable() { hashtable* table = (hashtable*)malloc(sizeof(hashtable)); if (table==nullptr) { cout << "init err" << endl; return nullptr; } table->capacity = initcapacity; table->factor = hashfactor; table->size = 0; table->buckets = (hashnode**)calloc(table->capacity, sizeof(hashnode*)); if (table->buckets == nullptr) { free(table); printf("createHashTable err: 桶数组分配失败\n"); return nullptr; } return table; } // 2. 销毁哈希表 void destroyhashtable(hashtable* table) { assert(table != nullptr); clearhashtable(table); if (table->buckets) { free(table->buckets); } free(table); } // 3. 插入键值对 int hashInsert(hashtable* table, const char* key, int value) { assert(key != nullptr && table != nullptr); checkResize(table); int index =getHashIndex(table, key); hashnode*head = table->buckets[index]; hashnode* current = head; while (current) { if (strcmp(current->key, key) == 0) { current->val = value; return 1; } current = current->next; } hashnode* node = createNode(key, value); if (node == nullptr) { cout << "err" << endl; return 0; } node->next = table->buckets[index]; table->buckets[index] = node; table->size++; return 1; } // 4. 查找键对应的值 int* hashSearch(hashtable* table, const char* key) { assert(table != nullptr && key != nullptr); int index = getHashIndex(table, key); hashnode* current = table->buckets[index]; while (current) { if (strcmp(current->key, key) == 0) { return &current->val; } current = current->next; } return nullptr; } // 5. 删除键值对 int hashDelete(hashtable* table, const char* key) { assert(table != nullptr && key != nullptr); int index = getHashIndex(table, key); hashnode* current = table->buckets[index]; hashnode* prev = nullptr; while (current) { if (strcmp(current->key, key) == 0) { if (prev) { prev->next = current->next; } else { table->buckets[index] = current->next; } freeNode(current); table->size--; return 1; } prev = current; current = current->next; } return 0; } // 6. 获取哈希表大小 int getSize(hashtable* table) { assert(table != nullptr); return table->size; } // 7. 判断哈希表是否为空 int isEmpty(hashtable* table) { assert(table != nullptr); return table->size == 0; } // 8. 打印哈希表 void printhashtable(hashtable* table) { assert(table != nullptr); if (isEmpty(table))return; printf("\n========== 哈希表内容 ==========\n"); for (int i = 0; i < table->capacity; i++) { printf("桶[%3d]: ", i); hashnode* current = table->buckets[i]; if (current == nullptr) { printf("(空)"); } else { while (current) { printf("[%s:%d]", current->key, current->val); if (current->next) { printf(" -> "); } current = current->next; } } printf("\n"); } printf("================================\n"); } // 9. 清空哈希表 void clearhashtable(hashtable* table) { assert(table != nullptr); for (int i = 0; i < table->capacity; i++) { hashnode* current = table->buckets[i]; while (current) { hashnode* next = current->next; freeNode(current); current = next; } table->buckets[i] = nullptr; } table->size = 0; } // 10. 获取当前负载因子 double getLoadFactor(hashtable* table) { assert(table != nullptr); return (double)table->size / table->capacity; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/3 7:31:45

YOLO目标检测API上线!按token调用,低成本接入

YOLO目标检测API上线&#xff01;按token调用&#xff0c;低成本接入 在智能制造车间的流水线上&#xff0c;一台工业相机每秒捕捉数十帧图像&#xff0c;传统视觉系统需要部署昂贵的工控机和专职算法工程师来维护——而现在&#xff0c;只需三行代码、几分钱token&#xff0c;…

作者头像 李华
网站建设 2026/5/8 5:43:09

论文阅读(十二月第四周)

标题 A Physics-informed deep neural network for the joint prediction of 3D chlorophyll-a and hydrographic fields in the Mediterranean Sea 背景 作者 Michela Sammartino&#xff0c;Lorenzo Della Cioppa, Simone Colella,Bruno Buongiorno Nardelli 期刊来源 Else…

作者头像 李华
网站建设 2026/5/1 9:44:36

YOLOv8-Detect-V8改进版:主干网络再优化

YOLOv8-Detect-V8改进版&#xff1a;主干网络再优化 在工业质检线上&#xff0c;一个微小的电子元件缺失可能意味着整批产品返工&#xff1b;在高速公路上&#xff0c;自动驾驶系统对远处车辆的漏检可能带来严重后果。这些现实场景不断向目标检测算法提出更严苛的要求——既要看…

作者头像 李华
网站建设 2026/5/1 18:30:55

YOLO目标检测准确率提升技巧:混合精度训练+GPU支持

YOLO目标检测准确率提升技巧&#xff1a;混合精度训练GPU支持 在工业自动化、智能安防和自动驾驶等场景中&#xff0c;实时目标检测早已不再是“能不能做”的问题&#xff0c;而是“能不能快且准地做”的工程挑战。面对海量图像数据和严苛的响应延迟要求&#xff0c;YOLO&#…

作者头像 李华
网站建设 2026/5/5 22:08:53

YOLO训练时GPU显存爆了?常见问题与解决方案汇总

YOLO训练时GPU显存爆了&#xff1f;常见问题与解决方案汇总 在部署一个实时缺陷检测系统时&#xff0c;工程师小李信心满满地启动YOLOv8的训练脚本&#xff0c;结果几秒后终端弹出熟悉的红色错误&#xff1a; CUDA out of memory. Tried to allocate 256.00 MiB...这不是个例。…

作者头像 李华
网站建设 2026/5/3 4:19:05

YOLO目标检测准确率低?可能是数据和GPU协同出了问题

YOLO目标检测准确率低&#xff1f;可能是数据和GPU协同出了问题 在工业质检线上&#xff0c;一张高清图像从相机捕获到缺陷判定本应只需几毫秒。但现实往往是&#xff1a;模型推理明明很快&#xff0c;整体延迟却居高不下&#xff1b;训练日志显示loss震荡剧烈&#xff0c;最终…

作者头像 李华