#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
C语言代码哈希表Hashmap
于 2022-09-11 21:34:38 首次发布