哈希表的分离链表法实现
为方便大家的学习,我已经将原码上传到了GitHub,欢迎大家下载。link。
·
哈希表的分离链表法实现
1、头文件
(哈希表的分离链表法实现.c)
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define MinTableSize 5
struct HashTable* initializeTable(int taleSize);
struct ListNode* find(int key, struct HashTable* h);
void insert(int key, struct HashTable* h);
struct ListNode* myDelete(int key, struct HashTable* h);
void myPrint(struct HashTable* h);
struct HashTable
{
int tableSize;
struct ListNode** theLists;
};
struct ListNode
{
int element;
struct ListNode* next;
};
2、main函数
#include "哈希表的分离链接法实现.h"
int main()
{
struct HashTable* hashTable = initializeTable(11);
insert(123, hashTable);
insert(99, hashTable);
insert(11, hashTable);
insert(78, hashTable);
insert(897, hashTable);
insert(340, hashTable);
insert(77, hashTable);
insert(993, hashTable);
insert(244, hashTable);
insert(423, hashTable);
insert(1456, hashTable);
insert(1233, hashTable);
insert(904, hashTable);
insert(234, hashTable);
myPrint(hashTable);
struct ListNode* l1 = find(423, hashTable);
if (NULL == l1) {
printf("not find!");
}
else {
printf("\nelment is:%d\n", l1->element);
}
struct ListNode* l2 = myDelete(423, hashTable);
if (NULL == l2) {
printf("not find!");
}
else {
printf("elment you delete is:%d\n", l2->element);
}
myPrint(hashTable);
return 0;
}
3、函数的实现
3.1 initializeTable.c
#include "哈希表的分离链接法实现.h"
int hash(const key, int tableSize);
//初始化哈希表
struct HashTable* initializeTable(int tableSize)
{
struct HashTable* h;
if (tableSize < MinTableSize) {
printf("Table size is too small!\n");
return NULL;
}
h = malloc(sizeof(struct HashTable));
if (NULL == h) {
printf("Out of space!\n");
return NULL;
}
h->tableSize = tableSize;
h->theLists = malloc(sizeof(struct listNode*) * h->tableSize);
if (NULL == h->theLists) {
printf("Out of space!\n");
}
for (int i = 0; i < h->tableSize; i++) {
h->theLists[i] = malloc(sizeof(struct ListNode));
if (NULL == h->theLists[i]) {
printf("Out of space!\n");
return NULL;
}
else {
h->theLists[i]->next = NULL;
}
}
return h;
}
int hash(const key, int tableSize)
{
return key % tableSize;
}
3.2 find.c
#include "哈希表的分离链接法实现.h"
extern int hash(const key, int tableSize);
struct ListNode* find(int key, struct HashTable* h)
{
struct ListNode* l = h->theLists[hash(key, h->tableSize)];
struct ListNode* p = l->next;
while (p != NULL && p->element != key) {
p = p->next;
}
return p;
}
3.3 insert.c
#include "哈希表的分离链接法实现.h"
void insert(int key, struct HashTable* h)
{
struct ListNode* pos = find(key, h);
if (NULL == pos) {
struct ListNode* newCell = malloc(sizeof(struct ListNode));
if (NULL == newCell) {
printf("Out of space!");
return;
}
else {
struct ListNode* l = h->theLists[hash(key, h->tableSize)];
newCell->next = l->next;
newCell->element = key;
l->next = newCell;
}
}
}
3.4 myDelete
#include "哈希表的分离链接法实现.h"
extern int hash(const key, int tableSize);
struct ListNode* myDelete(int key, struct HashTable* h)
{
struct ListNode* l1 = h->theLists[hash(key, h->tableSize)];
struct ListNode* l2 = l1->next;
while (key != l2->element) {
if (NULL == l2->next) {
printf("not find!");
return NULL;
}
l1 = l2;
l2 = l2->next;
}
l1->next = l2->next;
return l2;
}
3.5 myPrint.c
#include "哈希表的分离链接法实现.h"
void myPrint(struct HashTable* h)
{
struct ListNode* temp;
for (int i = 0; i < h->tableSize; i++) {
temp = h->theLists[i]->next;
while (NULL != temp) {
printf("%d ", temp->element);
temp = temp->next;
}
}
}
4、写在最后
为方便大家的学习,我已经将原码上传到了GitHub,欢迎大家下载。链接:link
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)