C语言代码哈希表Hashmap

本文介绍了使用CSDN文章中提供的哈希表结构实现,包括初始化、节点操作、搜索、扩容及删除功能。核心展示了StHashTable和StNode的数据结构,以及InsertNode和SearchTable等关键函数的使用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef struct
{
    char *m_ps8Key;
    char *m_ps8Value;
} StElement;


typedef struct StNode
{
    StElement m_stElement;
    struct StNode *m_pstNext;
} StNode;

typedef struct
{
    StNode *m_pstHead;
    int m_s32Size;
    int m_s32Count;
}StHashTable;

bool InitialStElement(StElement *p_stElement, const char* p_s8Key, const char* p_8Value)
{
    if(p_stElement == NULL || p_s8Key == NULL || p_8Value == NULL)
    {
        return false;
    }

    p_stElement->m_ps8Key = (char*)calloc(strlen(p_s8Key)+1,sizeof(char));
    strcpy(p_stElement->m_ps8Key, p_s8Key);
    p_stElement->m_ps8Value = (char*)calloc(strlen(p_8Value)+1,sizeof(char));
    strcpy(p_stElement->m_ps8Value, p_8Value);

    return true;
}

void FreeStElement(StElement *p_stElement)
{
    free(p_stElement->m_ps8Key);
    free(p_stElement->m_ps8Value);
}

//构造单个节点
void StNodeSingleIntial(StNode *p_stNode, StNode *p_stNodeNext, const char* p_s8Key, const char* p_8Value)
{
    InitialStElement(&p_stNode->m_stElement,p_s8Key, p_8Value);
    p_stNode->m_pstNext = p_stNodeNext;
}

//new单个节点
void NewSingleStNode(StNode **p_stNode, StNode *p_stNodeNext,  const char* p_s8Key, const char* p_8Value)
{
    *p_stNode = (StNode *)malloc(sizeof(StNode));
    StNodeSingleIntial(*p_stNode, p_stNodeNext, p_s8Key, p_8Value);
}

//析构单个节点
void DestroySingleStNode(StNode *p_stNewNode)
{   
    //printf("destroy:key%s,value:%s\n",p_stNewNode->m_stElement.m_ps8Key, p_stNewNode->m_stElement.m_ps8Value);
    FreeStElement(&p_stNewNode->m_stElement);
    p_stNewNode->m_pstNext = NULL;
}

//delete单个节点
void DeleteSingleStNode(StNode *p_stNewNode)
{
    DestroySingleStNode(p_stNewNode);
    free(p_stNewNode);
}

//删除该节点之后所有的节点
void DeleteRearStNode(StNode *p_stNewNode)
{
    StNode *l_pstNode;
    StNode *l_pstNextNode;
    if(p_stNewNode == NULL)
    {
        printf("节点为空\n");
        return;
    }
    for(l_pstNode = p_stNewNode->m_pstNext;
        l_pstNode != NULL;
        l_pstNode = l_pstNextNode)
    {
        l_pstNextNode = l_pstNode->m_pstNext;
        DeleteSingleStNode(l_pstNode);
    }

}
//new节点数组
void NewMulStNode(StNode **p_stNode, StElement *p_stElement, StNode *p_stNextNode, unsigned int p_u32Len)
{
    *p_stNode = (StNode *)calloc(p_u32Len, sizeof(StNode));
    int
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值