資料結構與算法分析——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周把這本書刷完。