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:
krahets
2023-10-18 02:16:26 +08:00
parent 070d23ee6e
commit 1e49574332
35 changed files with 503 additions and 599 deletions
@@ -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)