mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-04 23:00:54 +08:00
Simplify struct declarations of C.
Use PascalCase for all structs in C. SImplify n_queens.c Format C code for chapter of graph.
This commit is contained in:
@@ -6,35 +6,29 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 哈希表默认数组大小 */
|
||||
/* 哈希表默认大小 */
|
||||
#define HASH_MAP_DEFAULT_SIZE 100
|
||||
|
||||
/* 键值对 int->string */
|
||||
struct pair {
|
||||
typedef struct {
|
||||
int key;
|
||||
char *val;
|
||||
};
|
||||
} Pair;
|
||||
|
||||
typedef struct pair pair;
|
||||
|
||||
/* 用于表示键值对、键、值的集合 */
|
||||
struct mapSet {
|
||||
/* 键值对的集合 */
|
||||
typedef struct {
|
||||
void *set;
|
||||
int len;
|
||||
};
|
||||
|
||||
typedef struct mapSet mapSet;
|
||||
} MapSet;
|
||||
|
||||
/* 基于数组简易实现的哈希表 */
|
||||
struct arrayHashMap {
|
||||
pair *buckets[HASH_MAP_DEFAULT_SIZE];
|
||||
};
|
||||
|
||||
typedef struct arrayHashMap arrayHashMap;
|
||||
typedef struct {
|
||||
Pair *buckets[HASH_MAP_DEFAULT_SIZE];
|
||||
} ArrayHashMap;
|
||||
|
||||
/* 哈希表初始化函数 */
|
||||
arrayHashMap *newArrayHashMap() {
|
||||
arrayHashMap *map = malloc(sizeof(arrayHashMap));
|
||||
ArrayHashMap *newArrayHashMap() {
|
||||
ArrayHashMap *map = malloc(sizeof(ArrayHashMap));
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -45,27 +39,27 @@ int hashFunc(int key) {
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
const char *get(const arrayHashMap *d, const int key) {
|
||||
const char *get(const ArrayHashMap *d, const int key) {
|
||||
int index = hashFunc(key);
|
||||
const pair *pair = d->buckets[index];
|
||||
if (pair == NULL)
|
||||
const Pair *Pair = d->buckets[index];
|
||||
if (Pair == NULL)
|
||||
return NULL;
|
||||
return pair->val;
|
||||
return Pair->val;
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
void put(arrayHashMap *d, const int key, const char *val) {
|
||||
pair *pair = malloc(sizeof(pair));
|
||||
pair->key = key;
|
||||
pair->val = malloc(strlen(val) + 1);
|
||||
strcpy(pair->val, val);
|
||||
void put(ArrayHashMap *d, const int key, const char *val) {
|
||||
Pair *Pair = malloc(sizeof(Pair));
|
||||
Pair->key = key;
|
||||
Pair->val = malloc(strlen(val) + 1);
|
||||
strcpy(Pair->val, val);
|
||||
|
||||
int index = hashFunc(key);
|
||||
d->buckets[index] = pair;
|
||||
d->buckets[index] = Pair;
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
void removeItem(arrayHashMap *d, const int key) {
|
||||
void removeItem(ArrayHashMap *d, const int key) {
|
||||
int index = hashFunc(key);
|
||||
free(d->buckets[index]->val);
|
||||
free(d->buckets[index]);
|
||||
@@ -73,8 +67,8 @@ void removeItem(arrayHashMap *d, const int key) {
|
||||
}
|
||||
|
||||
/* 获取所有键值对 */
|
||||
void pairSet(arrayHashMap *d, mapSet *set) {
|
||||
pair *entries;
|
||||
void pairSet(ArrayHashMap *d, MapSet *set) {
|
||||
Pair *entries;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
|
||||
@@ -85,7 +79,7 @@ void pairSet(arrayHashMap *d, mapSet *set) {
|
||||
}
|
||||
}
|
||||
|
||||
entries = malloc(sizeof(pair) * total);
|
||||
entries = malloc(sizeof(Pair) * total);
|
||||
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
||||
if (d->buckets[i] != NULL) {
|
||||
entries[index].key = d->buckets[i]->key;
|
||||
@@ -100,7 +94,7 @@ void pairSet(arrayHashMap *d, mapSet *set) {
|
||||
}
|
||||
|
||||
/* 获取所有键 */
|
||||
void keySet(arrayHashMap *d, mapSet *set) {
|
||||
void keySet(ArrayHashMap *d, MapSet *set) {
|
||||
int *keys;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
@@ -125,7 +119,7 @@ void keySet(arrayHashMap *d, mapSet *set) {
|
||||
}
|
||||
|
||||
/* 获取所有值 */
|
||||
void valueSet(arrayHashMap *d, mapSet *set) {
|
||||
void valueSet(ArrayHashMap *d, MapSet *set) {
|
||||
char **vals;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
@@ -150,11 +144,11 @@ void valueSet(arrayHashMap *d, mapSet *set) {
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
void print(arrayHashMap *d) {
|
||||
void print(ArrayHashMap *d) {
|
||||
int i;
|
||||
mapSet set;
|
||||
MapSet set;
|
||||
pairSet(d, &set);
|
||||
pair *entries = (pair *)set.set;
|
||||
Pair *entries = (Pair *)set.set;
|
||||
for (i = 0; i < set.len; i++) {
|
||||
printf("%d -> %s\n", entries[i].key, entries[i].val);
|
||||
}
|
||||
@@ -164,7 +158,7 @@ void print(arrayHashMap *d) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化哈希表 */
|
||||
arrayHashMap *map = newArrayHashMap();
|
||||
ArrayHashMap *map = newArrayHashMap();
|
||||
|
||||
/* 添加操作 */
|
||||
// 在哈希表中添加键值对 (key, value)
|
||||
@@ -193,7 +187,7 @@ int main() {
|
||||
printf("\n遍历键值对 Key->Value\n");
|
||||
print(map);
|
||||
|
||||
mapSet set;
|
||||
MapSet set;
|
||||
|
||||
keySet(map, &set);
|
||||
int *keys = (int *)set.set;
|
||||
|
||||
@@ -8,36 +8,33 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 键值对 */
|
||||
struct pair {
|
||||
int key;
|
||||
char val[100]; // 假设 val 最大长度为 100
|
||||
};
|
||||
// 假设 val 最大长度为 100
|
||||
#define MAX_SIZE 100
|
||||
|
||||
typedef struct pair Pair;
|
||||
/* 键值对 */
|
||||
typedef struct {
|
||||
int key;
|
||||
char val[MAX_SIZE];
|
||||
} Pair;
|
||||
|
||||
/* 链表节点 */
|
||||
struct node {
|
||||
typedef struct Node {
|
||||
Pair *pair;
|
||||
struct node *next;
|
||||
};
|
||||
|
||||
typedef struct node Node;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
/* 链式地址哈希表 */
|
||||
struct hashMapChaining {
|
||||
typedef struct {
|
||||
int size; // 键值对数量
|
||||
int capacity; // 哈希表容量
|
||||
double loadThres; // 触发扩容的负载因子阈值
|
||||
int extendRatio; // 扩容倍数
|
||||
Node **buckets; // 桶数组
|
||||
};
|
||||
|
||||
typedef struct hashMapChaining hashMapChaining;
|
||||
} HashMapChaining;
|
||||
|
||||
/* 构造方法 */
|
||||
hashMapChaining *initHashMapChaining() {
|
||||
hashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
|
||||
HashMapChaining *initHashMapChaining() {
|
||||
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
|
||||
hashMap->size = 0;
|
||||
hashMap->capacity = 4;
|
||||
hashMap->loadThres = 2.0 / 3.0;
|
||||
@@ -50,7 +47,7 @@ hashMapChaining *initHashMapChaining() {
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
void freeHashMapChaining(hashMapChaining *hashMap) {
|
||||
void freeHashMapChaining(HashMapChaining *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Node *cur = hashMap->buckets[i];
|
||||
while (cur) {
|
||||
@@ -65,17 +62,17 @@ void freeHashMapChaining(hashMapChaining *hashMap) {
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
int hashFunc(hashMapChaining *hashMap, int key) {
|
||||
int hashFunc(HashMapChaining *hashMap, int key) {
|
||||
return key % hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 负载因子 */
|
||||
double loadFactor(hashMapChaining *hashMap) {
|
||||
double loadFactor(HashMapChaining *hashMap) {
|
||||
return (double)hashMap->size / (double)hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
char *get(hashMapChaining *hashMap, int key) {
|
||||
char *get(HashMapChaining *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
// 遍历桶,若找到 key 则返回对应 val
|
||||
Node *cur = hashMap->buckets[index];
|
||||
@@ -89,10 +86,10 @@ char *get(hashMapChaining *hashMap, int key) {
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
void put(hashMapChaining *hashMap, int key, const char *val);
|
||||
void put(HashMapChaining *hashMap, int key, const char *val);
|
||||
|
||||
/* 扩容哈希表 */
|
||||
void extend(hashMapChaining *hashMap) {
|
||||
void extend(HashMapChaining *hashMap) {
|
||||
// 暂存原哈希表
|
||||
int oldCapacity = hashMap->capacity;
|
||||
Node **oldBuckets = hashMap->buckets;
|
||||
@@ -120,7 +117,7 @@ void extend(hashMapChaining *hashMap) {
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
void put(hashMapChaining *hashMap, int key, const char *val) {
|
||||
void put(HashMapChaining *hashMap, int key, const char *val) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor(hashMap) > hashMap->loadThres) {
|
||||
extend(hashMap);
|
||||
@@ -147,7 +144,7 @@ void put(hashMapChaining *hashMap, int key, const char *val) {
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
void removeKey(hashMapChaining *hashMap, int key) {
|
||||
void removeKey(HashMapChaining *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
Node *cur = hashMap->buckets[index];
|
||||
Node *pre = NULL;
|
||||
@@ -171,7 +168,7 @@ void removeKey(hashMapChaining *hashMap, int key) {
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
void print(hashMapChaining *hashMap) {
|
||||
void print(HashMapChaining *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Node *cur = hashMap->buckets[i];
|
||||
printf("[");
|
||||
@@ -186,7 +183,7 @@ void print(hashMapChaining *hashMap) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化哈希表 */
|
||||
hashMapChaining *hashMap = initHashMapChaining();
|
||||
HashMapChaining *hashMap = initHashMapChaining();
|
||||
|
||||
/* 添加操作 */
|
||||
// 在哈希表中添加键值对 (key, value)
|
||||
|
||||
@@ -7,31 +7,27 @@
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 开放寻址哈希表 */
|
||||
struct pair {
|
||||
typedef struct {
|
||||
int key;
|
||||
char *val;
|
||||
};
|
||||
|
||||
typedef struct pair Pair;
|
||||
} Pair;
|
||||
|
||||
/* 开放寻址哈希表 */
|
||||
struct hashMapOpenAddressing {
|
||||
typedef struct {
|
||||
int size; // 键值对数量
|
||||
int capacity; // 哈希表容量
|
||||
double loadThres; // 触发扩容的负载因子阈值
|
||||
int extendRatio; // 扩容倍数
|
||||
Pair **buckets; // 桶数组
|
||||
Pair *TOMBSTONE; // 删除标记
|
||||
};
|
||||
|
||||
typedef struct hashMapOpenAddressing hashMapOpenAddressing;
|
||||
} HashMapOpenAddressing;
|
||||
|
||||
// 函数声明
|
||||
void extend(hashMapOpenAddressing *hashMap);
|
||||
void extend(HashMapOpenAddressing *hashMap);
|
||||
|
||||
/* 构造方法 */
|
||||
hashMapOpenAddressing *newHashMapOpenAddressing() {
|
||||
hashMapOpenAddressing *hashMap = (hashMapOpenAddressing *)malloc(sizeof(hashMapOpenAddressing));
|
||||
HashMapOpenAddressing *newHashMapOpenAddressing() {
|
||||
HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));
|
||||
hashMap->size = 0;
|
||||
hashMap->capacity = 4;
|
||||
hashMap->loadThres = 2.0 / 3.0;
|
||||
@@ -45,7 +41,7 @@ hashMapOpenAddressing *newHashMapOpenAddressing() {
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
void delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {
|
||||
void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Pair *pair = hashMap->buckets[i];
|
||||
if (pair != NULL && pair != hashMap->TOMBSTONE) {
|
||||
@@ -56,17 +52,17 @@ void delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
int hashFunc(hashMapOpenAddressing *hashMap, int key) {
|
||||
int hashFunc(HashMapOpenAddressing *hashMap, int key) {
|
||||
return key % hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 负载因子 */
|
||||
double loadFactor(hashMapOpenAddressing *hashMap) {
|
||||
double loadFactor(HashMapOpenAddressing *hashMap) {
|
||||
return (double)hashMap->size / (double)hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 搜索 key 对应的桶索引 */
|
||||
int findBucket(hashMapOpenAddressing *hashMap, int key) {
|
||||
int findBucket(HashMapOpenAddressing *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
int firstTombstone = -1;
|
||||
// 线性探测,当遇到空桶时跳出
|
||||
@@ -93,7 +89,7 @@ int findBucket(hashMapOpenAddressing *hashMap, int key) {
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
char *get(hashMapOpenAddressing *hashMap, int key) {
|
||||
char *get(HashMapOpenAddressing *hashMap, int key) {
|
||||
// 搜索 key 对应的桶索引
|
||||
int index = findBucket(hashMap, key);
|
||||
// 若找到键值对,则返回对应 val
|
||||
@@ -105,7 +101,7 @@ char *get(hashMapOpenAddressing *hashMap, int key) {
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
void put(hashMapOpenAddressing *hashMap, int key, char *val) {
|
||||
void put(HashMapOpenAddressing *hashMap, int key, char *val) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor(hashMap) > hashMap->loadThres) {
|
||||
extend(hashMap);
|
||||
@@ -132,7 +128,7 @@ void put(hashMapOpenAddressing *hashMap, int key, char *val) {
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
void removeItem(hashMapOpenAddressing *hashMap, int key) {
|
||||
void removeItem(HashMapOpenAddressing *hashMap, int key) {
|
||||
// 搜索 key 对应的桶索引
|
||||
int index = findBucket(hashMap, key);
|
||||
// 若找到键值对,则用删除标记覆盖它
|
||||
@@ -146,7 +142,7 @@ void removeItem(hashMapOpenAddressing *hashMap, int key) {
|
||||
}
|
||||
|
||||
/* 扩容哈希表 */
|
||||
void extend(hashMapOpenAddressing *hashMap) {
|
||||
void extend(HashMapOpenAddressing *hashMap) {
|
||||
// 暂存原哈希表
|
||||
Pair **bucketsTmp = hashMap->buckets;
|
||||
int oldCapacity = hashMap->capacity;
|
||||
@@ -167,7 +163,7 @@ void extend(hashMapOpenAddressing *hashMap) {
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
void print(hashMapOpenAddressing *hashMap) {
|
||||
void print(HashMapOpenAddressing *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Pair *pair = hashMap->buckets[i];
|
||||
if (pair == NULL) {
|
||||
@@ -183,7 +179,7 @@ void print(hashMapOpenAddressing *hashMap) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 初始化哈希表
|
||||
hashMapOpenAddressing *hashmap = newHashMapOpenAddressing();
|
||||
HashMapOpenAddressing *hashmap = newHashMapOpenAddressing();
|
||||
|
||||
// 添加操作
|
||||
// 在哈希表中添加键值对 (key, val)
|
||||
|
||||
Reference in New Issue
Block a user