哈希表实现

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#define MAX_N 2

typedef struct Node {
	char* s;
	struct Node* next;
} Node;

typedef struct HashTable { 
	Node* data;
	int cnt, size;
} HashTable;

void expend(HashTable* h);
void clearHashTable(HashTable* h);
Node* getNewNode(const char* s) {
	Node* p = (Node*)malloc(sizeof(Node));
	p->s = _strdup(s);
	p->next = NULL;
	return p; 
}

HashTable* getNewHashTable(int n) {
	HashTable* h = (HashTable*)malloc(sizeof(HashTable));
	h->data = (Node*)malloc(sizeof(Node) * n);
	for (int i = 0; i < n; i++) h->data[i].next = NULL;
	h->size = n;
	h->cnt = 0;
	return h;
}

int hash_func(const char* s) {
	int seed = 131, h = 0;
	for (int i = 0; s[i]; i++) {
		h = h * seed + s[i];
	}
	return h & 0x7fffffff;  
}

bool find(HashTable* h, const char* s) {
	int hcode = hash_func(s), ind = hcode % h->size;
	Node* p = h->data[ind].next;
	while (p) {
		if (strcmp(p->s, s) == 0) return true;
		p = p->next;
	}
	return false;
}

bool insert(HashTable* h, const char* s) {
	if (h->cnt >= h->size * 2) {
		expend(h);
	}
	int hcode = hash_func(s), ind = hcode % h->size;
	Node* p = getNewNode(s);
	p->next = h->data[ind].next;
	h->data[ind].next = p; 
	h->cnt += 1;
	return true;
}

void swapHashTable(HashTable* h1, HashTable* h2) {
	Node* p = h1->data;
	h1->data = h2->data, h2->data = p;
	int t = h1->cnt;
	h1->cnt = h2->cnt, h2->cnt = t;
	t = h1->size;
	h1->size = h2->size, h2->size = t;
	return;
}

void expend(HashTable* h) {
	HashTable* new_h = getNewHashTable(h->size * 2);
	for (int i = 0; i < h->size; i++) {
		Node* p = h->data[i].next;
		while (p) {
			insert(new_h, p->s);
			p = p->next;
		}
	}
	swapHashTable(h, new_h);
	clearHashTable(new_h);
	return;
}


void clearNode(Node* p) {
	if (p == NULL) return;
	if (p->s) free(p->s);
	free(p);
	return;
}

void clearHashTable(HashTable* h) {
	if (h == NULL) return;
	for (int i = 0; i < h->size; i++) {
		Node* p = h->data[i].next, * q;
		while (p) {
			q = p->next;
			clearNode(p);
			p = q;
		}
	}
	free(h->data);
	free(h);
	return;
}

void output(HashTable* h) {
	printf("\nHash Table(%d / %d) : \n", h->cnt, h->size);
	for (int i = 0; i < h->size; i++) {
		printf("%d : ", i);
		Node* p = h->data[i].next;
		while (p) {
			printf("%s -> ", p->s);
			p = p->next;
		}
		printf("\n");
	}
	printf("\n");
	return;
}

int main() {
	srand(time(0));

	char s[100];
	HashTable* h = getNewHashTable(MAX_N);
	while (~scanf("%s", s)) {
		if (strcmp(s, "end") == 0) break;
		insert(h, s);
		output(h);
	}
	output(h);
	while (~scanf("%s", s)) {
		printf("\nfind(%s) = %d\n\n", s, find(h, s));
	}

	return 0;
}

你可能感兴趣的:(数据结构与算法,哈希算法,数据结构)