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

数据结构8-哈希表

数据结构8-哈希表

  1. 动态分配内存方式:
#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-哈希表 动态分配内存方式&#xff1a; #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字体图标库

环境&#xff1a;vue3tsviteelement plus 介绍&#xff1a;这里安装引用的是Font-Awesome 6.x 版本&#xff0c;有专业版&#xff08;付费&#xff09;&#xff0c;这里只介绍免费版字体使用方法 一、安装 1.使用npm安装&#xff0c;终端打开项目目录或者命令行cd到目录文件夹…...

Python: Django 服务部署可能遇到的一些问题

502 bad gateway 不要用 python3 manage.py runserver 启动服务&#xff0c; 而要用&#xff1a; daphne -b 0.0.0.0 -p <端口> <工程名>.asgi:application此外&#xff0c;在 setting.py 中&#xff0c;修改&#xff1a; import osSECRET_KEY os.environ.get(D…...

Python爬虫时遇到连接超时解决方案

在进行Python爬虫任务时&#xff0c;经常会遇到连接超时&#xff08;TimeoutError&#xff09;错误。连接超时意味着爬虫无法在规定的时间内建立与目标服务器的连接&#xff0c;导致请求失败。为了帮助您解决这个常见的问题&#xff0c;本文将提供一些解决办法&#xff0c;并提…...

这所国字头双一流,根本招不满,学硕都没人报!

一、学校及专业介绍 中国民航大学&#xff0c;位于天津市&#xff0c;是民航局、天津市、教育部共建高校&#xff0c;是天津市“双一流”建设高校和高水平特色大学建设高校。 1.1 招生情况 2023年中国民航大学电子信息与自动化学院&#xff0c;初试考806信号与系统的一共有两…...

macos 查询端口占用 命令

在 macOS 上查询端口占用的命令是通过使用lsof&#xff08;list open files&#xff09;工具来实现的。 lsof可以显示当前系统中打开的文件&#xff08;包括网络连接和端口&#xff09;的相关信息。 打开终端应用程序&#xff08;Terminal&#xff09;&#xff0c;然后输入以下…...

无代码开发:打破传统开发模式,引领数字化转型新方向

随着数字化转型的加速&#xff0c;企业对于高效、便捷的软件开发需求愈发旺盛。无代码开发作为一种新兴的软件开发模式&#xff0c;以其可视化、模块化的开发方式&#xff0c;为数字化转型提供了新的方向。本文将从无代码开发的优势、应用场景、如何实现等方面进行详细解读&…...

go-zero超强工具goctl的常用命令api,rpc,model及其构建的服务解析

goctl api 详情移步&#xff1a; go-zero的路由机制解析 基于go-zero的api服务刨析并对比与gin的区别 goctl rpc goctl支持多种rpc&#xff0c;较为流行的是google开源的grpc&#xff0c;这里主要介绍goctl rpc protoc的代码生成与使用。 protoc是grpc的命令&#xff0c;作用…...

手机python编程软件怎么用,手机python编程软件下载

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;手机python编程软件保存的代码在哪里&#xff0c;手机python编程软件怎么运行&#xff0c;现在让我们一起来看看吧&#xff01; 原标题&#xff1a;盘点几个在手机上可以用来学习编程的软件 前天在悟空问答的时候&#…...

【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

家居行业解决方案 | 君子签电子签约助力家居企业减负增效

过去&#xff0c;家居行业因供需两端碎片化、服务链条较长等因素&#xff0c;导致线上发展较为缓慢&#xff0c;近年来&#xff0c;互联网的发展推动直播电商、兴趣电商兴起&#xff0c;促使家居行业数字化建设需求越来越为迫切。 合同管理作为家居行业企业经营的一项重要管理…...

Nodejs 第五章(Npm run 原理)

npm run xxx 发生了什么 按照下面的例子npm run dev 举例过程中发生了什么 读取package json 的scripts 对应的脚本命令(dev:vite),vite是个可执行脚本&#xff0c;他的查找规则是&#xff1a; 先从当前项目的node_modules/.bin去查找可执行命令vite如果没找到就去全局的node…...

150. 逆波兰表达式求值

给你一个字符串数组 tokens &#xff0c;表示一个根据 逆波兰表示法 表示的算术表达式。 请你计算该表达式。返回一个表示表达式值的整数。 注意&#xff1a; 有效的算符为 、-、* 和 / 。 每个操作数&#xff08;运算对象&#xff09;都可以是一个整数或者另一个表达式。 两个…...

js中的设计模式

设计模式 代码整体的结构会更加清楚&#xff0c;管理起来会更加方便&#xff0c;更好地维护 设计模式是一种思想 发布订阅 模块化开发 导入很多模块 容器即数组存储未来要执行的方法&#xff0c;同addEventListener 数组塌陷问题* 由于删除了元素&#xff0c;导致从删除元素的位…...

PostgreSQL:string_agg 多列值聚合成一列

PostgreSQL:string_agg 多列值聚合成一列 string_agg是PostgreSQL中的一个聚合函数&#xff0c;用于将一组值连接为一个字符串。它接受两个参数&#xff1a;要连接的值和连接符。 语法如下&#xff1a; string_agg(expression, delimiter)其中&#xff0c;expression是要连接…...

通向架构师的道路之apache_tomcat_https应用

一、总结前一天的学习 通过上一章我们知道、了解并掌握了Web Server结合App Server是怎么样的一种架构&#xff0c;并且亲手通过Apache的Http Server与Tomcat6进行了整合的实验。 这样的架构的好处在于&#xff1a; 减轻App Server端的压力&#xff0c;用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…...

恒运资本:股票总市值是什么意思?

职业新手可能会疑惑地问&#xff0c;股票总市值到底是什么意思&#xff1f;究竟&#xff0c;这是普通出资者常常看到的词汇&#xff0c;要了解股票总市值的含义&#xff0c;是需求了解金融商场的基本概念的。 股票总市值简介 股票的总市值是由公司一切的股票的数量乘以现在的价…...

Selenium Chrome Webdriver 如何获取 Youtube 悬停文本

导语 Youtube 是一个非常流行的视频分享平台&#xff0c;有时候我们可能想要爬取一些视频的信息&#xff0c;比如标题、播放量、点赞数等。但是有些信息并不是直接显示在网页上的&#xff0c;而是需要我们将鼠标悬停在某个元素上才能看到&#xff0c;比如视频的时长、上传时间…...

【LeetCode每日一题】——766.托普利茨矩阵

文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目示例】六【题目提示】七【题目进阶】八【解题思路】九【时间频度】十【代码实现】十一【提交结果】 一【题目类别】 矩阵 二【题目难度】 简单 三【题目编号】 766.托普利茨矩阵 四【题目描述…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

【根据当天日期输出明天的日期(需对闰年做判定)。】2022-5-15

缘由根据当天日期输出明天的日期(需对闰年做判定)。日期类型结构体如下&#xff1a; struct data{ int year; int month; int day;};-编程语言-CSDN问答 struct mdata{ int year; int month; int day; }mdata; int 天数(int year, int month) {switch (month){case 1: case 3:…...

工程地质软件市场:发展现状、趋势与策略建议

一、引言 在工程建设领域&#xff0c;准确把握地质条件是确保项目顺利推进和安全运营的关键。工程地质软件作为处理、分析、模拟和展示工程地质数据的重要工具&#xff0c;正发挥着日益重要的作用。它凭借强大的数据处理能力、三维建模功能、空间分析工具和可视化展示手段&…...

基于数字孪生的水厂可视化平台建设:架构与实践

分享大纲&#xff1a; 1、数字孪生水厂可视化平台建设背景 2、数字孪生水厂可视化平台建设架构 3、数字孪生水厂可视化平台建设成效 近几年&#xff0c;数字孪生水厂的建设开展的如火如荼。作为提升水厂管理效率、优化资源的调度手段&#xff0c;基于数字孪生的水厂可视化平台的…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

数据链路层的主要功能是什么

数据链路层&#xff08;OSI模型第2层&#xff09;的核心功能是在相邻网络节点&#xff08;如交换机、主机&#xff09;间提供可靠的数据帧传输服务&#xff0c;主要职责包括&#xff1a; &#x1f511; 核心功能详解&#xff1a; 帧封装与解封装 封装&#xff1a; 将网络层下发…...

MySQL中【正则表达式】用法

MySQL 中正则表达式通过 REGEXP 或 RLIKE 操作符实现&#xff08;两者等价&#xff09;&#xff0c;用于在 WHERE 子句中进行复杂的字符串模式匹配。以下是核心用法和示例&#xff1a; 一、基础语法 SELECT column_name FROM table_name WHERE column_name REGEXP pattern; …...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

Docker 本地安装 mysql 数据库

Docker: Accelerated Container Application Development 下载对应操作系统版本的 docker &#xff1b;并安装。 基础操作不再赘述。 打开 macOS 终端&#xff0c;开始 docker 安装mysql之旅 第一步 docker search mysql 》〉docker search mysql NAME DE…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...