From c37b7c807ba8e374ebe5640a754ef55c2a83ca90 Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 9 Oct 2023 18:21:18 +0800 Subject: [PATCH] Several bug fixes --- .../c/chapter_array_and_linkedlist/my_list.c | 30 +++++++++---------- codes/c/chapter_backtracking/n_queens.c | 5 ++-- .../time_complexity.go | 2 +- docs/chapter_backtracking/n_queens_problem.md | 2 ++ 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 2bcb4dffb..8a8f718d3 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -14,13 +14,13 @@ struct myList { int extendRatio; // 列表每次扩容的倍数 }; -typedef struct myList 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; @@ -29,35 +29,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); // 扩容 } @@ -66,7 +66,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)) { @@ -81,7 +81,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++) { @@ -92,7 +92,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); @@ -111,14 +111,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); diff --git a/codes/c/chapter_backtracking/n_queens.c b/codes/c/chapter_backtracking/n_queens.c index c5997d920..56c517d9b 100644 --- a/codes/c/chapter_backtracking/n_queens.c +++ b/codes/c/chapter_backtracking/n_queens.c @@ -9,6 +9,7 @@ #define MAX_N 100 #define MAX_RES 1000 +/* 放置结果 */ struct result { char ***data; int size; @@ -17,8 +18,8 @@ struct result { 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], Result *res, + 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); diff --git a/codes/go/chapter_computational_complexity/time_complexity.go b/codes/go/chapter_computational_complexity/time_complexity.go index cdb09b755..cdf66eaf8 100644 --- a/codes/go/chapter_computational_complexity/time_complexity.go +++ b/codes/go/chapter_computational_complexity/time_complexity.go @@ -109,7 +109,7 @@ func linearLogRecur(n float64) int { if n <= 1 { return 1 } - count := linearLogRecur(n / 2) + linearLogRecur(n / 2) + count := linearLogRecur(n/2) + linearLogRecur(n/2) for i := 0.0; i < n; i++ { count++ } diff --git a/docs/chapter_backtracking/n_queens_problem.md b/docs/chapter_backtracking/n_queens_problem.md index 378edd3ee..bd7968130 100644 --- a/docs/chapter_backtracking/n_queens_problem.md +++ b/docs/chapter_backtracking/n_queens_problem.md @@ -123,6 +123,8 @@ === "C" ```c title="n_queens.c" + [class]{result}-[func]{} + [class]{}-[func]{backtrack} [class]{}-[func]{nQueens}