分离链接散列表

数据结构与算法分析——c语言描述 第五章 分离链接散列表


特点就是通过关键字来确定一个位置。用c涉及到字符串好麻烦。


hashsep.h

typedef char* ElementType;

#ifndef _HashSep_H

struct ListNode;
typedef struct ListNode* Position;
struct HashTbl;
typedef struct HashTbl* HashTable;

HashTable initializeTable(int tableSize);
void destroyTable(HashTable h);
Position find(ElementType key, HashTable h);
void insert(ElementType key, HashTable h);
void Delete(ElementType key, HashTable h);
ElementType retrive(Position p);

#endif



hashsep.c

#include"hashsep.h"
#include"fatal.h"
#include<math.h>
#include<string.h>
#define MinTableSize 10

struct ListNode {
	ElementType element;
	Position next;
};

typedef Position List;

struct HashTbl {
	int tableSize;
	List *theLists;//数组指针
};

static int hash(ElementType key, int tableSize) {
	unsigned int hashVal = 0;
	while (*key != '\0')
		hashVal = (hashVal << 5) + *key++;
	return hashVal % (tableSize);
}
static int isPrime(int num) {
	for (int i = 2; i <= sqrt(num); i++)
		if (num%i == 0)
			return 0;
	return 1;
}
static int nextPrime(int num) {
	int i = num;
	while (!isPrime(i))
		i++;
	return i;
}


HashTable initializeTable(int tableSize) {
	HashTable h;
	int i;
	if (tableSize < MinTableSize) {
		Error("Table size too small");
		return NULL;
	}
	h = malloc(sizeof(struct HashTbl));
	if (h == NULL)
		FatalError("Out of space!!!");
	h->tableSize = nextPrime(tableSize);
	h->theLists = malloc(sizeof(List)*h->tableSize);
	if (h->theLists == NULL)
		FatalError("Out of space!!!");
	for (i = 0; i < h->tableSize; i++) {
		h->theLists[i] = malloc(sizeof(struct ListNode));
		if (h->theLists == NULL)
			FatalError("Out of space!!!");
		else {
			h->theLists[i]->next = NULL;
		}
	}
	return h;
}

void destroyTable(HashTable h) {
	for (int i = 0; i < h->tableSize; i++) {
		List l = h->theLists[i];
		Position p = l;
		while (p) {
			Position temp;
			temp = p->next;
			free(p);
			p = temp;
		}
	}
	free(h->theLists);
	free(h);
}

Position find(ElementType key, HashTable h) {
	Position p;
	List l;

	l = h->theLists[hash(key, h->tableSize)];

	p = l->next;
	while (p&& strcmp(p->element, key) != 0)
		p = p->next;
	return p;
}

void insert(ElementType key, HashTable h) {
	Position pos, newCell;
	List l;

	pos = find(key, h);
	if (pos == NULL) {
		newCell = malloc(sizeof(struct ListNode));
		if (newCell == NULL)
			FatalError("Out of space!!!");
		newCell->element = malloc(sizeof(char)*strlen(key)+1);//+1是为了给\0留空间
		if (newCell->element == NULL)
			FatalError("Out of space!!!");

		l = h->theLists[hash(key, h->tableSize)];
		newCell->next = l->next;
		l->next = newCell;
		strcpy(newCell->element, key);

	}
}

void Delete(ElementType key, HashTable h) {
	Position p;
	List l;

	l = h->theLists[hash(key, h->tableSize)];
	p = l;
	while (p->next&& strcmp( p->next->element ,key)!=0)
		p = p->next;
	if (p->next) {
		Position deleteCell = p->next;
		p->next = deleteCell->next;
		free(deleteCell->element);
		free(deleteCell);
	}
}

ElementType retrive(Position p) {
	return p->element;
}


main.c

#include"hashsep.h"
#include<stdio.h>
int main() {
	HashTable h = initializeTable(500);
	insert("aaaaaaa", h);
	Position p = find("aaaaaaa", h);
	if (p)
		printf("%s", retrive(p));
	Delete("aaaaaaa", h);
	p = find("aaaaaaa", h);
	if (p)
		printf("%s", retrive(p));
	destroyTable(h);
}


剩下7周把这本书刷完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值