可以使用哈希表来存储色盲基因库数据,通过哈希函数把 4 位色盲基因映射到哈希表中,大大提高检索的效率
#include<Windows.h>
#include<iostream>
#include<stdio.h>
using namespace std;
#define isEqual(a, b) (0==strcmp((const char*)a,(const char*)b))
#define DEFAULT_SIZE 128
typedef struct _listNode {
struct _listNode* next;
void* key;
void* data;
}listNode;
typedef listNode* list;
typedef listNode* element;
typedef struct _hashTable {
int tableSize;
list* theLists;
}hashTable;
hashTable* initHash(int tableSize);
int hashFunction(void* key, int tableSize);//哈希函数
element findElement(hashTable* &hashTable, void* key);
bool insertElement(hashTable* &hashTable, void* key, void* value);
bool deleteElement(hashTable* &hastTable, void* key);
unsigned int SDBMHash(void* key);
void destroyHash(hashTable* &hashTable);
int main() {
const char* str[] = { "ABCD","BDCD","ACDB","BACD" };
char tester[] = "ABDCDC";
char tmp[5] = {0};
strncpy_s(tmp,sizeof(tmp), tester + 1, 4);
hashTable* hashTab = initHash(30);
for (int i = 0; i < 4; i++) {
if (insertElement(hashTab, (char*)str[i] , (char*)str[i])) {
cout << "插入成功!" << endl;
}
else {
cout << "插入失败!" << endl;
}
}
if (!deleteElement(hashTab, (char*)str[3]))
cout << "删除失败!" << endl;
element E = findElement(hashTab, tmp);
if (E) {
cout << "找到的元素为:" << (char*)(E->data) << endl;
}
else
cout << "未找到key对应的元素" << endl;
destroyHash(hashTab);
system("pause");
return 0;
}
hashTable* initHash(int tableSize) {
int i = 0;
hashTable* hTable = NULL;
if (tableSize <= 0) {
tableSize = DEFAULT_SIZE;
}
hTable = (hashTable*)malloc(sizeof(hashTable));
if (hTable == NULL) {
cout << "hashTable malloc failure" << endl;
return NULL;
}
hTable->tableSize = tableSize;
//为哈希桶分配内存空间,其为一个指针数组
hTable->theLists = (list*)malloc(tableSize * sizeof(list));
if (hTable->theLists == NULL) {
cout << "theLists malloc failure" << endl;
free(hTable);
return NULL;
}
//为哈希桶对应的指针数组初始化链表结点
for (int i = 0; i < tableSize; i++) {
hTable->theLists[i] = (listNode*)malloc(sizeof(listNode));
if (hTable->theLists[i] == NULL) {
cout << "theLists malloc failure" << endl;
free(hTable->theLists);
free(hTable);
return NULL;
}
memset(hTable->theLists[i], 0, sizeof(listNode));
}
return hTable;
}
int hashFunction(void* key, int tableSize) {
/*根据 key 计算索引,定位 Hash 桶的位置*/
return (SDBMHash(key) % tableSize);
}
element findElement(hashTable* &hashTable, void* key) {
int i = 0;
list L = NULL;
element E = NULL;
i = hashFunction(key, hashTable->tableSize);
L = hashTable->theLists[i];
E = L->next;
while (E != NULL && !isEqual(E->key,key)) {
E = E->next;
}
return E;
}
bool insertElement(hashTable* &hashTable, void* key, void* value) {
element E = NULL, tmp = NULL;
list L = NULL;
E = findElement(hashTable, key);
if (E == NULL) {
tmp = (listNode*)malloc(sizeof(listNode));
if (tmp == NULL) {
printf("listNode malloc failure!");
return false;
}
tmp->data = value;
tmp->key = key;
tmp->next = NULL;
int index = hashFunction(key,hashTable->tableSize);
L = hashTable->theLists[index];
tmp->next = L->next;
L->next = tmp;
return true;
}
else {
cout << "element already exits" << endl;
return false;
}
}
bool deleteElement(hashTable* &hashTable, void* key) {
element E = NULL;
element L = NULL;
int index = hashFunction(key, hashTable->tableSize);
L = hashTable->theLists[index];
E = L->next;
while ( E != NULL && !isEqual(E->key, key) ) {
L = E;
E = E->next;
}
if (E) {
L->next = E->next;
delete E;
return true;
}
return false;
}
unsigned int SDBMHash(void* key) {
unsigned int hash = 0;
char* str = (char*)key;
while (*str) {
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
void destroyHash(hashTable* &hashTable) {
element E = NULL;
element tmp = NULL;
list L = NULL;
for (int i = 0; i < hashTable->tableSize; i++) {
L = hashTable->theLists[i];
E = L->next;
while (E) {
tmp = E;
E = E->next;
delete tmp;
}
delete L;
}
delete hashTable->theLists;
delete hashTable;
}
来源:CSDN
作者:lee李家军
链接:https://blog.csdn.net/weixin_40071289/article/details/104149618