news 2026/6/10 15:58:34

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

作者头像

张小明

前端开发工程师

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/6/9 7:47:06

YOLO在智能零售场景的应用:GPU边缘盒子方案

YOLO在智能零售场景的应用&#xff1a;GPU边缘盒子方案 在连锁便利店、无人货架和智慧商超日益普及的今天&#xff0c;如何实时掌握店内动态——从商品陈列是否完整&#xff0c;到顾客行为是否有异常——已成为提升运营效率与防损能力的关键。传统的视频监控依赖人工回看&#…

作者头像 李华
网站建设 2026/6/10 12:58:25

YOLO + GPU加速:打造工业视觉检测的黄金组合

YOLO GPU加速&#xff1a;打造工业视觉检测的黄金组合 在现代工厂的高速生产线上&#xff0c;每分钟都有成百上千个工件经过质检环节。一个微小的划痕、一次错位装配&#xff0c;都可能引发后续产品的大规模返修甚至召回。传统依靠人工目检或基于规则图像处理的方式早已不堪重…

作者头像 李华
网站建设 2026/6/7 18:05:11

YOLO开源社区最火项目TOP10,全部支持GPU加速

YOLO开源社区最火项目TOP10&#xff0c;全部支持GPU加速 在智能制造、城市大脑和自动驾驶等前沿领域&#xff0c;实时视觉感知正成为系统决策的“眼睛”。而在这双眼睛背后&#xff0c;YOLO系列模型几乎无处不在——从工厂流水线上的缺陷检测&#xff0c;到十字路口的车流统计&…

作者头像 李华
网站建设 2026/6/9 22:21:06

YOLO模型太大无法部署?教你用GPU+TensorRT极致压缩

YOLO模型太大无法部署&#xff1f;教你用GPUTensorRT极致压缩 在工业质检线上&#xff0c;每秒流过数十个零件&#xff0c;系统必须在几十毫秒内完成缺陷检测&#xff1b;在自动驾驶车辆中&#xff0c;摄像头实时捕捉道路画面&#xff0c;任何延迟都可能带来安全隐患。这些场景…

作者头像 李华
网站建设 2026/6/10 11:11:23

Next AI Draw.io 核心实现深度分析

Next AI Draw.io 核心实现深度分析 请关注公众号【碳硅化合物AI】 前言 大家好&#xff01;上一篇我们聊了项目的整体架构&#xff0c;今天咱们深入代码&#xff0c;看看这 8 个核心模块是怎么实现的。我会从入口类开始&#xff0c;分析关键类的关系&#xff0c;然后用时序图…

作者头像 李华