当前位置: 首页 > news >正文

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

头文件

#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; }
http://www.jsqmd.com/news/154304/

相关文章:

  • YOLO模型训练中断?自动恢复机制+GPU容错部署
  • ‌移动性能测试:5G时代的优化技巧
  • 利用showapi在线查询快递
  • 基于Java+SpringBoot的技术的电商精准营销推荐系统(源码+讲解视频+LW)
  • 基于Java+SpringBoot的见山茶食酒馆网站系统(源码+讲解视频+LW)
  • 面试官:如何在 Kafka 中实现延迟消息?
  • Java线程简介
  • YOLO训练超参数调优:贝叶斯搜索+多GPU并行
  • mshtmpgr.dll损坏丢失找不到 打不开程序问题 下载方法
  • Java线程的启动及操作
  • msidcrl40.dll损坏丢失找不到 打不开程序问题 下载方法
  • 小学生0基础学大语言模型应用(第7课 《分支结构:如果魔法门》)
  • YOLOv10引入动态标签分配,对GPU计算有何影响?
  • Docker Compose 部署 MySQL 多实例 日常运维全指南-补充
  • 基于Java+SpringBoot的服装销售管理系统的设计与实现(源码+讲解视频+LW)
  • YOLO目标检测支持多语言标签?GPU加速文本渲染
  • 利用showapi提供的接口,根据地名查询天气预报
  • msimg32.dll损坏丢失找不到 打不开软件问题 下载方法
  • 仿照天气预报,制作一个前端页面,显示快递的至少2个指标
  • 开发中,2个项目A和B,A如何不引用B项目或者动态库,从而实现B的功能
  • 2025对称道岔资深厂商TOP5权威推荐:精准选型指南,助力轨道工程安全高效 - mypinpai
  • 7款免费AI论文工具实测:1小时出初稿+真实文献,轻松搞定毕业
  • 基于Java+SpringBoot的高校机动车认证信息管理系统(源码+讲解视频+LW)
  • YOLO目标检测为何如此高效?深度剖析其单阶段架构优势
  • 利用showapi提供的接口,根据地名查询快递
  • 如何请求和响应HTTP
  • 语言与智能的新见解
  • YOLO模型导出为engine文件?TensorRT + GPU流程详解
  • 2025年哈尔滨瓷砖建材企业服务能力TOP5推荐:凯联盛建材的安装难度大吗? - myqiye
  • 2025-2026年中国高端顶尖工业设计品牌:医疗器械/医疗仪器/医疗设备/机器人/实验室设备/工业设备/家电设备外观设计品牌推荐 - 匠子网络