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/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 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 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..52deb1e17 --- /dev/null +++ b/codes/c/chapter_array_and_linkedlist/list.c @@ -0,0 +1,206 @@ +/** + * 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_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..714ed1fb2 --- /dev/null +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -0,0 +1,92 @@ +/** + * File: linkedlist_stack.c + * Created Time: 2022-01-12 + * Author: Zero (glj0@outlook.com) + */ + +#include "../include/include.h" + +/* 基于链表实现的栈 */ +struct LinkedListStack { + ListNode* stackTop; // 将头结点作为栈顶 + size_t stkSize; // 栈的长度 +}; + +typedef struct LinkedListStack LinkedListStack; + +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; +} +/* 访问栈顶元素 */ +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/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 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" 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 }