数据结构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.托普利茨矩阵 四【题目描述…...
SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...
【JVM】- 内存结构
引言 JVM:Java Virtual Machine 定义:Java虚拟机,Java二进制字节码的运行环境好处: 一次编写,到处运行自动内存管理,垃圾回收的功能数组下标越界检查(会抛异常,不会覆盖到其他代码…...
成都鼎讯硬核科技!雷达目标与干扰模拟器,以卓越性能制胜电磁频谱战
在现代战争中,电磁频谱已成为继陆、海、空、天之后的 “第五维战场”,雷达作为电磁频谱领域的关键装备,其干扰与抗干扰能力的较量,直接影响着战争的胜负走向。由成都鼎讯科技匠心打造的雷达目标与干扰模拟器,凭借数字射…...
uniapp中使用aixos 报错
问题: 在uniapp中使用aixos,运行后报如下错误: AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...
听写流程自动化实践,轻量级教育辅助
随着智能教育工具的发展,越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式,也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建,…...
用机器学习破解新能源领域的“弃风”难题
音乐发烧友深有体会,玩音乐的本质就是玩电网。火电声音偏暖,水电偏冷,风电偏空旷。至于太阳能发的电,则略显朦胧和单薄。 不知你是否有感觉,近两年家里的音响声音越来越冷,听起来越来越单薄? —…...
【笔记】WSL 中 Rust 安装与测试完整记录
#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统:Ubuntu 24.04 LTS (WSL2)架构:x86_64 (GNU/Linux)Rust 版本:rustc 1.87.0 (2025-05-09)Cargo 版本:cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...
现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?
现有的 Redis 分布式锁库(如 Redisson)相比于开发者自己基于 Redis 命令(如 SETNX, EXPIRE, DEL)手动实现分布式锁,提供了巨大的便利性和健壮性。主要体现在以下几个方面: 原子性保证 (Atomicity)ÿ…...
如何更改默认 Crontab 编辑器 ?
在 Linux 领域中,crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用,用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益,允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...
mac 安装homebrew (nvm 及git)
mac 安装nvm 及git 万恶之源 mac 安装这些东西离不开Xcode。及homebrew 一、先说安装git步骤 通用: 方法一:使用 Homebrew 安装 Git(推荐) 步骤如下:打开终端(Terminal.app) 1.安装 Homebrew…...
