news 2026/4/30 16:39:46

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

作者头像

张小明

前端开发工程师

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/4/24 10:31:01

mshtmled.dll损坏丢失找不到 打不开软件程序 下载方法

在使用电脑系统时经常会出现丢失找不到某些文件的情况&#xff0c;由于很多常用软件都是采用 Microsoft Visual Studio 编写的&#xff0c;所以这类软件的运行需要依赖微软Visual C运行库&#xff0c;比如像 QQ、迅雷、Adobe 软件等等&#xff0c;如果没有安装VC运行库或者安装…

作者头像 李华
网站建设 2026/4/18 2:29:52

YOLO模型部署跨平台?CUDA版本兼容性全解析

YOLO模型部署跨平台&#xff1f;CUDA版本兼容性全解析 在工业质检线上&#xff0c;一台搭载RTX 3060的边缘设备正实时分析传送带上的产品缺陷&#xff1b;同一时刻&#xff0c;数据中心内的A100集群正在处理数千路监控视频流。它们运行着同一个YOLO模型&#xff0c;却面临截然不…

作者头像 李华
网站建设 2026/4/23 20:48:34

YOLO + Triton推理服务器:构建高并发检测服务

YOLO Triton推理服务器&#xff1a;构建高并发检测服务 在智能制造车间的质检线上&#xff0c;数十台高清摄像头正以每秒30帧的速度持续拍摄产品图像。后台系统需要在毫秒级内判断每个零部件是否存在划痕、缺损或装配偏差——这不仅是对算法精度的考验&#xff0c;更是对整个A…

作者头像 李华
网站建设 2026/4/23 12:50:36

(38)基于XML配置方式的AOP(了解)

第一步&#xff1a;编写目标类 package com.powernode.spring6.service;// 目标类 public class VipService {public void add(){System.out.println("保存vip信息。");} }第二步&#xff1a;编写切面类&#xff0c;并且编写通知 package com.powernode.spring6.serv…

作者头像 李华
网站建设 2026/4/24 16:17:10

YOLO工业部署难点解析:从模型导出到GPU推理优化

YOLO工业部署难点解析&#xff1a;从模型导出到GPU推理优化 在现代智能制造产线中&#xff0c;视觉检测系统早已不再是“锦上添花”的辅助模块&#xff0c;而是决定良品率与自动化水平的核心环节。一条每分钟处理数百件产品的流水线上&#xff0c;留给目标检测算法的响应时间往…

作者头像 李华
网站建设 2026/4/29 13:14:53

ITU-T G.729 语音编解码器负载格式、传输机制与性能深度分析报告

ITU-T G.729 语音编解码器负载格式、传输机制与性能深度分析报告 1. 引言&#xff1a;G.729 标准的演进与技术定位 1.1 标准化背景与电信级压缩的需求 在数字语音通信的发展历程中&#xff0c;ITU-T G.729 标准的发布标志着一个重要的转折点。20世纪90年代中期&#xff0c;随…

作者头像 李华