数据结构——哈希表使用

embedded/2025/2/21 0:10:01/

        目标:利用哈希表存放若干个单词,用户输入某个单词,查询在哈希表中是否存在该单词

主函数 main.c ↓↓↓↓↓

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"int main(void)
{struct list_head *parray = NULL;int i = 0;char str[5][32] = {0};char num[32] = {0};char cnt[32] = {0};for(i = 0; i < 5; i++){gets(str[i]);}parray = create_hashtable();for(i = 0; i < 5; i++){insert_hashtable(parray, str[i]);}show_hashtable(parray);printf("请输入要查询的单词:\n");gets(num);#if 1if (find_hashtable(parray, num)){printf("%s 单词存在\n", num);}else {printf("%s 节点不存在\n", num);}
#endifprintf("请输入要删除的单词:\n");gets(num);delete_hashtable(parray, num);printf("已删除 %s 此单词\n", num);show_hashtable(parray);destroy_hashtable(parray);parray = NULL;return 0;
}

封装函数 ↓↓↓↓↓

#include "hashtable.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>//创建
struct list_head *create_hashtable(void)
{   int i = 0;struct list_head *ptmd = NULL;ptmd = malloc(MAXNUM * sizeof(struct list_head));if(NULL == ptmd){return NULL;}for(i = 0; i < MAXNUM; i++){INIT_LIST_HEAD(&ptmd[i]);}return ptmd;
}//判断
static int asc_compare(struct list_head *pnew, struct list_head *pnode)
{return strcmp((list_entry(pnew, hashnode_t, node)->str), (list_entry(pnode, hashnode_t, node)->str));
}//插入
int insert_hashtable(struct list_head *pheadlist, char *pstr)
{int key = 0;hashnode_t *ptmd = NULL;ptmd = malloc(sizeof(hashnode_t));if(NULL == ptmd){return -1;}strcpy(ptmd->str, pstr);if(pstr[1] >= 'A' && pstr[1] <= 'Z'){key = *pstr - 'A';}else if(pstr[1] >= 'a' && pstr[1] <= 'z'){key = *pstr - 'a' + 26;}list_add_order(&ptmd->node, &pheadlist[key], asc_compare);return 0;
}//打印
int show_hashtable(struct list_head *pheadlist)
{int i = 0;struct list_head *ptmpnode = NULL;for (i = 0; i < MAXNUM; i++){if(i >= 0 && i < 26){printf("%c: ", (char)i + 'A');//或者+65}else{printf("%c: ", (char)i + 'a' - 26);//或者+71}list_for_each(ptmpnode, &pheadlist[i]){printf(" %s----", list_entry(ptmpnode, hashnode_t, node)->str);}printf("\n");}return 0;
}//查找
hashnode_t *find_hashtable(struct list_head *pheadlist, char *pstr)
{int key = 0;struct list_head *ptmpnode = NULL;if(pstr[1] >= 'A' && pstr[1] <= 'Z'){key = *pstr - 'A';}else if(pstr[1] >= 'a' && pstr[1] <= 'z'){key = *pstr - 'a' + 26;}list_for_each(ptmpnode, &pheadlist[key]){if (strcmp(list_entry(ptmpnode, hashnode_t, node)->str, pstr) == 0){return list_entry(ptmpnode, hashnode_t, node);}else if (list_entry(ptmpnode, hashnode_t, node)->str > pstr){break;}}return NULL;
}//删除
int delete_hashtable(struct list_head *parray, char *pstr)
{hashnode_t *ptmpnode = NULL;int cnt = 0;while (1){ptmpnode = find_hashtable(parray, pstr);if (NULL == ptmpnode){break;}list_del(&ptmpnode->node);free(ptmpnode);cnt++;}return cnt;
}//销毁
int destroy_hashtable(struct list_head *parray)
{int i = 0;hashnode_t *ptmpnode = NULL;hashnode_t *pnextnode = NULL;for (i = 0; i < MAXNUM; i++){list_for_each_entry_safe(ptmpnode, pnextnode, &parray[i], node){free(ptmpnode);}}free(parray);return 0;
}

结构体函数↓↓↓↓↓

#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__#include "list.h"typedef struct hashnode
{struct list_head node;char str[32];
}hashnode_t;#define MAXNUM      52extern struct list_head *create_hashtable(void);
extern int insert_hashtable(struct list_head *pheadlist, char *pstr);
extern int show_hashtable(struct list_head *pheadlist);
extern hashnode_t *find_hashtable(struct list_head *pheadlist, char *pstr);
extern int delete_hashtable(struct list_head *parray, char *pstr);
extern int destroy_hashtable(struct list_head *parray);#endif


http://www.ppmy.cn/embedded/163138.html

相关文章

Spring Boot 从 2.7.x 升级到 3.3注意事项

将 Spring Boot 从 2.7.x 升级到 3.3 是一个重要的迁移过程&#xff0c;特别是因为 Spring Boot 3.x 系列基于 Jakarta EE 9&#xff0c;而不再使用 Java EE。此版本升级伴随着许多重大变化&#xff0c;以下是你在升级过程中需要注意的关键事项&#xff1a; 1. JDK 版本升级 …

Qt——连接MySQL数据库之编译数据库驱动的方法详细总结(各版本大同小异,看这一篇就够了)

【系列专栏】:博主结合工作实践输出的,解决实际问题的专栏,朋友们看过来! 《项目案例分享》 《极客DIY开源分享》 《嵌入式通用开发实战》 《C++语言开发基础总结》 《从0到1学习嵌入式Linux开发》 《QT开发实战》 《Android开发实战》 《实用硬件方案设计》 《结构建模设…

运用Deek Seeker协助数据分析

我的数据源有两张表&#xff0c;一个是每日销售表(字段有日期、产品名称、实际销量)&#xff0c;一个是每月目标表(字段有年度月份、产品名称、目标销量);我的需求是&#xff0c;按月、按年来统计每个产品的目标完成情况请问用PowerBl进行分析&#xff0c;应该如何建立数据模型…

Datawhale 数学建模导论二 笔记1

第6章 数据处理与拟合模型 本章主要涉及到的知识点有&#xff1a; 数据与大数据Python数据预处理常见的统计分析模型随机过程与随机模拟数据可视化 本章内容涉及到基础的概率论与数理统计理论&#xff0c;如果对这部分内容不熟悉&#xff0c;可以参考相关概率论与数理统计的…

浅谈无人机群技术的作战应用与战略意义

近年来&#xff0c;无人机技术在军事领域的应用迅速崛起&#xff0c;成为现代战争中不可或缺的力量倍增器。无人机群技术&#xff0c;即通过集群控制系统使多个无人机在相同或不同任务环境下协同工作&#xff0c;形成一种集体作战、智能化操作的新模式&#xff0c;更是将这一趋…

Centos安装php-8.0.24.tar

查看系统环境 cat /etc/redhat-release 预先安装必要的依赖 yum install -y \ wget \ gcc \ gcc-c \ autoconf \ automake \ libtool \ make \ libxml2 \ libxml2-devel \ openssl \ openssl-devel \ sqlite-devel yum update 1、下载解压 cd /data/ wget https:/…

python连点器

要实现一个用于抖音点赞的鼠标连点工具&#xff0c;可以通过编程或现有软件实现。以下是两种常见方法&#xff08;但请注意&#xff1a;频繁自动化操作可能违反平台规则&#xff0c;需谨慎使用&#xff09;&#xff1a; 方法 1&#xff1a;使用现成工具&#xff08;如 AutoClic…

PHP关键字入门指南:分类与功能全解析

如果你是刚接触PHP的新手,可能会对代码中那些“特殊单词”感到困惑。别担心!本文将用最通俗易懂的方式,带你认识PHP中的关键字——它们就像编程世界的“魔法咒语”,每个都有独特的作用。文末还附有代码示例,帮你快速上手! 一、什么是PHP关键字? PHP关键字是语言内置的特…