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