mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:56:31 +08:00
5f7385c8a3
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
213 lines
5.7 KiB
C
213 lines
5.7 KiB
C
/**
|
|
* File: hash_map_chaining.c
|
|
* Created Time: 2023-10-13
|
|
* Author: SenMing (1206575349@qq.com), krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// 假設 val 最大長度為 100
|
|
#define MAX_SIZE 100
|
|
|
|
/* 鍵值對 */
|
|
typedef struct {
|
|
int key;
|
|
char val[MAX_SIZE];
|
|
} Pair;
|
|
|
|
/* 鏈結串列節點 */
|
|
typedef struct Node {
|
|
Pair *pair;
|
|
struct Node *next;
|
|
} Node;
|
|
|
|
/* 鏈式位址雜湊表 */
|
|
typedef struct {
|
|
int size; // 鍵值對數量
|
|
int capacity; // 雜湊表容量
|
|
double loadThres; // 觸發擴容的負載因子閾值
|
|
int extendRatio; // 擴容倍數
|
|
Node **buckets; // 桶陣列
|
|
} HashMapChaining;
|
|
|
|
/* 建構子 */
|
|
HashMapChaining *newHashMapChaining() {
|
|
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
|
|
hashMap->size = 0;
|
|
hashMap->capacity = 4;
|
|
hashMap->loadThres = 2.0 / 3.0;
|
|
hashMap->extendRatio = 2;
|
|
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
hashMap->buckets[i] = NULL;
|
|
}
|
|
return hashMap;
|
|
}
|
|
|
|
/* 析構函式 */
|
|
void delHashMapChaining(HashMapChaining *hashMap) {
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
Node *cur = hashMap->buckets[i];
|
|
while (cur) {
|
|
Node *tmp = cur;
|
|
cur = cur->next;
|
|
free(tmp->pair);
|
|
free(tmp);
|
|
}
|
|
}
|
|
free(hashMap->buckets);
|
|
free(hashMap);
|
|
}
|
|
|
|
/* 雜湊函式 */
|
|
int hashFunc(HashMapChaining *hashMap, int key) {
|
|
return key % hashMap->capacity;
|
|
}
|
|
|
|
/* 負載因子 */
|
|
double loadFactor(HashMapChaining *hashMap) {
|
|
return (double)hashMap->size / (double)hashMap->capacity;
|
|
}
|
|
|
|
/* 查詢操作 */
|
|
char *get(HashMapChaining *hashMap, int key) {
|
|
int index = hashFunc(hashMap, key);
|
|
// 走訪桶,若找到 key ,則返回對應 val
|
|
Node *cur = hashMap->buckets[index];
|
|
while (cur) {
|
|
if (cur->pair->key == key) {
|
|
return cur->pair->val;
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
return ""; // 若未找到 key ,則返回空字串
|
|
}
|
|
|
|
/* 新增操作 */
|
|
void put(HashMapChaining *hashMap, int key, const char *val);
|
|
|
|
/* 擴容雜湊表 */
|
|
void extend(HashMapChaining *hashMap) {
|
|
// 暫存原雜湊表
|
|
int oldCapacity = hashMap->capacity;
|
|
Node **oldBuckets = hashMap->buckets;
|
|
// 初始化擴容後的新雜湊表
|
|
hashMap->capacity *= hashMap->extendRatio;
|
|
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
hashMap->buckets[i] = NULL;
|
|
}
|
|
hashMap->size = 0;
|
|
// 將鍵值對從原雜湊表搬運至新雜湊表
|
|
for (int i = 0; i < oldCapacity; i++) {
|
|
Node *cur = oldBuckets[i];
|
|
while (cur) {
|
|
put(hashMap, cur->pair->key, cur->pair->val);
|
|
Node *temp = cur;
|
|
cur = cur->next;
|
|
// 釋放記憶體
|
|
free(temp->pair);
|
|
free(temp);
|
|
}
|
|
}
|
|
|
|
free(oldBuckets);
|
|
}
|
|
|
|
/* 新增操作 */
|
|
void put(HashMapChaining *hashMap, int key, const char *val) {
|
|
// 當負載因子超過閾值時,執行擴容
|
|
if (loadFactor(hashMap) > hashMap->loadThres) {
|
|
extend(hashMap);
|
|
}
|
|
int index = hashFunc(hashMap, key);
|
|
// 走訪桶,若遇到指定 key ,則更新對應 val 並返回
|
|
Node *cur = hashMap->buckets[index];
|
|
while (cur) {
|
|
if (cur->pair->key == key) {
|
|
strcpy(cur->pair->val, val); // 若遇到指定 key ,則更新對應 val 並返回
|
|
return;
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
// 若無該 key ,則將鍵值對新增至鏈結串列頭部
|
|
Pair *newPair = (Pair *)malloc(sizeof(Pair));
|
|
newPair->key = key;
|
|
strcpy(newPair->val, val);
|
|
Node *newNode = (Node *)malloc(sizeof(Node));
|
|
newNode->pair = newPair;
|
|
newNode->next = hashMap->buckets[index];
|
|
hashMap->buckets[index] = newNode;
|
|
hashMap->size++;
|
|
}
|
|
|
|
/* 刪除操作 */
|
|
void removeItem(HashMapChaining *hashMap, int key) {
|
|
int index = hashFunc(hashMap, key);
|
|
Node *cur = hashMap->buckets[index];
|
|
Node *pre = NULL;
|
|
while (cur) {
|
|
if (cur->pair->key == key) {
|
|
// 從中刪除鍵值對
|
|
if (pre) {
|
|
pre->next = cur->next;
|
|
} else {
|
|
hashMap->buckets[index] = cur->next;
|
|
}
|
|
// 釋放記憶體
|
|
free(cur->pair);
|
|
free(cur);
|
|
hashMap->size--;
|
|
return;
|
|
}
|
|
pre = cur;
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
|
|
/* 列印雜湊表 */
|
|
void print(HashMapChaining *hashMap) {
|
|
for (int i = 0; i < hashMap->capacity; i++) {
|
|
Node *cur = hashMap->buckets[i];
|
|
printf("[");
|
|
while (cur) {
|
|
printf("%d -> %s, ", cur->pair->key, cur->pair->val);
|
|
cur = cur->next;
|
|
}
|
|
printf("]\n");
|
|
}
|
|
}
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
/* 初始化雜湊表 */
|
|
HashMapChaining *hashMap = newHashMapChaining();
|
|
|
|
/* 新增操作 */
|
|
// 在雜湊表中新增鍵值對 (key, value)
|
|
put(hashMap, 12836, "小哈");
|
|
put(hashMap, 15937, "小囉");
|
|
put(hashMap, 16750, "小算");
|
|
put(hashMap, 13276, "小法");
|
|
put(hashMap, 10583, "小鴨");
|
|
printf("\n新增完成後,雜湊表為\nKey -> Value\n");
|
|
print(hashMap);
|
|
|
|
/* 查詢操作 */
|
|
// 向雜湊表中輸入鍵 key ,得到值 value
|
|
char *name = get(hashMap, 13276);
|
|
printf("\n輸入學號 13276 ,查詢到姓名 %s\n", name);
|
|
|
|
/* 刪除操作 */
|
|
// 在雜湊表中刪除鍵值對 (key, value)
|
|
removeItem(hashMap, 12836);
|
|
printf("\n刪除學號 12836 後,雜湊表為\nKey -> Value\n");
|
|
print(hashMap);
|
|
|
|
/* 釋放雜湊表空間 */
|
|
delHashMapChaining(hashMap);
|
|
|
|
return 0;
|
|
}
|