From 009191f712ab095eba323c6091d05fefb5bb3f61 Mon Sep 17 00:00:00 2001 From: Gonglja Date: Thu, 12 Jan 2023 15:44:02 +0800 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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 }