数据结构8-哈希表
数据结构8-哈希表
- 动态分配内存方式:
#include <stdio.h>
#include <stdlib.h>#define SIZE 20struct DataItem {int data; int key;
};struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;//获取键值
int hashCode(int key) {return key % SIZE;
}//查找
struct DataItem *search(int key) {//get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL) {if(hashArray[hashIndex]->key == key)return hashArray[hashIndex]; //go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;} return NULL;
}//插入
void insert(int key,int data) {struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));item->data = data; item->key = key;//get the hash int hashIndex = hashCode(key);//move in array until an empty or deleted cellwhile(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;}hashArray[hashIndex] = item;
}//删除
struct DataItem* delete(struct DataItem* item) {int key = item->key;//get the hash int hashIndex = hashCode(key);//move in array until an emptywhile(hashArray[hashIndex] != NULL) {if(hashArray[hashIndex]->key == key) {struct DataItem* temp = hashArray[hashIndex]; //assign a dummy item at deleted positionhashArray[hashIndex] = dummyItem; return temp;}//go to next cell++hashIndex;//wrap around the tablehashIndex %= SIZE;} return NULL;
}//打印全部
void display() {int i = 0;for(i = 0; i < SIZE; i++) {if(hashArray[i] != NULL && hashArray[i]->key != -1) {printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);} else {printf(" ~~ ");}}printf("\n");
}
//主函数
int main() {dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));dummyItem->data = -1; dummyItem->key = -1; insert(1, 20);insert(2, 70);insert(42, 80);insert(4, 25);insert(12, 44);insert(14, 32);insert(17, 11);insert(13, 78);insert(37, 97);insert(21, 88);display();item = search(37);if(item != NULL) {printf("Element found: %d\n", item->data);} else {printf("Element not found\n");}delete(item);item = search(37);if(item != NULL) {printf("Element found: %d\n", item->data);} else {printf("Element not found\n");}item = search(1);printf("Element found: %d\n", item->data);item = search(21);printf("Element found: %d\n", item->data);
}
2.数组方式
#include <stdio.h>
#include <stdlib.h>#define SIZE 10
#define EMPTY -1struct DataItem {int data; int key;
} hashArray[SIZE];// Initialize the hash table
void init()
{int i;for (i = 0; i < SIZE; i++) {hashArray[i].key = EMPTY;}
}int hashCode(int key) {return key % SIZE;
}void insert(int key,int data) {int i=0;struct DataItem item;item.data = data; item.key = key;int hashIndex = hashCode(key);while (hashArray[hashIndex].key != EMPTY) {// Go to next cell++hashIndex;// Wrap around the tablehashIndex %= SIZE;if(i++ > SIZE){printf("data inset full-- key:%d,data:%d error\r\n",key,data);break;}}hashArray[hashIndex] = item;
}struct DataItem *search(int key) {int hashIndex = hashCode(key); while (hashArray[hashIndex].key != key) {// Go to next cell++hashIndex;// Wrap around the tablehashIndex %= SIZE;}return &hashArray[hashIndex];
}void display() {int i;for(i = 0; i<SIZE; i++) {if (hashArray[i].key != EMPTY){printf(" (%d,%d)",hashArray[i].key,hashArray[i].data);} else {printf(" ~~ ");}}printf("\n");
}int main() {init();insert(1, 20);insert(2, 70);insert(42, 80);insert(4, 25);insert(12, 44);insert(14, 32);insert(17, 11);insert(13, 78);insert(37, 97);insert(11, 1);insert(21, 3);display();struct DataItem *item;item = search(37);if(item != NULL) {printf("Element found: %d\n", item->data);} else {printf("Element not found\n");}item = search(2);printf("Element found: %d\n", item->data);item = search(12);printf("Element found: %d\n", item->data);return 0;
}相关文章:
数据结构8-哈希表
数据结构8-哈希表 动态分配内存方式: #include <stdio.h> #include <stdlib.h>#define SIZE 20struct DataItem {int data; int key; };struct DataItem* hashArray[SIZE]; struct DataItem* dummyItem; struct DataItem* item;//获取键值 int has…...
vue3引用Font-Awesome字体图标库
环境:vue3tsviteelement plus 介绍:这里安装引用的是Font-Awesome 6.x 版本,有专业版(付费),这里只介绍免费版字体使用方法 一、安装 1.使用npm安装,终端打开项目目录或者命令行cd到目录文件夹…...
Python: Django 服务部署可能遇到的一些问题
502 bad gateway 不要用 python3 manage.py runserver 启动服务, 而要用: daphne -b 0.0.0.0 -p <端口> <工程名>.asgi:application此外,在 setting.py 中,修改: import osSECRET_KEY os.environ.get(D…...
Python爬虫时遇到连接超时解决方案
在进行Python爬虫任务时,经常会遇到连接超时(TimeoutError)错误。连接超时意味着爬虫无法在规定的时间内建立与目标服务器的连接,导致请求失败。为了帮助您解决这个常见的问题,本文将提供一些解决办法,并提…...
这所国字头双一流,根本招不满,学硕都没人报!
一、学校及专业介绍 中国民航大学,位于天津市,是民航局、天津市、教育部共建高校,是天津市“双一流”建设高校和高水平特色大学建设高校。 1.1 招生情况 2023年中国民航大学电子信息与自动化学院,初试考806信号与系统的一共有两…...
macos 查询端口占用 命令
在 macOS 上查询端口占用的命令是通过使用lsof(list open files)工具来实现的。 lsof可以显示当前系统中打开的文件(包括网络连接和端口)的相关信息。 打开终端应用程序(Terminal),然后输入以下…...
无代码开发:打破传统开发模式,引领数字化转型新方向
随着数字化转型的加速,企业对于高效、便捷的软件开发需求愈发旺盛。无代码开发作为一种新兴的软件开发模式,以其可视化、模块化的开发方式,为数字化转型提供了新的方向。本文将从无代码开发的优势、应用场景、如何实现等方面进行详细解读&…...
go-zero超强工具goctl的常用命令api,rpc,model及其构建的服务解析
goctl api 详情移步: go-zero的路由机制解析 基于go-zero的api服务刨析并对比与gin的区别 goctl rpc goctl支持多种rpc,较为流行的是google开源的grpc,这里主要介绍goctl rpc protoc的代码生成与使用。 protoc是grpc的命令,作用…...
手机python编程软件怎么用,手机python编程软件下载
大家好,小编来为大家解答以下问题,手机python编程软件保存的代码在哪里,手机python编程软件怎么运行,现在让我们一起来看看吧! 原标题:盘点几个在手机上可以用来学习编程的软件 前天在悟空问答的时候&#…...
【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
家居行业解决方案 | 君子签电子签约助力家居企业减负增效
过去,家居行业因供需两端碎片化、服务链条较长等因素,导致线上发展较为缓慢,近年来,互联网的发展推动直播电商、兴趣电商兴起,促使家居行业数字化建设需求越来越为迫切。 合同管理作为家居行业企业经营的一项重要管理…...
Nodejs 第五章(Npm run 原理)
npm run xxx 发生了什么 按照下面的例子npm run dev 举例过程中发生了什么 读取package json 的scripts 对应的脚本命令(dev:vite),vite是个可执行脚本,他的查找规则是: 先从当前项目的node_modules/.bin去查找可执行命令vite如果没找到就去全局的node…...
150. 逆波兰表达式求值
给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。 请你计算该表达式。返回一个表示表达式值的整数。 注意: 有效的算符为 、-、* 和 / 。 每个操作数(运算对象)都可以是一个整数或者另一个表达式。 两个…...
js中的设计模式
设计模式 代码整体的结构会更加清楚,管理起来会更加方便,更好地维护 设计模式是一种思想 发布订阅 模块化开发 导入很多模块 容器即数组存储未来要执行的方法,同addEventListener 数组塌陷问题* 由于删除了元素,导致从删除元素的位…...
PostgreSQL:string_agg 多列值聚合成一列
PostgreSQL:string_agg 多列值聚合成一列 string_agg是PostgreSQL中的一个聚合函数,用于将一组值连接为一个字符串。它接受两个参数:要连接的值和连接符。 语法如下: string_agg(expression, delimiter)其中,expression是要连接…...
通向架构师的道路之apache_tomcat_https应用
一、总结前一天的学习 通过上一章我们知道、了解并掌握了Web Server结合App Server是怎么样的一种架构,并且亲手通过Apache的Http Server与Tomcat6进行了整合的实验。 这样的架构的好处在于: 减轻App Server端的压力,用Web Server来分压…...
iOS——锁与死锁问题
iOS中的锁 什么是锁锁的分类互斥锁1. synchronized2. NSLock3. pthread 递归锁1. NSRecursiveLock2. pthread 信号量Semaphore1. dispatch_semaphore_t2. pthread 条件锁1. NSCodition2. NSCoditionLock3. POSIX Conditions 分布式锁NSDistributedLock 读写锁1. dispatch_barri…...
恒运资本:股票总市值是什么意思?
职业新手可能会疑惑地问,股票总市值到底是什么意思?究竟,这是普通出资者常常看到的词汇,要了解股票总市值的含义,是需求了解金融商场的基本概念的。 股票总市值简介 股票的总市值是由公司一切的股票的数量乘以现在的价…...
Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
导语 Youtube 是一个非常流行的视频分享平台,有时候我们可能想要爬取一些视频的信息,比如标题、播放量、点赞数等。但是有些信息并不是直接显示在网页上的,而是需要我们将鼠标悬停在某个元素上才能看到,比如视频的时长、上传时间…...
【LeetCode每日一题】——766.托普利茨矩阵
文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目示例】六【题目提示】七【题目进阶】八【解题思路】九【时间频度】十【代码实现】十一【提交结果】 一【题目类别】 矩阵 二【题目难度】 简单 三【题目编号】 766.托普利茨矩阵 四【题目描述…...
AIVideo一站式AI长视频工具与Visual Studio的深度集成开发
AIVideo一站式AI长视频工具与Visual Studio的深度集成开发 1. 引言 作为一名长期使用Visual Studio进行开发的程序员,我经常遇到这样的痛点:想要录制一段代码演示视频,需要反复切换多个软件;想要制作项目介绍视频,得…...
京东抢购自动化全攻略:从入门到精通的技术实践指南
京东抢购自动化全攻略:从入门到精通的技术实践指南 【免费下载链接】JDspyder 京东预约&抢购脚本,可以自定义商品链接 项目地址: https://gitcode.com/gh_mirrors/jd/JDspyder 30秒快速评估:你是否需要JDspyder? 在决…...
RePKG终极指南:Wallpaper Engine资源提取与转换的完整解决方案
RePKG终极指南:Wallpaper Engine资源提取与转换的完整解决方案 【免费下载链接】repkg Wallpaper engine PKG extractor/TEX to image converter 项目地址: https://gitcode.com/gh_mirrors/re/repkg 你是否曾经遇到过这样的问题?在Wallpaper Eng…...
PDF-Extract-Kit-1.0保姆级部署教程:4090D单卡一键启动Jupyter实战
PDF-Extract-Kit-1.0保姆级部署教程:4090D单卡一键启动Jupyter实战 你是不是经常需要从PDF里提取表格、公式或者分析文档布局?手动操作不仅费时费力,还容易出错。今天,我要给你介绍一个神器——PDF-Extract-Kit-1.0。这是一个功能…...
Ostrakon-VL-8B效果展示:看AI如何从店铺图片中识别问题与机会
Ostrakon-VL-8B效果展示:看AI如何从店铺图片中识别问题与机会 1. 引言:当AI成为你的店铺巡检专家 想象一下这样的场景:你是一家连锁超市的运营经理,每天需要检查数十家门店的货架陈列、商品摆放和卫生状况。传统方法需要派遣大量…...
Java函数计算部署被低估的致命风险:类加载冲突、内存泄漏、上下文丢失——3个真实P0故障复盘
第一章:Java函数计算部署被低估的致命风险:类加载冲突、内存泄漏、上下文丢失——3个真实P0故障复盘在Serverless架构下,Java函数计算因其启动慢、内存占用高而常被“降级使用”,但更隐蔽的风险来自运行时环境的不可见性。我们复盘…...
Pylint魔法方法验证:10个技巧确保特殊方法符合Python规范的终极指南
Pylint魔法方法验证:10个技巧确保特殊方法符合Python规范的终极指南 【免费下载链接】pylint Its not just a linter that annoys you! 项目地址: https://gitcode.com/gh_mirrors/pyl/pylint Python开发者们,你是否曾为魔法方法(dund…...
GPEN快速上手教程:手机自拍模糊修复,30秒获取高清证件照
GPEN快速上手教程:手机自拍模糊修复,30秒获取高清证件照 你是不是也遇到过这种情况:急着要用证件照,翻遍手机相册却发现每张自拍都模糊不清?要么是光线太暗,要么是手抖拍糊了,要么就是像素太低…...
从‘虚拟’到‘物理’:程序员视角下的内存块、页框与页到底是怎么协作的?
从‘虚拟’到‘物理’:程序员视角下的内存块、页框与页到底是怎么协作的? 当你调试程序时遇到"Segmentation fault"或"Page fault"错误,是否好奇这些术语背后究竟发生了什么?作为开发者,我们每天都…...
虚拟内存 pagefile.sys 安全迁移教程|释放 3~8GB
摘要Windows 系统默认将虚拟内存(pagefile.sys)存放在 C 盘,长期占用 3~8GB 系统盘空间,不仅会加剧 C 盘爆满问题,还会增加磁盘读写压力,影响系统运行性能。本文整理 官方原生、安全无毒、无需第三方工具 的…...
