#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 i;
if(p_stElement == NULL && p_stNextNode != NULL)
{
for(i = 0; i < p_u32Len; i++)
{
StNodeSingleIntial(&(*p_stNode)[i], &p_stNextNode[i], NULL, NULL);
}
}
else if(p_stElement != NULL && p_stNextNode == NULL)
{
for(i = 0; i < p_u32Len; i++)
{
StNodeSingleIntial(&(*p_stNode)[i], NULL, p_stElement[i].m_ps8Key, p_stElement[i].m_ps8Value);
}
}
else if(p_stElement != NULL && p_stNextNode != NULL)
{
for(i = 0; i < p_u32Len; i++)
{
StNodeSingleIntial(&(*p_stNode)[i], &p_stNextNode[i], p_stElement[i].m_ps8Key, p_stElement[i].m_ps8Value);
}
}
else if(p_stElement == NULL && p_stNextNode == NULL)
{
}
}
void NewZeroMulStNode(StNode **p_stNode, unsigned int p_u32Len)
{
*p_stNode = (StNode *)calloc(p_u32Len, sizeof(StNode));
}
//delete节点数组
void DeleteMulStNode(StNode *p_stNode,
unsigned int p_u32len,
unsigned char p_u8DeleteRearFlg)
{
int i;
for(i = 0; i < p_u32len; i++)
{
if(p_u8DeleteRearFlg == 1)
{
DeleteRearStNode(&p_stNode[i]);
DestroySingleStNode(&p_stNode[i]);
}
else
{
DestroySingleStNode(&p_stNode[i]);
}
}
free(p_stNode);
}
bool InitialTable(StHashTable *p_stTable, int p_s32TableSize)
{
if(p_stTable == NULL)
{
return false;
}
p_stTable->m_s32Size = p_s32TableSize;
p_stTable->m_s32Count = 0;
NewMulStNode(&p_stTable->m_pstHead, NULL, NULL, p_s32TableSize);
//NewZeroMulStNode(&p_stTable->m_pstHead, p_s32TableSize);
return true;
}
void FreeHashTable(StHashTable *p_stTable)
{
int i;
DeleteMulStNode(p_stTable->m_pstHead, p_stTable->m_s32Size, 1);
}
int HashFunction(unsigned int p_u32TableSize, const char *p_s8Key)
{
int i;
int l_s32Sum = 0;
for(i = 0; i < strlen(p_s8Key); i++)
{
l_s32Sum += p_s8Key[i];
}
return l_s32Sum % p_u32TableSize;
}
StNode *SearchTable(StHashTable *p_stTable, const char *p_s8Key)
{
if(p_s8Key == NULL)
{
return NULL;
}
int l_s32Idx = HashFunction(p_stTable->m_s32Size, p_s8Key);
int i;
StNode *l_pstNode;
for(l_pstNode = &p_stTable->m_pstHead[l_s32Idx];
l_pstNode->m_pstNext;
l_pstNode = l_pstNode->m_pstNext)
{
if(l_pstNode->m_pstNext->m_stElement.m_ps8Key
&& strcmp(l_pstNode->m_pstNext->m_stElement.m_ps8Key, p_s8Key) == 0)
{
break;
}
}
return l_pstNode->m_pstNext;
}
void ReHash(StHashTable *p_stTable)
{
int i,j;
int l_s32Sum = 0;
int l_s32NewIdx;
int l_s32NewSize = (p_stTable->m_s32Size + 1) * 2;
printf("ReHash Count:%d\n", l_s32NewSize);
StNode *l_pstNewHead;
NewMulStNode(&l_pstNewHead, NULL, NULL, l_s32NewSize);
//NewZeroMulStNode(&l_pstNewHead, l_s32NewSize);
for(i = 0; i < p_stTable->m_s32Size; i++)
{
StNode *l_pstNode;
for(l_pstNode = p_stTable->m_pstHead[i].m_pstNext;
l_pstNode;
l_pstNode = l_pstNode->m_pstNext
)
{
l_s32NewIdx = HashFunction(l_s32NewSize, l_pstNode->m_stElement.m_ps8Key);
StNode *l_pstNewNode;
// NewSingleStNode(&l_pstNewNode,
// l_pstNewHead[l_s32NewIdx].m_pstNext,
// (const char*)l_pstNode->m_stElement.m_ps8Key,
// (const char*) l_pstNode->m_stElement.m_ps8Value);
NewMulStNode(&l_pstNewNode,
&l_pstNode->m_stElement,
l_pstNewHead[l_s32NewIdx].m_pstNext,
1);
l_pstNewHead[l_s32NewIdx].m_pstNext = l_pstNewNode;
}
}
FreeHashTable(p_stTable);
p_stTable->m_s32Size = l_s32NewSize;
p_stTable->m_pstHead = l_pstNewHead;
}
int InsertNode(StHashTable *p_stTable, const char *p_s8Key, const char *p_s8Value)
{
StNode *l_pstExistNode = SearchTable(p_stTable, p_s8Key);
if(l_pstExistNode != NULL)
{
printf("元素已经存在\n");
return 0;
}
if(p_stTable->m_s32Count > p_stTable->m_s32Size * 0.75)
{
ReHash(p_stTable);
}
int l_s32Idx = HashFunction(p_stTable->m_s32Size, p_s8Key);
StNode *l_pstNode;
NewSingleStNode(&l_pstNode,
p_stTable->m_pstHead[l_s32Idx].m_pstNext,
p_s8Key,
p_s8Value);
p_stTable->m_pstHead[l_s32Idx].m_pstNext = l_pstNode;
p_stTable->m_s32Count++;
return 1;
}
void PrintHashMap(StHashTable *p_stTable)
{
int i;
StNode *l_pstNode;
for(i = 0; i < p_stTable->m_s32Size; i++)
{
for(l_pstNode = &p_stTable->m_pstHead[i];
l_pstNode->m_pstNext;
l_pstNode = l_pstNode->m_pstNext
)
{
printf("[%d] Key=%s Value=%s\n", i, l_pstNode->m_pstNext->m_stElement.m_ps8Key, l_pstNode->m_pstNext->m_stElement.m_ps8Value);
}
}
printf("\n");
}
int DealNode(StHashTable *p_stTable, const char *key)
{
int l_s32Idx = HashFunction(p_stTable->m_s32Size, key);
StNode *l_pstNode;
for(l_pstNode = &p_stTable->m_pstHead[l_s32Idx];
l_pstNode->m_pstNext;
l_pstNode = l_pstNode->m_pstNext
)
{
if(l_pstNode->m_pstNext->m_stElement.m_ps8Key
&& strcmp(l_pstNode->m_pstNext->m_stElement.m_ps8Key, key) == 0)
{
printf("Deal Node key:%s, Info:%s\n", l_pstNode->m_pstNext->m_stElement.m_ps8Key, l_pstNode->m_pstNext->m_stElement.m_ps8Value);
StNode *l_pstTmpNode = l_pstNode->m_pstNext; //l_pstNode的后面是删除的节点
l_pstNode->m_pstNext = l_pstTmpNode->m_pstNext;
//DeleteSingleStNode(l_pstTmpNode);
DeleteMulStNode(l_pstTmpNode,1,0);
p_stTable->m_s32Count--;
return 1;
}
}
return 0;
}
int main(int argc, char const *argv[])
{
StHashTable l_pHashTable;
InitialTable(&l_pHashTable, 10);
InsertNode(&l_pHashTable , "Key1", "KeyInfo1");
InsertNode(&l_pHashTable , "Key2", "KeyInfo2");
InsertNode(&l_pHashTable , "Key3", "KeyInfo3");
InsertNode(&l_pHashTable , "Key4", "KeyInfo4");
InsertNode(&l_pHashTable , "Key5", "KeyInfo5");
InsertNode(&l_pHashTable , "Key6", "KeyInfo6");
InsertNode(&l_pHashTable , "Key7", "KeyInfo7");
InsertNode(&l_pHashTable , "Key8", "KeyInfo8");
PrintHashMap(&l_pHashTable);
InsertNode(&l_pHashTable , "Key9", "KeyInfo9");
InsertNode(&l_pHashTable , "Key10", "KeyInfo10");
InsertNode(&l_pHashTable , "Key11", "KeyInfo11");
InsertNode(&l_pHashTable , "Key12", "KeyInfo12");
PrintHashMap(&l_pHashTable);
InsertNode(&l_pHashTable , "Key13", "KeyInfo13");
InsertNode(&l_pHashTable , "Key14", "KeyInfo14");
InsertNode(&l_pHashTable , "Key15", "KeyInfo15");
InsertNode(&l_pHashTable , "Key16", "KeyInfo16");
PrintHashMap(&l_pHashTable);
InsertNode(&l_pHashTable , "Key17", "KeyInfo17");
InsertNode(&l_pHashTable , "Key18", "KeyInfo18");
InsertNode(&l_pHashTable , "Key19", "KeyInfo19");
InsertNode(&l_pHashTable , "Key20", "KeyInfo20");
InsertNode(&l_pHashTable , "Key21", "KeyInfo21");
InsertNode(&l_pHashTable , "Key22", "KeyInfo22");
PrintHashMap(&l_pHashTable);
DealNode(&l_pHashTable , "Key4");
PrintHashMap(&l_pHashTable );
StNode *node = SearchTable(&l_pHashTable , "Key3");
if (node != NULL)
{
printf("find:%s-%s\n", node->m_stElement.m_ps8Key, node->m_stElement.m_ps8Value);
}
StNode *node1 = SearchTable(&l_pHashTable , "Key4");
if (node1 != NULL)
{
printf("find:%s-%s\n", node1->m_stElement.m_ps8Key, node1->m_stElement.m_ps8Value);
}
printf("\n");
FreeHashTable(&l_pHashTable);
return 0;
}
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行
csdn文章推荐受影响解决办法10个字10行