当前位置: 首页 > 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;
}
http://www.lryc.cn/news/107094.html

相关文章:

  • vue3引用Font-Awesome字体图标库
  • Python: Django 服务部署可能遇到的一些问题
  • Python爬虫时遇到连接超时解决方案
  • 这所国字头双一流,根本招不满,学硕都没人报!
  • macos 查询端口占用 命令
  • 无代码开发:打破传统开发模式,引领数字化转型新方向
  • go-zero超强工具goctl的常用命令api,rpc,model及其构建的服务解析
  • 手机python编程软件怎么用,手机python编程软件下载
  • 【使用 DSP 滤波器加速速度和位移】使用信号处理算法过滤加速度数据并将其转换为速度和位移研究(Matlab代码实现)
  • 家居行业解决方案 | 君子签电子签约助力家居企业减负增效
  • Nodejs 第五章(Npm run 原理)
  • 150. 逆波兰表达式求值
  • js中的设计模式
  • PostgreSQL:string_agg 多列值聚合成一列
  • 通向架构师的道路之apache_tomcat_https应用
  • iOS——锁与死锁问题
  • 恒运资本:股票总市值是什么意思?
  • Selenium Chrome Webdriver 如何获取 Youtube 悬停文本
  • 【LeetCode每日一题】——766.托普利茨矩阵
  • 第三方材料检测实验室LIMS系统源码 lims源码
  • 【数据结构与算法——TypeScript】数组、栈、队列、链表
  • [运维|中间件] Apache APISIX使用笔记
  • Android Intent 使用(详细版)
  • 【Clion 2】多行TODO使用
  • 【运维】hive 终端突然不能使用:Hive Schema version does not match metastore‘s schema version
  • P1049 [NOIP2001 普及组] 装箱问题
  • QCustomPlot获取选点坐标
  • Qt配置Android开发
  • 花费7元训练自己的GPT 2模型
  • 性能分析工具