ADD file via upload
This commit is contained in:
parent
1e3ea0f713
commit
19355985cb
|
@ -0,0 +1,170 @@
|
|||
#include <transform.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include"test_hashmap.h"
|
||||
|
||||
int hashCode(void* key) {
|
||||
// 假设键是整数类型
|
||||
return *(int*)key;
|
||||
}
|
||||
|
||||
int equal(void* key1, void* key2) {
|
||||
// 假设键是整数类型
|
||||
return *(int*)key1 == *(int*)key2;
|
||||
}
|
||||
|
||||
HashMap createHashMap(int listSize, int autoAssign) {
|
||||
HashMap map = (HashMap)malloc(sizeof(struct hashMap));
|
||||
map->size = 0;
|
||||
map->listSize = listSize;
|
||||
map->hashCode = &hashCode;
|
||||
map->equal = &equal;
|
||||
map->list = (Entry)malloc(sizeof(struct entry) * listSize);
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
map->list[i].key = NULL;
|
||||
map->list[i].value = NULL;
|
||||
map->list[i].next = NULL;
|
||||
}
|
||||
map->autoAssign = autoAssign;
|
||||
return map;
|
||||
}
|
||||
|
||||
void defaultPut(HashMap map, void* key, void* value) {
|
||||
int index = map->hashCode(key) % map->listSize;
|
||||
Entry entry = &map->list[index];
|
||||
|
||||
while (entry->next != NULL) {
|
||||
if (map->equal(entry->next->key, key)) {
|
||||
entry->next->value = value;
|
||||
return;
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
Entry newEntry = (Entry)malloc(sizeof(struct entry));
|
||||
newEntry->key = key;
|
||||
newEntry->value = value;
|
||||
newEntry->next = NULL;
|
||||
entry->next = newEntry;
|
||||
map->size++;
|
||||
|
||||
if (map->autoAssign && map->size > map->listSize) {
|
||||
int newSize = map->listSize * 2;
|
||||
Entry newList = (Entry)realloc(map->list, sizeof(struct entry) * newSize);
|
||||
if (newList != NULL) {
|
||||
map->listSize = newSize;
|
||||
map->list = newList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void* defaultGet(HashMap map, void* key) {
|
||||
int index = map->hashCode(key) % map->listSize;
|
||||
Entry entry = &map->list[index];
|
||||
|
||||
while (entry->next != NULL) {
|
||||
if (map->equal(entry->next->key, key)) {
|
||||
return entry->next->value;
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void defaultRemove(HashMap map, void* key) {
|
||||
int index = map->hashCode(key) % map->listSize;
|
||||
Entry entry = &map->list[index];
|
||||
|
||||
while (entry->next != NULL) {
|
||||
if (map->equal(entry->next->key, key)) {
|
||||
Entry temp = entry->next;
|
||||
entry->next = entry->next->next;
|
||||
free(temp);
|
||||
map->size--;
|
||||
return;
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
}
|
||||
|
||||
void defaultClear(HashMap map) {
|
||||
for (int i = 0; i < map->listSize; i++) {
|
||||
Entry entry = &map->list[i];
|
||||
while (entry->next != NULL) {
|
||||
Entry temp = entry->next;
|
||||
entry->next = entry->next->next;
|
||||
free(temp);
|
||||
}
|
||||
entry->key = NULL;
|
||||
entry->value = NULL;
|
||||
}
|
||||
map->size = 0;
|
||||
}
|
||||
|
||||
int defaultExists(HashMap map, void* key) {
|
||||
int index = map->hashCode(key) % map->listSize;
|
||||
Entry entry = &map->list[index];
|
||||
|
||||
while (entry->next != NULL) {
|
||||
if (map->equal(entry->next->key, key)) {
|
||||
return 1;
|
||||
}
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Hash_Map() {
|
||||
// 创建一个哈希表
|
||||
HashMap map = createHashMap(10, 1);
|
||||
|
||||
// 添加键值对
|
||||
int key1 = 1;
|
||||
int value1 = 100;
|
||||
defaultPut(map, &key1, &value1);
|
||||
|
||||
int key2 = 2;
|
||||
int value2 = 200;
|
||||
defaultPut(map, &key2, &value2);
|
||||
|
||||
// 检查键是否存在
|
||||
int exists1 = defaultExists(map, &key1);
|
||||
if (exists1) {
|
||||
printf("Key %d exists in the map\n", key1);
|
||||
} else {
|
||||
printf("Key %d does not exist in the map\n", key1);
|
||||
}
|
||||
|
||||
// 获取键对应的值
|
||||
void* result1 = defaultGet(map, &key1);
|
||||
if (result1 != NULL) {
|
||||
printf("Key: %d, Value: %d\n", key1, *(int*)result1);
|
||||
}
|
||||
|
||||
void* result2 = defaultGet(map, &key2);
|
||||
if (result2 != NULL) {
|
||||
printf("Key: %d, Value: %d\n", key2, *(int*)result2);
|
||||
}
|
||||
|
||||
// 移除键值对
|
||||
defaultRemove(map, &key1);
|
||||
|
||||
// 检查键是否存在
|
||||
exists1 = defaultExists(map, &key1);
|
||||
if (exists1) {
|
||||
printf("Key %d exists in the map\n", key1);
|
||||
} else {
|
||||
printf("Key %d does not exist in the map\n", key1);
|
||||
}
|
||||
|
||||
// 清空哈希表
|
||||
defaultClear(map);
|
||||
|
||||
// 释放哈希表内存
|
||||
free(map->list);
|
||||
free(map);
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(Hash_Map, Implement hash_map, PRIV_SHELL_CMD_MAIN_ATTR);
|
Loading…
Reference in New Issue