- UID
- 5181
- 精华
- 积分
- 453
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
本帖最后由 天马座 于 2021-7-11 02:05 编辑
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //哈希桶数量
- #define HASHSIZE (1 << 16)
- typedef int Value;
- struct list {
- //单链表
- char *key;
- Value value;
- struct list *next;
- }
- ;
- //哈希表
- struct list *hashList[HASHSIZE];
- Value valuedup(Value value){
- //兼容字符串代码
- return value;
- }
- struct list *newNode(char* key,Value value) {
- //创建一个新节点
- struct list *p = (struct list *)malloc(sizeof(struct list));
- if (p == NULL)
- return NULL;
- p->key = strdup(key);
- if (p->key == NULL){
- free(p);
- return NULL;
- }
- p->value = valuedup(value);
- //if (p->value == NULL){
- // free(p->key);
- // free(p);
- // return NULL;
- //}
- return p;
- }
- void freeNode(struct list *p){
- //释放节点
- free(p->key);
- //free(p->value);
- free(p);
- }
- unsigned int hashCode(char* s) {
- //计算哈希值
- unsigned int h = 0;
- while(*s != '\0')
- h = *s++ + 31 * h;
- return h;
- }
- int indexOf(char *key) {
- //计算key所在的下标
- return hashCode(key) & (HASHSIZE - 1) ;
- }
- struct list *listFind(struct list *head, char *key) {
- //链表查找键
- for (struct list *p = head; p != NULL; p = p->next)
- if (strcmp(p->key, key)==0)
- return p;
- return NULL;
- }
- struct list *find(char *key) {
- //哈希表查找键
- return listFind(hashList[indexOf(key)],key);
- }
- int contains(char *key) {
- //key是否存在
- return find(key) == NULL ? 0 : 1;
- }
- void add(char *key, Value value) {
- //添加键值对
- int i = indexOf(key);
- struct list *p = listFind(hashList[i],key);
- if (p == NULL) {
- //插入表头
- struct list *p = newNode(key,value);
- if (p == NULL)
- return;
- p->next = hashList[i];
- hashList[i] = p;
- } else{
- Value v = valuedup(value);
- //if (v == NULL)
- // return ;
- // free(p->value);
- p->value = v;
- }
- }
- void removeKey(char *key) {
- //删除一个key
- struct list *del = NULL;
- int i = indexOf(key);
- struct list *p = hashList[i];
- if (p == NULL) return;
- if (strcmp(p->key, key)==0) {
- del = p;
- hashList[i] = p->next;
- } else {
- for (; p->next != NULL; p = p->next)
- if (strcmp(p->next->key, key)==0) {
- del = p->next;
- p->next = p->next->next;
- break;
- }
- }
- if (del == NULL) return;;
- freeNode(del);
- }
- void release() {
- //销毁哈希表
- for (int i = 0; i < HASHSIZE; i++)
- while(hashList[i] != NULL) {
- struct list *p = hashList[i];
- hashList[i] = p->next;
- freeNode(p);
- }
-
- }
- void traversal() {
- //遍历哈希表
- for (int i = 0; i < HASHSIZE; i++)
- for (struct list *p = hashList[i]; p != NULL;p = p->next)
- printf("key = %s value = %d \n",p->key ,p->value);
-
-
- }
- void main() {
- add("a",1);
- add("b",2);
- add("c",3);
- traversal();
- removeKey("c");
- traversal();
- release();
- }
复制代码 |
|