分离链接散列表

数据结构与算法分析——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周把这本书刷完。

你可能感兴趣的:(分离链接散列表)