哈希表的分离链表法实现

哈希表的分离链表法实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值