From 009191f712ab095eba323c6091d05fefb5bb3f61 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 15:44:02 +0800 Subject: [PATCH 01/17] feat(codes/c): add list.c --- codes/c/chapter_array_and_linkedlist/list.c | 169 ++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 codes/c/chapter_array_and_linkedlist/list.c diff --git a/codes/c/chapter_array_and_linkedlist/list.c b/codes/c/chapter_array_and_linkedlist/list.c new file mode 100644 index 000000000..9171d618a --- /dev/null +++ b/codes/c/chapter_array_and_linkedlist/list.c @@ -0,0 +1,169 @@ +/** + * File: list.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + +#include "../include/include.h" +#include + +typedef int ElemType; +#define MAX_ELEM_SIZE 10 + +// 用数组实现 list +struct list { + ElemType* nums; + size_t numsCapacity; + size_t numsSize; + bool isInit; +}; + +typedef struct list List; + +void listInit(List* l) { + if (l->isInit != true) { + l->numsCapacity = MAX_ELEM_SIZE; + l->nums = malloc(sizeof(ElemType) * l->numsCapacity); + l->numsSize = 0; + l->isInit = true; + } +} + + +size_t listSize(List* l) { + return l->numsSize; +} + +size_t listCapacity(List* l) { + return l->numsCapacity; +} + +ElemType listGet(List* l, int idx) { + if (l->isInit) { + if (idx < l->numsSize) { + return l->nums[idx]; + } + } + assert("out_of_range"); +} + +void listSet(List* l, int idx, ElemType elem) { + if (l->isInit) { + if (idx < l->numsSize) { + l->nums[idx] = elem; + } + } + assert("listSet elem assert."); +} + +void listExtendCapacity(List* l) { + +} + +void listAdd(List* l, ElemType elem) { + if (listSize(l) == listCapacity(l)) { + listExtendCapacity(l); // 扩容 + } + l->nums[listSize(l)] = elem; + l->numsSize ++; +} + +void listInsert(List* l, size_t idx, ElemType elem) { + if (l->isInit) { + if (idx < listSize(l)) { + for (size_t i=listSize(l); i>idx; --i) { + l->nums[i] = l->nums[i-1]; + } + l->nums[idx] = elem; + l->numsSize++; + } else { + // 越界 -- 抛出异常 + } + } else { + // 抛出异常 + } +} + +ElemType listRemove(List* l, int idx) { + if (l->isInit) { + if (idx < listSize(l)) { + size_t i = idx; + if ( i != listSize(l)-1) { + for (; inums[i] = l->nums[i+1]; + } + } + l->numsSize--; + + } else { + // 数组越界 + } + } else { + // 抛出异常 + } +} + +void listClear(List* l) { + l->numsSize = 0; +} + +void printList(List* l) { + size_t i=0; + + printf("["); + if (l->numsSize) { + for (; inumsSize-1; i++) { + printf("%d, ", l->nums[i]); + } + printf("%d", l->nums[i]); + } + printf("]\r\n"); +} + +int main() { + List list; + /* 初始化列表 */ + listInit(&list); + listAdd(&list, 1); + listAdd(&list, 3); + listAdd(&list, 2); + listAdd(&list, 5); + listAdd(&list, 4); + + printf("列表 list = "); + printList(&list); + + + /* 访问元素 */ + int num = listGet(&list, 1); + printf("访问索引 1 处的元素,得到 num = %d\r\n", num); + + /* 更新元素 */ + listSet(&list, 1, 0); + printf("将索引 1 处的元素更新为 0 ,得到 list = "); + printList(&list); + + /* 清空列表 */ + listClear(&list); + printf("清空列表后 list = "); + printList(&list); + + /* 尾部添加元素 */ + listAdd(&list, 1); + listAdd(&list, 3); + listAdd(&list, 2); + listAdd(&list, 5); + listAdd(&list, 4); + printf("添加元素后 list = "); + printList(&list); + + /* 中间插入元素 */ + listInsert(&list, 3, 6); + printf("在索引 3 处插入数字 6 ,得到 list = "); + printList(&list); + + /* 删除元素 */ + listRemove(&list, 3); + printf("删除索引 3 处的元素,得到 list = "); + printList(&list); +} \ No newline at end of file From e7bb42f552d374d26b815ef2a451e73ec53f8765 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 18:36:39 +0800 Subject: [PATCH 02/17] feat(codes/c): add list.c listExtendCapacity --- codes/c/chapter_array_and_linkedlist/list.c | 37 ++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/codes/c/chapter_array_and_linkedlist/list.c b/codes/c/chapter_array_and_linkedlist/list.c index 9171d618a..4af0d93ae 100644 --- a/codes/c/chapter_array_and_linkedlist/list.c +++ b/codes/c/chapter_array_and_linkedlist/list.c @@ -8,7 +8,9 @@ #include typedef int ElemType; -#define MAX_ELEM_SIZE 10 + +#define MAX_ELEM_SIZE 10 +#define CAPACITY_RACTOR 2 // 用数组实现 list struct list { @@ -29,6 +31,14 @@ void listInit(List* l) { } } +void listInitWithCapacity(List* l, size_t newCapacity) { + if (l->isInit != true) { + l->numsCapacity = newCapacity; + l->nums = malloc(sizeof(ElemType) * l->numsCapacity); + l->numsSize = 0; + l->isInit = true; + } +} size_t listSize(List* l) { return l->numsSize; @@ -57,7 +67,21 @@ void listSet(List* l, int idx, ElemType elem) { } void listExtendCapacity(List* l) { + // 先分配空间 + size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR; + ElemType *newData = (ElemType *)malloc(sizeof(ElemType) * newCapacity); + ElemType *oldData = l->nums; + // 拷贝旧数据到新数据 + for(size_t i=0; inums[i]; + + // 释放旧数据 + free(oldData); + + // 更新新数据 + l->nums = newData; + l->numsCapacity = newCapacity; } void listAdd(List* l, ElemType elem) { @@ -166,4 +190,15 @@ int main() { listRemove(&list, 3); printf("删除索引 3 处的元素,得到 list = "); printList(&list); + + /* 尾部添加元素 */ + listAdd(&list, 1); + listAdd(&list, 3); + listAdd(&list, 2); + listAdd(&list, 5); + listAdd(&list, 4); + listAdd(&list, 4); + + printf("添加元素后 list = "); + printList(&list); } \ No newline at end of file From 3970e88be2b995190c498cf6e8a54bcaecb2d779 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 20:27:18 +0800 Subject: [PATCH 03/17] style(codes/c): update comment format --- codes/c/chapter_array_and_linkedlist/list.c | 62 +++++++++++---------- codes/c/include/include.h | 1 + 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/list.c b/codes/c/chapter_array_and_linkedlist/list.c index 4af0d93ae..52deb1e17 100644 --- a/codes/c/chapter_array_and_linkedlist/list.c +++ b/codes/c/chapter_array_and_linkedlist/list.c @@ -5,18 +5,16 @@ */ #include "../include/include.h" -#include -typedef int ElemType; #define MAX_ELEM_SIZE 10 #define CAPACITY_RACTOR 2 // 用数组实现 list struct list { - ElemType* nums; - size_t numsCapacity; - size_t numsSize; + int* nums; + size_t cap; + size_t size; bool isInit; }; @@ -24,42 +22,46 @@ typedef struct list List; void listInit(List* l) { if (l->isInit != true) { - l->numsCapacity = MAX_ELEM_SIZE; - l->nums = malloc(sizeof(ElemType) * l->numsCapacity); - l->numsSize = 0; + l->cap = MAX_ELEM_SIZE; + l->nums = malloc(sizeof(int) * l->cap); + l->size = 0; l->isInit = true; } } void listInitWithCapacity(List* l, size_t newCapacity) { if (l->isInit != true) { - l->numsCapacity = newCapacity; - l->nums = malloc(sizeof(ElemType) * l->numsCapacity); - l->numsSize = 0; + l->cap = newCapacity; + l->nums = malloc(sizeof(int) * l->cap); + l->size = 0; l->isInit = true; } } size_t listSize(List* l) { - return l->numsSize; + return l->size; } size_t listCapacity(List* l) { - return l->numsCapacity; + return l->cap; } -ElemType listGet(List* l, int idx) { +int listGet(List* l, int idx) { + assert(l->isInit); if (l->isInit) { - if (idx < l->numsSize) { + if (idx < l->size) { return l->nums[idx]; + } else { + } } - assert("out_of_range"); + } -void listSet(List* l, int idx, ElemType elem) { +void listSet(List* l, int idx, int elem) { + assert(l->isInit); if (l->isInit) { - if (idx < l->numsSize) { + if (idx < l->size) { l->nums[idx] = elem; } } @@ -69,8 +71,8 @@ void listSet(List* l, int idx, ElemType elem) { void listExtendCapacity(List* l) { // 先分配空间 size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR; - ElemType *newData = (ElemType *)malloc(sizeof(ElemType) * newCapacity); - ElemType *oldData = l->nums; + int *newData = (int *)malloc(sizeof(int) * newCapacity); + int *oldData = l->nums; // 拷贝旧数据到新数据 for(size_t i=0; inums = newData; - l->numsCapacity = newCapacity; + l->cap = newCapacity; } -void listAdd(List* l, ElemType elem) { +void listAdd(List* l, int elem) { if (listSize(l) == listCapacity(l)) { listExtendCapacity(l); // 扩容 } l->nums[listSize(l)] = elem; - l->numsSize ++; + l->size ++; } -void listInsert(List* l, size_t idx, ElemType elem) { +void listInsert(List* l, size_t idx, int elem) { if (l->isInit) { if (idx < listSize(l)) { for (size_t i=listSize(l); i>idx; --i) { l->nums[i] = l->nums[i-1]; } l->nums[idx] = elem; - l->numsSize++; + l->size++; } else { // 越界 -- 抛出异常 } @@ -108,7 +110,7 @@ void listInsert(List* l, size_t idx, ElemType elem) { } } -ElemType listRemove(List* l, int idx) { +int listRemove(List* l, int idx) { if (l->isInit) { if (idx < listSize(l)) { size_t i = idx; @@ -117,7 +119,7 @@ ElemType listRemove(List* l, int idx) { l->nums[i] = l->nums[i+1]; } } - l->numsSize--; + l->size--; } else { // 数组越界 @@ -128,15 +130,15 @@ ElemType listRemove(List* l, int idx) { } void listClear(List* l) { - l->numsSize = 0; + l->size = 0; } void printList(List* l) { size_t i=0; printf("["); - if (l->numsSize) { - for (; inumsSize-1; i++) { + if (l->size) { + for (; isize-1; i++) { printf("%d, ", l->nums[i]); } printf("%d", l->nums[i]); diff --git a/codes/c/include/include.h b/codes/c/include/include.h index 9f30b0529..e3919a9f5 100644 --- a/codes/c/include/include.h +++ b/codes/c/include/include.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "list_node.h" #include "tree_node.h" From 2a1b47900296feaf0c55339e08664c0373109d5b Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 20:42:36 +0800 Subject: [PATCH 04/17] feat(codes/c): add list.c cmake compile method --- codes/c/chapter_array_and_linkedlist/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt index dfa3322fb..144aa2d74 100644 --- a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt +++ b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt @@ -1,2 +1,3 @@ add_executable(array array.c) -add_executable(linked_list linked_list.c) \ No newline at end of file +add_executable(linked_list linked_list.c) +add_executable(list list.c) \ No newline at end of file From cbbb7d34b2bfce9a6a3b7e597593bab20514e1cb Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 20:44:17 +0800 Subject: [PATCH 05/17] feat(code/c): add c code Makefile compile method --- codes/c/Makefile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codes/c/Makefile diff --git a/codes/c/Makefile b/codes/c/Makefile new file mode 100644 index 000000000..6d360feeb --- /dev/null +++ b/codes/c/Makefile @@ -0,0 +1,15 @@ +CXXFLAGS = # -Wall -Werror +LDFLAGS = + +src = $(wildcard ./*/*.c) +target = $(patsubst %.c, %, ${src}) + +.PHONY: all clean + +%:%.cpp + $(CXX) ${CXXFLAGS} ${LDFLAGS} $^ -o $@ + +all: ${target} + +clean: + rm -f ${target} \ No newline at end of file From 153846c94b3008e67c46d9ff1302f8a2fe9509ab Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 21:09:51 +0800 Subject: [PATCH 06/17] feat(codes/c/): add array_stack and linkedlist_stack frame --- codes/c/CMakeLists.txt | 1 + .../c/chapter_stack_and_queue/CMakeLists.txt | 2 + codes/c/chapter_stack_and_queue/array_stack.c | 43 +++++++++++++++++++ .../linkedlist_stack.c | 42 ++++++++++++++++++ codes/c/chapter_tree/binary_search_tree.c | 3 ++ 5 files changed, 91 insertions(+) create mode 100644 codes/c/chapter_stack_and_queue/CMakeLists.txt create mode 100644 codes/c/chapter_stack_and_queue/array_stack.c create mode 100644 codes/c/chapter_stack_and_queue/linkedlist_stack.c diff --git a/codes/c/CMakeLists.txt b/codes/c/CMakeLists.txt index 1a208dd9d..932d74fd1 100644 --- a/codes/c/CMakeLists.txt +++ b/codes/c/CMakeLists.txt @@ -10,3 +10,4 @@ add_subdirectory(chapter_computational_complexity) add_subdirectory(chapter_array_and_linkedlist) add_subdirectory(chapter_sorting) add_subdirectory(chapter_tree) +add_subdirectory(chapter_stack_and_queue) diff --git a/codes/c/chapter_stack_and_queue/CMakeLists.txt b/codes/c/chapter_stack_and_queue/CMakeLists.txt new file mode 100644 index 000000000..5c2be36b6 --- /dev/null +++ b/codes/c/chapter_stack_and_queue/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(array_stack array_stack.c) +add_executable(linkedlist_stack linkedlist_stack.c) \ No newline at end of file diff --git a/codes/c/chapter_stack_and_queue/array_stack.c b/codes/c/chapter_stack_and_queue/array_stack.c new file mode 100644 index 000000000..df7e08139 --- /dev/null +++ b/codes/c/chapter_stack_and_queue/array_stack.c @@ -0,0 +1,43 @@ +/** + * File: array_stack.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + +#include "../include/include.h" + + +struct ArrayStack { + int *array; + size_t stkSize; +}; + +typedef struct ArrayStack ArrayStack; + +void new(ArrayStack* stk) { + +} + +size_t size(ArrayStack* stk) { + +} + +bool empty(ArrayStack* stk) { + +} + +void push(ArrayStack* stk, int num) { + +} + +void pop(ArrayStack* stk) { + +} + +int top(ArrayStack* stk) { + +} + +int main() { + return 0; +} \ No newline at end of file diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c new file mode 100644 index 000000000..e190d3d63 --- /dev/null +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -0,0 +1,42 @@ +/** + * File: linkedlist_stack.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + +#include "../include/include.h" + +struct LinkedListStack { + ListNode* stkTop; + size_t stkSize; +}; + +typedef struct LinkedListStack LinkedListStack; + +void new(LinkedListStack* stk) { + +} + +size_t size(LinkedListStack* stk) { + +} + +bool empty(LinkedListStack* stk) { + +} + +void push(LinkedListStack* stk, int num) { + +} + +void pop(LinkedListStack* stk) { + +} + +int top(LinkedListStack* stk) { + +} + +int main() { + return 0; +} diff --git a/codes/c/chapter_tree/binary_search_tree.c b/codes/c/chapter_tree/binary_search_tree.c index f993faec3..908216db4 100644 --- a/codes/c/chapter_tree/binary_search_tree.c +++ b/codes/c/chapter_tree/binary_search_tree.c @@ -6,3 +6,6 @@ #include "../include/include.h" +int main() { + return 0; +} \ No newline at end of file From 3839f47ac0c5e056911d19ea5079ce0a2ce37dab Mon Sep 17 00:00:00 2001 From: Gonglja Date: Fri, 13 Jan 2023 05:48:29 +0800 Subject: [PATCH 07/17] feat(codes/c): update linkedlist_stack.c some code --- .../linkedlist_stack.c | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index e190d3d63..fe457afd9 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -14,27 +14,64 @@ struct LinkedListStack { typedef struct LinkedListStack LinkedListStack; void new(LinkedListStack* stk) { + // 创建头结点 + stk->stkTop = (ListNode *)malloc(sizeof(ListNode)); + stk->stkTop->next = NULL; + stk->stkTop->val = 0; + // 初始化栈大小 + stk->stkSize = 0; } size_t size(LinkedListStack* stk) { - + assert(stk); + return stk->stkSize; } bool empty(LinkedListStack* stk) { - + assert(stk); + return size(stk) == 0; } void push(LinkedListStack* stk, int num) { + assert(stk); + // 创建一个新结点 + ListNode *n = (ListNode *)malloc(sizeof(ListNode)); + ListNode *h; + n->next = NULL; + n->val = num; + + // 遍历链表,将新结点挂在最后面 + h = stk->stkTop; + while(h && h->next) { + h = h->next; + } + h->next = n; + + // 栈大小自增 + stk->stkSize++; } void pop(LinkedListStack* stk) { + assert(stk); + ListNode *h = stk->stkTop; + ListNode *n; + // 找到倒数第一个结点 h -> (h->next) -> (h->next->next) + while(h && h->next && h->next->next) { + h = h->next; + } + // 先保存倒数个结点,断开 + n = h->next; + h->next = NULL; + + // 删除保存的结点 + free(n); } int top(LinkedListStack* stk) { - + assert(stk); } int main() { From 08d715a2e9dcb314d1c63a9d785b1b70f4ad0f69 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Fri, 13 Jan 2023 06:44:31 +0800 Subject: [PATCH 08/17] feat(codes/c): update linkedlist_stack.c some code --- .../linkedlist_stack.c | 111 ++++++++++-------- codes/c/include/print_util.h | 8 ++ 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index fe457afd9..714ed1fb2 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -6,74 +6,87 @@ #include "../include/include.h" +/* 基于链表实现的栈 */ struct LinkedListStack { - ListNode* stkTop; - size_t stkSize; + ListNode* stackTop; // 将头结点作为栈顶 + size_t stkSize; // 栈的长度 }; typedef struct LinkedListStack LinkedListStack; -void new(LinkedListStack* stk) { - // 创建头结点 - stk->stkTop = (ListNode *)malloc(sizeof(ListNode)); - stk->stkTop->next = NULL; - stk->stkTop->val = 0; - - // 初始化栈大小 +void constructor(LinkedListStack* stk) { + stk->stackTop = NULL; stk->stkSize = 0; } - +/* 获取栈的长度 */ size_t size(LinkedListStack* stk) { assert(stk); return stk->stkSize; } - +/* 判断栈是否为空 */ bool empty(LinkedListStack* stk) { assert(stk); return size(stk) == 0; } - -void push(LinkedListStack* stk, int num) { - assert(stk); - - // 创建一个新结点 - ListNode *n = (ListNode *)malloc(sizeof(ListNode)); - ListNode *h; - n->next = NULL; - n->val = num; - - // 遍历链表,将新结点挂在最后面 - h = stk->stkTop; - while(h && h->next) { - h = h->next; - } - h->next = n; - - // 栈大小自增 - stk->stkSize++; -} - -void pop(LinkedListStack* stk) { - assert(stk); - ListNode *h = stk->stkTop; - ListNode *n; - // 找到倒数第一个结点 h -> (h->next) -> (h->next->next) - while(h && h->next && h->next->next) { - h = h->next; - } - - // 先保存倒数个结点,断开 - n = h->next; - h->next = NULL; - - // 删除保存的结点 - free(n); -} - +/* 访问栈顶元素 */ int top(LinkedListStack* stk) { assert(stk); + if (size(stk) == 0 ) + // 抛出异常 + return stk->stackTop->val; +} +/* 入栈 */ +void push(LinkedListStack* stk, int num) { + assert(stk); + ListNode *node = (ListNode *)malloc(sizeof(ListNode)); + node->next = stk->stackTop; + node->val = num; + stk->stackTop = node; + stk->stkSize++; +} +/* 出栈 */ +void pop(LinkedListStack* stk) { + assert(stk); + int num = top(stk); + ListNode *tmp = stk->stackTop; + stk->stackTop = stk->stackTop->next; + // 释放内存 + free(tmp); + stk->stkSize--; } + +/* Driver Code */ int main() { + /* 初始化栈 */ + LinkedListStack stack; + + /* 元素入栈 */ + push(&stack, 1); + push(&stack, 3); + push(&stack, 2); + push(&stack, 5); + push(&stack, 4); + + printf("栈 stack = "); + printStack(&stack); + + /* 访问栈顶元素 */ + int stackTop = top(&stack); + printf("栈顶元素 top = %d\r\n", stackTop); + + /* 元素出栈 */ + pop(&stack); + printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop); + printStack(&stack); + + /* 获取栈的长度 */ + size_t stackSize = size(&stack); + printf("栈的长度 size = %ld\r\n", stackSize); + + /* 判断是否为空 */ + bool isEmpty = empty(&stack); + printf("栈是否为空 = %d", isEmpty); + return 0; } diff --git a/codes/c/include/print_util.h b/codes/c/include/print_util.h index a0c3a1500..f7955b586 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -127,6 +127,14 @@ static void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } +/** + * @brief Print a stack + * + * @param head + */ +static void printStack(void *stack) { + +} #ifdef __cplusplus } From a94010645db4200fd7ed5f009506164df9292d30 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Sat, 14 Jan 2023 15:45:38 +0800 Subject: [PATCH 09/17] feat(docs/c): add linkedlist_stack.c --- .../linkedlist_stack.c | 26 +++++++++---------- codes/c/include/print_util.h | 8 ------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index 714ed1fb2..f8d56459e 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -9,7 +9,7 @@ /* 基于链表实现的栈 */ struct LinkedListStack { ListNode* stackTop; // 将头结点作为栈顶 - size_t stkSize; // 栈的长度 + int stkSize; // 栈的长度 }; typedef struct LinkedListStack LinkedListStack; @@ -19,7 +19,7 @@ void constructor(LinkedListStack* stk) { stk->stkSize = 0; } /* 获取栈的长度 */ -size_t size(LinkedListStack* stk) { +int size(LinkedListStack* stk) { assert(stk); return stk->stkSize; } @@ -31,18 +31,17 @@ bool empty(LinkedListStack* stk) { /* 访问栈顶元素 */ int top(LinkedListStack* stk) { assert(stk); - if (size(stk) == 0 ) - // 抛出异常 + assert(size(stk) != 0); return stk->stackTop->val; } /* 入栈 */ void push(LinkedListStack* stk, int num) { assert(stk); ListNode *node = (ListNode *)malloc(sizeof(ListNode)); - node->next = stk->stackTop; - node->val = num; - stk->stackTop = node; - stk->stkSize++; + node->next = stk->stackTop; // 更新新加结点指针域 + node->val = num; // 更新新加结点数据域 + stk->stackTop = node; // 更新栈顶 + stk->stkSize++; // 更新栈大小 } /* 出栈 */ void pop(LinkedListStack* stk) { @@ -60,6 +59,7 @@ void pop(LinkedListStack* stk) { int main() { /* 初始化栈 */ LinkedListStack stack; + constructor(&stack); /* 元素入栈 */ push(&stack, 1); @@ -69,7 +69,7 @@ int main() { push(&stack, 4); printf("栈 stack = "); - printStack(&stack); + printLinkedList(stack.stackTop); /* 访问栈顶元素 */ int stackTop = top(&stack); @@ -78,15 +78,15 @@ int main() { /* 元素出栈 */ pop(&stack); printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop); - printStack(&stack); + printLinkedList(stack.stackTop); /* 获取栈的长度 */ - size_t stackSize = size(&stack); - printf("栈的长度 size = %ld\r\n", stackSize); + int stackSize = size(&stack); + printf("栈的长度 size = %d\r\n", stackSize); /* 判断是否为空 */ bool isEmpty = empty(&stack); - printf("栈是否为空 = %d", isEmpty); + printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no"); return 0; } diff --git a/codes/c/include/print_util.h b/codes/c/include/print_util.h index f7955b586..a0c3a1500 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -127,14 +127,6 @@ static void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } -/** - * @brief Print a stack - * - * @param head - */ -static void printStack(void *stack) { - -} #ifdef __cplusplus } From dceae63f383fb3cdfe3ecb302fa6b7049e99fe19 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Sat, 14 Jan 2023 16:58:38 +0800 Subject: [PATCH 10/17] feat(codes/c): Rename `list.c` to `my_list.c`, modify part of the code in `my_list.c` --- codes/c/chapter_array_and_linkedlist/list.c | 206 ------------------ .../c/chapter_array_and_linkedlist/my_list.c | 154 +++++++++++++ 2 files changed, 154 insertions(+), 206 deletions(-) delete mode 100644 codes/c/chapter_array_and_linkedlist/list.c create mode 100644 codes/c/chapter_array_and_linkedlist/my_list.c diff --git a/codes/c/chapter_array_and_linkedlist/list.c b/codes/c/chapter_array_and_linkedlist/list.c deleted file mode 100644 index 52deb1e17..000000000 --- a/codes/c/chapter_array_and_linkedlist/list.c +++ /dev/null @@ -1,206 +0,0 @@ -/** - * File: list.c - * Created Time: 2022-01-12 - * Author: Zero (glj0@outlook.com) - */ - -#include "../include/include.h" - - -#define MAX_ELEM_SIZE 10 -#define CAPACITY_RACTOR 2 - -// 用数组实现 list -struct list { - int* nums; - size_t cap; - size_t size; - bool isInit; -}; - -typedef struct list List; - -void listInit(List* l) { - if (l->isInit != true) { - l->cap = MAX_ELEM_SIZE; - l->nums = malloc(sizeof(int) * l->cap); - l->size = 0; - l->isInit = true; - } -} - -void listInitWithCapacity(List* l, size_t newCapacity) { - if (l->isInit != true) { - l->cap = newCapacity; - l->nums = malloc(sizeof(int) * l->cap); - l->size = 0; - l->isInit = true; - } -} - -size_t listSize(List* l) { - return l->size; -} - -size_t listCapacity(List* l) { - return l->cap; -} - -int listGet(List* l, int idx) { - assert(l->isInit); - if (l->isInit) { - if (idx < l->size) { - return l->nums[idx]; - } else { - - } - } - -} - -void listSet(List* l, int idx, int elem) { - assert(l->isInit); - if (l->isInit) { - if (idx < l->size) { - l->nums[idx] = elem; - } - } - assert("listSet elem assert."); -} - -void listExtendCapacity(List* l) { - // 先分配空间 - size_t newCapacity = listCapacity(l) * CAPACITY_RACTOR; - int *newData = (int *)malloc(sizeof(int) * newCapacity); - int *oldData = l->nums; - - // 拷贝旧数据到新数据 - for(size_t i=0; inums[i]; - - // 释放旧数据 - free(oldData); - - // 更新新数据 - l->nums = newData; - l->cap = newCapacity; -} - -void listAdd(List* l, int elem) { - if (listSize(l) == listCapacity(l)) { - listExtendCapacity(l); // 扩容 - } - l->nums[listSize(l)] = elem; - l->size ++; -} - -void listInsert(List* l, size_t idx, int elem) { - if (l->isInit) { - if (idx < listSize(l)) { - for (size_t i=listSize(l); i>idx; --i) { - l->nums[i] = l->nums[i-1]; - } - l->nums[idx] = elem; - l->size++; - } else { - // 越界 -- 抛出异常 - } - } else { - // 抛出异常 - } -} - -int listRemove(List* l, int idx) { - if (l->isInit) { - if (idx < listSize(l)) { - size_t i = idx; - if ( i != listSize(l)-1) { - for (; inums[i] = l->nums[i+1]; - } - } - l->size--; - - } else { - // 数组越界 - } - } else { - // 抛出异常 - } -} - -void listClear(List* l) { - l->size = 0; -} - -void printList(List* l) { - size_t i=0; - - printf("["); - if (l->size) { - for (; isize-1; i++) { - printf("%d, ", l->nums[i]); - } - printf("%d", l->nums[i]); - } - printf("]\r\n"); -} - -int main() { - List list; - /* 初始化列表 */ - listInit(&list); - listAdd(&list, 1); - listAdd(&list, 3); - listAdd(&list, 2); - listAdd(&list, 5); - listAdd(&list, 4); - - printf("列表 list = "); - printList(&list); - - - /* 访问元素 */ - int num = listGet(&list, 1); - printf("访问索引 1 处的元素,得到 num = %d\r\n", num); - - /* 更新元素 */ - listSet(&list, 1, 0); - printf("将索引 1 处的元素更新为 0 ,得到 list = "); - printList(&list); - - /* 清空列表 */ - listClear(&list); - printf("清空列表后 list = "); - printList(&list); - - /* 尾部添加元素 */ - listAdd(&list, 1); - listAdd(&list, 3); - listAdd(&list, 2); - listAdd(&list, 5); - listAdd(&list, 4); - printf("添加元素后 list = "); - printList(&list); - - /* 中间插入元素 */ - listInsert(&list, 3, 6); - printf("在索引 3 处插入数字 6 ,得到 list = "); - printList(&list); - - /* 删除元素 */ - listRemove(&list, 3); - printf("删除索引 3 处的元素,得到 list = "); - printList(&list); - - /* 尾部添加元素 */ - listAdd(&list, 1); - listAdd(&list, 3); - listAdd(&list, 2); - listAdd(&list, 5); - listAdd(&list, 4); - listAdd(&list, 4); - - printf("添加元素后 list = "); - printList(&list); -} \ No newline at end of file diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c new file mode 100644 index 000000000..4f530b6ea --- /dev/null +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -0,0 +1,154 @@ +/** + * File: list.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + +#include "../include/include.h" + + +// 用数组实现 list +struct mylist { + int* nums; // 数组(存储列表元素) + int capacity; // 列表容量 + int size; // 列表大小 + int extendRatio; // 列表每次扩容的倍数 +}; + +typedef struct mylist MyList; +/* 声明函数 */ +void extendCapacity(MyList* l); + +/* 构造函数 */ +void constructor(MyList* l) { + l->capacity = 10; + l->nums = malloc(sizeof(int) * l->capacity); + l->size = 0; + l->extendRatio = 2; +} + +/* 获取列表长度 */ +int size(MyList* l) { + return l->size; +} +/* 获取列表容量 */ +int capacity(MyList* l) { + return l->capacity; +} +/* 访问元素 */ +int get(MyList* l, int idx) { + if (idx < l->size) { + return l->nums[idx]; + } else { + + } +} +/* 更新元素 */ +void set(MyList* l, int idx, int elem) { + if (idx < l->size) { + l->nums[idx] = elem; + } +} +/* 尾部添加元素 */ +void add(MyList* l, int elem) { + if (size(l) == capacity(l)) { + extendCapacity(l); // 扩容 + } + l->nums[size(l)] = elem; + l->size ++; +} +/* 中间插入元素 */ +void insert(MyList* l, int idx, int elem) { + if (idx < size(l)) { + for (int i=size(l); i>idx; --i) { + l->nums[i] = l->nums[i-1]; + } + l->nums[idx] = elem; + l->size++; + } else { + // 越界:异常处理 + } +} +/* 删除元素 */ +// 由于引入了 stdio.h ,此处无法使用 remove 关键词 +// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 +int removeNum(MyList* l, int idx) { + if (idx < size(l)) { + size_t i = idx; + if ( i != size(l)-1) { + for (; inums[i] = l->nums[i+1]; + } + } + l->size--; + } else { + // 数组越界 + } +} +/* 列表扩容 */ +void extendCapacity(MyList* l) { + // 先分配空间 + size_t newCapacity = capacity(l) * l->extendRatio; + int *extend = (int *)malloc(sizeof(int) * newCapacity); + int *temp = l->nums; + + // 拷贝旧数据到新数据 + for(size_t i=0; inums[i]; + + // 释放旧数据 + free(temp); + + // 更新新数据 + l->nums = extend; + l->capacity = newCapacity; +} + +/* 将列表转换为 Array 用于打印 */ +int* toArray(MyList* l) { + return l->nums; +} + +int main() { + /* 初始化列表 */ + MyList list; + constructor(&list); + /* 尾部添加元素 */ + add(&list, 1); + add(&list, 3); + add(&list, 2); + add(&list, 5); + add(&list, 4); + printf("列表 list = "); + printArray(toArray(&list), size(&list)); + printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list)); + + /* 中间插入元素 */ + insert(&list, 3, 6); + printf("在索引 3 处插入数字 6 ,得到 list = "); + printArray(toArray(&list), size(&list)); + + /* 删除元素 */ + removeNum(&list, 3); + printf("删除索引 3 处的元素,得到 list = "); + printArray(toArray(&list), size(&list)); + + /* 访问元素 */ + int num = get(&list, 1); + printf("访问索引 1 处的元素,得到 num = %d\r\n", num); + + /* 更新元素 */ + set(&list, 1, 0); + printf("将索引 1 处的元素更新为 0 ,得到 list = "); + printArray(toArray(&list), size(&list)); + + /* 测试扩容机制 */ + for (int i = 0; i < 10; i++) { + // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 + add(&list, i); + } + + printf("扩容后的列表 list = "); + printArray(toArray(&list), size(&list)); + printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list)); +} \ No newline at end of file From 81cdf1d53039e2005e2f3db3367acd8b232ce598 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Sat, 14 Jan 2023 17:10:04 +0800 Subject: [PATCH 11/17] docs(codes/c): add function description --- codes/c/chapter_array_and_linkedlist/my_list.c | 2 +- codes/c/chapter_stack_and_queue/linkedlist_stack.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 4f530b6ea..f80802448 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -16,7 +16,7 @@ struct mylist { }; typedef struct mylist MyList; -/* 声明函数 */ +/* 前置声明 */ void extendCapacity(MyList* l); /* 构造函数 */ diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index f8d56459e..c0d02c968 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -13,7 +13,7 @@ struct LinkedListStack { }; typedef struct LinkedListStack LinkedListStack; - +/* 构造函数 */ void constructor(LinkedListStack* stk) { stk->stackTop = NULL; stk->stkSize = 0; From c461b134363fd8bfab7dfa45b0cedc8b4ce87504 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Sat, 14 Jan 2023 20:32:35 +0800 Subject: [PATCH 12/17] style(codes/c): Update code style --- .../c/chapter_array_and_linkedlist/my_list.c | 66 +++++++++---------- .../linkedlist_stack.c | 21 +++--- 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index f80802448..a1ca7717a 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -20,7 +20,7 @@ typedef struct mylist MyList; void extendCapacity(MyList* l); /* 构造函数 */ -void constructor(MyList* l) { +void newMyList(MyList* l) { l->capacity = 10; l->nums = malloc(sizeof(int) * l->capacity); l->size = 0; @@ -31,60 +31,56 @@ void constructor(MyList* l) { int size(MyList* l) { return l->size; } + /* 获取列表容量 */ int capacity(MyList* l) { return l->capacity; } + /* 访问元素 */ -int get(MyList* l, int idx) { - if (idx < l->size) { - return l->nums[idx]; - } else { - - } +int get(MyList* l, int index) { + assert(index < l->size); + return l->nums[index]; } + /* 更新元素 */ -void set(MyList* l, int idx, int elem) { - if (idx < l->size) { - l->nums[idx] = elem; - } +void set(MyList* l, int index, int num) { + assert(index < l->size); + l->nums[index] = num; } + /* 尾部添加元素 */ -void add(MyList* l, int elem) { +void add(MyList* l, int num) { if (size(l) == capacity(l)) { extendCapacity(l); // 扩容 } - l->nums[size(l)] = elem; - l->size ++; + l->nums[size(l)] = num; + l->size++; } + /* 中间插入元素 */ -void insert(MyList* l, int idx, int elem) { - if (idx < size(l)) { - for (int i=size(l); i>idx; --i) { - l->nums[i] = l->nums[i-1]; - } - l->nums[idx] = elem; - l->size++; - } else { - // 越界:异常处理 +void insert(MyList* l, int index, int num) { + assert(index < size(l)); + for (int i=size(l); i>index; --i) { + l->nums[i] = l->nums[i-1]; } + l->nums[index] = num; + l->size++; } + /* 删除元素 */ // 由于引入了 stdio.h ,此处无法使用 remove 关键词 // 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 -int removeNum(MyList* l, int idx) { - if (idx < size(l)) { - size_t i = idx; - if ( i != size(l)-1) { - for (; inums[i] = l->nums[i+1]; - } - } - l->size--; - } else { - // 数组越界 +int removeNum(MyList* l, int index) { + assert(index < size(l)); + int num = l->nums[index]; + for (int i=index; inums[i] = l->nums[i+1]; } + l->size--; + return num; } + /* 列表扩容 */ void extendCapacity(MyList* l) { // 先分配空间 @@ -112,7 +108,7 @@ int* toArray(MyList* l) { int main() { /* 初始化列表 */ MyList list; - constructor(&list); + newMyList(&list); /* 尾部添加元素 */ add(&list, 1); add(&list, 3); diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index c0d02c968..5f09b8cf8 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -9,31 +9,36 @@ /* 基于链表实现的栈 */ struct LinkedListStack { ListNode* stackTop; // 将头结点作为栈顶 - int stkSize; // 栈的长度 + int size; // 栈的长度 }; typedef struct LinkedListStack LinkedListStack; + /* 构造函数 */ -void constructor(LinkedListStack* stk) { +void newLinkedListStack(LinkedListStack* stk) { stk->stackTop = NULL; - stk->stkSize = 0; + stk->size = 0; } + /* 获取栈的长度 */ int size(LinkedListStack* stk) { assert(stk); - return stk->stkSize; + return stk->size; } + /* 判断栈是否为空 */ bool empty(LinkedListStack* stk) { assert(stk); return size(stk) == 0; } + /* 访问栈顶元素 */ int top(LinkedListStack* stk) { assert(stk); assert(size(stk) != 0); return stk->stackTop->val; } + /* 入栈 */ void push(LinkedListStack* stk, int num) { assert(stk); @@ -41,8 +46,9 @@ void push(LinkedListStack* stk, int num) { node->next = stk->stackTop; // 更新新加结点指针域 node->val = num; // 更新新加结点数据域 stk->stackTop = node; // 更新栈顶 - stk->stkSize++; // 更新栈大小 + stk->size++; // 更新栈大小 } + /* 出栈 */ void pop(LinkedListStack* stk) { assert(stk); @@ -51,15 +57,14 @@ void pop(LinkedListStack* stk) { stk->stackTop = stk->stackTop->next; // 释放内存 free(tmp); - stk->stkSize--; + stk->size--; } - /* Driver Code */ int main() { /* 初始化栈 */ LinkedListStack stack; - constructor(&stack); + newLinkedListStack(&stack); /* 元素入栈 */ push(&stack, 1); From 2ff4c00fea3bcfb0d20dd655f3976bad2050fc6b Mon Sep 17 00:00:00 2001 From: Gonglja Date: Sat, 14 Jan 2023 20:33:58 +0800 Subject: [PATCH 13/17] fix(codes/c): Remove makefile --- codes/c/Makefile | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 codes/c/Makefile diff --git a/codes/c/Makefile b/codes/c/Makefile deleted file mode 100644 index 6d360feeb..000000000 --- a/codes/c/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -CXXFLAGS = # -Wall -Werror -LDFLAGS = - -src = $(wildcard ./*/*.c) -target = $(patsubst %.c, %, ${src}) - -.PHONY: all clean - -%:%.cpp - $(CXX) ${CXXFLAGS} ${LDFLAGS} $^ -o $@ - -all: ${target} - -clean: - rm -f ${target} \ No newline at end of file From 78b4c4f029af6684b799b093734b67c6a86ad4d1 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Mon, 16 Jan 2023 16:16:38 +0800 Subject: [PATCH 14/17] fix(codes/c): Fix compilation error of `list.c` caused by renaming. --- codes/c/chapter_array_and_linkedlist/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt index 144aa2d74..29677a0be 100644 --- a/codes/c/chapter_array_and_linkedlist/CMakeLists.txt +++ b/codes/c/chapter_array_and_linkedlist/CMakeLists.txt @@ -1,3 +1,3 @@ add_executable(array array.c) add_executable(linked_list linked_list.c) -add_executable(list list.c) \ No newline at end of file +add_executable(my_list my_list.c) \ No newline at end of file From 3698865b3228259ab604d38bedfe95b60c26701f Mon Sep 17 00:00:00 2001 From: Gonglja Date: Mon, 16 Jan 2023 16:27:55 +0800 Subject: [PATCH 15/17] fix(codes/c): Fix linkedlist_stack.c memory release problem --- codes/c/chapter_stack_and_queue/linkedlist_stack.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index 5f09b8cf8..a63ea1919 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -20,6 +20,16 @@ void newLinkedListStack(LinkedListStack* stk) { stk->size = 0; } +/* 析构函数 */ +void delLinkedListStack(LinkedListStack* stk) { + while(stk->stackTop) { + ListNode *n = stk->stackTop->next; + free(stk->stackTop); + stk->stackTop = n; + } + stk->size = 0; +} + /* 获取栈的长度 */ int size(LinkedListStack* stk) { assert(stk); @@ -64,6 +74,7 @@ void pop(LinkedListStack* stk) { int main() { /* 初始化栈 */ LinkedListStack stack; + /* 构造函数 */ newLinkedListStack(&stack); /* 元素入栈 */ @@ -93,5 +104,7 @@ int main() { bool isEmpty = empty(&stack); printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no"); + /* 析构函数 */ + delLinkedListStack(&stack); return 0; } From ed91372594c75a434b356ef388e8d22fce203526 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Wed, 18 Jan 2023 08:14:19 +0800 Subject: [PATCH 16/17] style(codes/c): Update my_list.c code format --- .../c/chapter_array_and_linkedlist/my_list.c | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index a1ca7717a..39a1aea58 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -17,92 +17,92 @@ struct mylist { typedef struct mylist MyList; /* 前置声明 */ -void extendCapacity(MyList* l); +void extendCapacity(MyList *list); /* 构造函数 */ -void newMyList(MyList* l) { - l->capacity = 10; - l->nums = malloc(sizeof(int) * l->capacity); - l->size = 0; - l->extendRatio = 2; +void newMyList(MyList *list) { + list->capacity = 10; + list->nums = malloc(sizeof(int) * list->capacity); + list->size = 0; + list->extendRatio = 2; } /* 获取列表长度 */ -int size(MyList* l) { - return l->size; +int size(MyList *list) { + return list->size; } /* 获取列表容量 */ -int capacity(MyList* l) { - return l->capacity; +int capacity(MyList *list) { + return list->capacity; } /* 访问元素 */ -int get(MyList* l, int index) { - assert(index < l->size); - return l->nums[index]; +int get(MyList *list, int index) { + assert(index < list->size); + return list->nums[index]; } /* 更新元素 */ -void set(MyList* l, int index, int num) { - assert(index < l->size); - l->nums[index] = num; +void set(MyList *list, int index, int num) { + assert(index < list->size); + list->nums[index] = num; } /* 尾部添加元素 */ -void add(MyList* l, int num) { - if (size(l) == capacity(l)) { - extendCapacity(l); // 扩容 +void add(MyList *list, int num) { + if (size(list) == capacity(list)) { + extendCapacity(list); // 扩容 } - l->nums[size(l)] = num; - l->size++; + list->nums[size(list)] = num; + list->size++; } /* 中间插入元素 */ -void insert(MyList* l, int index, int num) { - assert(index < size(l)); - for (int i=size(l); i>index; --i) { - l->nums[i] = l->nums[i-1]; +void insert(MyList *list, int index, int num) { + assert(index < size(list)); + for (int i = size(list); i > index; --i) { + list->nums[i] = list->nums[i-1]; } - l->nums[index] = num; - l->size++; + list->nums[index] = num; + list->size++; } /* 删除元素 */ // 由于引入了 stdio.h ,此处无法使用 remove 关键词 // 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888 -int removeNum(MyList* l, int index) { - assert(index < size(l)); - int num = l->nums[index]; - for (int i=index; inums[i] = l->nums[i+1]; +int removeNum(MyList *list, int index) { + assert(index < size(list)); + int num = list->nums[index]; + for (int i = index; i < size(list) - 1; i++) { + list->nums[i] = list->nums[i+1]; } - l->size--; + list->size--; return num; } /* 列表扩容 */ -void extendCapacity(MyList* l) { +void extendCapacity(MyList *list) { // 先分配空间 - size_t newCapacity = capacity(l) * l->extendRatio; + int newCapacity = capacity(list) * list->extendRatio; int *extend = (int *)malloc(sizeof(int) * newCapacity); - int *temp = l->nums; + int *temp = list->nums; // 拷贝旧数据到新数据 - for(size_t i=0; inums[i]; + for(int i = 0; i < size(list); i++) + extend[i] = list->nums[i]; // 释放旧数据 free(temp); // 更新新数据 - l->nums = extend; - l->capacity = newCapacity; + list->nums = extend; + list->capacity = newCapacity; } /* 将列表转换为 Array 用于打印 */ -int* toArray(MyList* l) { - return l->nums; +int* toArray(MyList *list) { + return list->nums; } int main() { From 6f9d7ef64f6dd33eed389e4d6cc4173ca6f2e7ad Mon Sep 17 00:00:00 2001 From: Gonglja Date: Wed, 18 Jan 2023 08:15:27 +0800 Subject: [PATCH 17/17] feat(codes/c): Fix the problem that mylist.c memory is not released --- codes/c/chapter_array_and_linkedlist/my_list.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index 39a1aea58..879d74333 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -27,6 +27,12 @@ void newMyList(MyList *list) { list->extendRatio = 2; } +/* 析构函数 */ +void delMyList(MyList *list) { + list->size = 0; + free(list->nums); +} + /* 获取列表长度 */ int size(MyList *list) { return list->size; @@ -147,4 +153,7 @@ int main() { printf("扩容后的列表 list = "); printArray(toArray(&list), size(&list)); printf("容量 = %d ,长度 = %d\r\n", capacity(&list), size(&list)); + + /* 析构函数,释放分配内存 */ + delMyList(&list); } \ No newline at end of file