-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash_map.c
188 lines (163 loc) · 4.61 KB
/
hash_map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 10
#define LOAD_FACTOR 0.75
// 哈希表的元素结构体
typedef struct Entry {
char *key;
int value;
struct Entry *next; // 用于链表解决冲突
} Entry;
// 哈希表结构体
typedef struct {
Entry **table; // 哈希表数组
int size; // 哈希表中存储的元素数量
int capacity; // 哈希表的容量
} HashMap;
// 哈希函数(使用简单的字符串哈希函数)
unsigned int hash(const char *key, int capacity) {
unsigned int hashValue = 0;
while (*key) {
hashValue = (hashValue * 31) + *key;
key++;
}
return hashValue % capacity;
}
// 初始化 HashMap
void initHashMap(HashMap *map) {
map->size = 0;
map->capacity = INITIAL_CAPACITY;
map->table = (Entry **)malloc(sizeof(Entry *) * map->capacity);
for (int i = 0; i < map->capacity; i++) {
map->table[i] = NULL;
}
}
// 重新分配容量,扩容 HashMap
void resizeMap(HashMap *map) {
int newCapacity = map->capacity * 2;
Entry **newTable = (Entry **)malloc(sizeof(Entry *) * newCapacity);
// 初始化新表
for (int i = 0; i < newCapacity; i++) {
newTable[i] = NULL;
}
// 迁移原表数据到新表
for (int i = 0; i < map->capacity; i++) {
Entry *entry = map->table[i];
while (entry) {
int newIndex = hash(entry->key, newCapacity);
Entry *newEntry = (Entry *)malloc(sizeof(Entry));
newEntry->key = strdup(entry->key);
newEntry->value = entry->value;
newEntry->next = newTable[newIndex];
newTable[newIndex] = newEntry;
entry = entry->next;
}
}
// 释放原表并更新哈希表
for (int i = 0; i < map->capacity; i++) {
Entry *entry = map->table[i];
while (entry) {
Entry *next = entry->next;
free(entry->key);
free(entry);
entry = next;
}
}
free(map->table);
map->table = newTable;
map->capacity = newCapacity;
}
// 插入键值对(如果存在则更新)
void put(HashMap *map, const char *key, int value) {
// 判断是否需要扩容
if ((float)map->size / map->capacity > LOAD_FACTOR) {
resizeMap(map);
}
int index = hash(key, map->capacity);
Entry *entry = map->table[index];
// 遍历链表,检查是否已经存在相同的键
while (entry) {
if (strcmp(entry->key, key) == 0) {
entry->value = value; // 更新值
return;
}
entry = entry->next;
}
// 如果没有找到相同的键,插入新键值对
Entry *newEntry = (Entry *)malloc(sizeof(Entry));
newEntry->key = strdup(key);
newEntry->value = value;
newEntry->next = map->table[index];
map->table[index] = newEntry;
map->size++;
}
// 查找键
int get(HashMap *map, const char *key) {
int index = hash(key, map->capacity);
Entry *entry = map->table[index];
while (entry) {
if (strcmp(entry->key, key) == 0) {
return entry->value;
}
entry = entry->next;
}
return -1; // 返回 -1 表示未找到
}
// 删除键
void delete(HashMap *map, const char *key) {
int index = hash(key, map->capacity);
Entry *entry = map->table[index];
Entry *prev = NULL;
while (entry) {
if (strcmp(entry->key, key) == 0) {
if (prev) {
prev->next = entry->next;
} else {
map->table[index] = entry->next;
}
free(entry->key);
free(entry);
map->size--;
return;
}
prev = entry;
entry = entry->next;
}
}
// 释放哈希表
void freeHashMap(HashMap *map) {
for (int i = 0; i < map->capacity; i++) {
Entry *entry = map->table[i];
while (entry) {
Entry *next = entry->next;
free(entry->key);
free(entry);
entry = next;
}
}
free(map->table);
}
// 测试
int main() {
HashMap map;
initHashMap(&map);
put(&map, "apple", 10);
put(&map, "banana", 20);
put(&map, "orange", 30);
printf("apple: %d\n", get(&map, "apple"));
printf("banana: %d\n", get(&map, "banana"));
printf("grape: %d (not found)\n", get(&map, "grape"));
delete(&map, "banana");
printf("banana after delete: %d\n", get(&map, "banana"));
freeHashMap(&map);
return 0;
}
/*
jarry@MacBook-Pro hash % gcc hash_map.c
jarry@MacBook-Pro hash % ./a.out
apple: 10
banana: 20
grape: -1 (not found)
banana after delete: -1
*/