数据结构——哈希表使用

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

主函数 ++main.c++↓↓↓↓↓

cpp 复制代码
#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 1
	if (find_hashtable(parray, num))
	{
		printf("%s 单词存在\n", num);
	}
	else 
	{
		printf("%s 节点不存在\n", num);
	}
#endif

	printf("请输入要删除的单词:\n");
	gets(num);

	delete_hashtable(parray, num);
	printf("已删除 %s 此单词\n", num);

	show_hashtable(parray);

	destroy_hashtable(parray);
	parray = NULL;

	return 0;
}

封装函数 ↓↓↓↓↓

cpp 复制代码
#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;
}

结构体函数↓↓↓↓↓

cpp 复制代码
#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__

#include "list.h"

typedef struct hashnode
{
    struct list_head node;
    char str[32];
}hashnode_t;

#define MAXNUM      52

extern 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
相关推荐
雾里看山1 小时前
顺序表VS单链表VS带头双向循环链表
数据结构·链表
好好研究4 小时前
学习栈和队列的插入和删除操作
数据结构·学习
挺菜的6 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
2401_858286117 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8368 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
学不动CV了11 小时前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
运器12313 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
算法_小学生13 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
岁忧13 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
Wo3Shi4七15 小时前
哈希冲突
数据结构·算法·go