Skip to content

Commit

Permalink
Simplify struct declarations of C.
Browse files Browse the repository at this point in the history
Use PascalCase for all structs in C.
SImplify n_queens.c
Format C code for chapter of graph.
  • Loading branch information
krahets committed Oct 17, 2023
1 parent 070d23e commit 1e49574
Show file tree
Hide file tree
Showing 35 changed files with 502 additions and 598 deletions.
34 changes: 16 additions & 18 deletions codes/c/chapter_array_and_linkedlist/my_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
#include "../utils/common.h"

/* 列表类简易实现 */
struct myList {
typedef struct {
int *arr; // 数组(存储列表元素)
int capacity; // 列表容量
int size; // 列表大小
int extendRatio; // 列表每次扩容的倍数
};
} MyList;

typedef struct myList myList;

void extendCapacity(myList *nums);
void extendCapacity(MyList *nums);

/* 构造函数 */
myList *newMyList() {
myList *nums = malloc(sizeof(myList));
MyList *newMyList() {
MyList *nums = malloc(sizeof(MyList));
nums->capacity = 10;
nums->arr = malloc(sizeof(int) * nums->capacity);
nums->size = 0;
Expand All @@ -29,35 +27,35 @@ myList *newMyList() {
}

/* 析构函数 */
void delMyList(myList *nums) {
void delMyList(MyList *nums) {
free(nums->arr);
free(nums);
}

/* 获取列表长度 */
int size(myList *nums) {
int size(MyList *nums) {
return nums->size;
}

/* 获取列表容量 */
int capacity(myList *nums) {
int capacity(MyList *nums) {
return nums->capacity;
}

/* 访问元素 */
int get(myList *nums, int index) {
int get(MyList *nums, int index) {
assert(index >= 0 && index < nums->size);
return nums->arr[index];
}

/* 更新元素 */
void set(myList *nums, int index, int num) {
void set(MyList *nums, int index, int num) {
assert(index >= 0 && index < nums->size);
nums->arr[index] = num;
}

/* 尾部添加元素 */
void add(myList *nums, int num) {
void add(MyList *nums, int num) {
if (size(nums) == capacity(nums)) {
extendCapacity(nums); // 扩容
}
Expand All @@ -66,7 +64,7 @@ void add(myList *nums, int num) {
}

/* 中间插入元素 */
void insert(myList *nums, int index, int num) {
void insert(MyList *nums, int index, int num) {
assert(index >= 0 && index < size(nums));
// 元素数量超出容量时,触发扩容机制
if (size(nums) == capacity(nums)) {
Expand All @@ -81,7 +79,7 @@ void insert(myList *nums, int index, int num) {

/* 删除元素 */
// 注意:stdio.h 占用了 remove 关键词
int removeNum(myList *nums, int index) {
int removeNum(MyList *nums, int index) {
assert(index >= 0 && index < size(nums));
int num = nums->arr[index];
for (int i = index; i < size(nums) - 1; i++) {
Expand All @@ -92,7 +90,7 @@ int removeNum(myList *nums, int index) {
}

/* 列表扩容 */
void extendCapacity(myList *nums) {
void extendCapacity(MyList *nums) {
// 先分配空间
int newCapacity = capacity(nums) * nums->extendRatio;
int *extend = (int *)malloc(sizeof(int) * newCapacity);
Expand All @@ -111,14 +109,14 @@ void extendCapacity(myList *nums) {
}

/* 将列表转换为 Array 用于打印 */
int *toArray(myList *nums) {
int *toArray(MyList *nums) {
return nums->arr;
}

/* Driver Code */
int main() {
/* 初始化列表 */
myList *nums = newMyList();
MyList *nums = newMyList();
/* 尾部添加元素 */
add(nums, 1);
add(nums, 3);
Expand Down
52 changes: 22 additions & 30 deletions codes/c/chapter_backtracking/n_queens.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@
#define MAX_N 100
#define MAX_RES 1000

/* 放置结果 */
struct result {
char ***data;
int size;
};

typedef struct result Result;

/* 回溯算法:N 皇后 */
void backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res,
bool cols[MAX_N], bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {
void backtrack(int row, int n, char state[MAX_N][MAX_N], char ***res, int *resSize, bool cols[MAX_N],
bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {
// 当放置完所有行时,记录解
if (row == n) {
res->data[res->size] = (char **)malloc(sizeof(char *) * n);
res[*resSize] = (char **)malloc(sizeof(char *) * n);
for (int i = 0; i < n; ++i) {
res->data[res->size][i] = (char *)malloc(sizeof(char) * (n + 1));
strcpy(res->data[res->size][i], state[i]);
res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));
strcpy(res[*resSize][i], state[i]);
}
res->size++;
(*resSize)++;
return;
}
// 遍历所有列
Expand All @@ -41,7 +33,7 @@ void backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res,
state[row][col] = 'Q';
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = '#';
cols[col] = diags1[diag1] = diags2[diag2] = false;
Expand All @@ -50,7 +42,7 @@ void backtrack(int row, int n, char state[MAX_N][MAX_N], Result *res,
}

/* 求解 N 皇后 */
Result *nQueens(int n) {
char ***nQueens(int n, int *returnSize) {
char state[MAX_N][MAX_N];
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
for (int i = 0; i < n; ++i) {
Expand All @@ -63,26 +55,26 @@ Result *nQueens(int n) {
bool diags1[2 * MAX_N - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_N - 1] = {false}; // 记录副对角线是否有皇后

Result *res = malloc(sizeof(Result));
res->data = (char ***)malloc(sizeof(char **) * MAX_RES);
res->size = 0;
backtrack(0, n, state, res, cols, diags1, diags2);
char ***res = (char ***)malloc(sizeof(char **) * MAX_RES);
*returnSize = 0;
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
return res;
}

/* Driver Code */
int main() {
int n = 4;
Result *res = nQueens(n);
int returnSize;
char ***res = nQueens(n, &returnSize);

printf("输入棋盘长宽为%d\n", n);
printf("皇后放置方案共有 %d 种\n", res->size);
for (int i = 0; i < res->size; ++i) {
printf("皇后放置方案共有 %d 种\n", returnSize);
for (int i = 0; i < returnSize; ++i) {
for (int j = 0; j < n; ++j) {
printf("[");
for (int k = 0; res->data[i][j][k] != '\0'; ++k) {
printf("%c", res->data[i][j][k]);
if (res->data[i][j][k + 1] != '\0') {
for (int k = 0; res[i][j][k] != '\0'; ++k) {
printf("%c", res[i][j][k]);
if (res[i][j][k + 1] != '\0') {
printf(", ");
}
}
Expand All @@ -92,13 +84,13 @@ int main() {
}

// 释放内存
for (int i = 0; i < res->size; ++i) {
for (int i = 0; i < returnSize; ++i) {
for (int j = 0; j < n; ++j) {
free(res->data[i][j]);
free(res[i][j]);
}
free(res->data[i]);
free(res[i]);
}
free(res->data);
free(res);

return 0;
}
12 changes: 5 additions & 7 deletions codes/c/chapter_computational_complexity/space_complexity.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ void constant(int n) {
}

/* 哈希表 */
struct hashTable {
typedef struct {
int key;
int val;
UT_hash_handle hh; // 基于 uthash.h 实现
};

typedef struct hashTable hashTable;
} HashTable;

/* 线性阶 */
void linear(int n) {
Expand All @@ -57,16 +55,16 @@ void linear(int n) {
free(nodes);

// 长度为 n 的哈希表占用 O(n) 空间
hashTable *h = NULL;
HashTable *h = NULL;
for (int i = 0; i < n; i++) {
hashTable *tmp = malloc(sizeof(hashTable));
HashTable *tmp = malloc(sizeof(HashTable));
tmp->key = i;
tmp->val = i;
HASH_ADD_INT(h, key, tmp);
}

// 内存释放
hashTable *curr, *tmp;
HashTable *curr, *tmp;
HASH_ITER(hh, h, curr, tmp) {
HASH_DEL(h, curr);
free(curr);
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_divide_and_conquer/binary_search_recur.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ int main() {
// 二分查找(双闭区间)
int index = binarySearch(nums, target, numsSize);
printf("目标元素 6 的索引 = %d\n", index);

return 0;
}
20 changes: 10 additions & 10 deletions codes/c/chapter_divide_and_conquer/build_tree.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* File : build_tree.c
* Created Time: 2023-10-16
* Author : lucas ([email protected])
*/
* File : build_tree.c
* Created Time: 2023-10-16
* Author : lucas ([email protected])
*/

#include "../utils/common.h"

// 假设所有元素都小于 1000
#define MAX_N 1000

/* 构建二叉树:分治 */
TreeNode* dfs(int* preorder, int* inorderMap, int i, int l, int r, int size) {
TreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {
// 子树区间为空时终止
if (r - l < 0)
return NULL;
// 初始化根节点
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
root->val = preorder[i];
root->left = NULL;
root->right = NULL;
Expand All @@ -30,13 +30,13 @@ TreeNode* dfs(int* preorder, int* inorderMap, int i, int l, int r, int size) {
}

/* 构建二叉树 */
TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
TreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {
// 初始化哈希表,存储 inorder 元素到索引的映射
int* inorderMap = (int*)malloc(sizeof(int) * MAX_N);
int *inorderMap = (int *)malloc(sizeof(int) * MAX_N);
for (int i = 0; i < inorderSize; i++) {
inorderMap[inorder[i]] = i;
}
TreeNode* root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);
TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);
free(inorderMap);
return root;
}
Expand All @@ -52,7 +52,7 @@ int main() {
printf("中序遍历 = ");
printArray(inorder, inorderSize);

TreeNode* root = buildTree(preorder, preorderSize, inorder, inorderSize);
TreeNode *root = buildTree(preorder, preorderSize, inorder, inorderSize);
printf("构建的二叉树为:\n");
printTree(root);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int climbingStairsBacktrack(int n) {
int choices[2] = {1, 2}; // 可选择向上爬 1 或 2 阶
int state = 0; // 从第 0 阶开始爬
int *res = (int *)malloc(sizeof(int));
*res = 0; // 使用 res[0] 记录方案数量
*res = 0; // 使用 res[0] 记录方案数量
int len = sizeof(choices) / sizeof(int);
backtrack(choices, state, n, res, len);
int result = *res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int climbingStairsDFSMem(int n) {
/* Driver Code */
int main() {
int n = 9;

int res = climbingStairsDFSMem(n);
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ int main() {
int costSize = sizeof(cost) / sizeof(cost[0]);
printf("输入楼梯的代价列表为 [");
for (int i = 0; i < costSize; i++) {
if (i != costSize - 1) printf("%d, ", cost[i]);
else printf("%d", cost[i]);
if (i != costSize - 1)
printf("%d, ", cost[i]);
else
printf("%d", cost[i]);
}
printf("]\n");

Expand Down
Loading

0 comments on commit 1e49574

Please sign in to comment.