Merge branch 'dev'

pull/289/head
Gonglja 2 years ago
commit 74218bf7f0

@ -10,3 +10,4 @@ add_subdirectory(chapter_computational_complexity)
add_subdirectory(chapter_array_and_linkedlist) add_subdirectory(chapter_array_and_linkedlist)
add_subdirectory(chapter_sorting) add_subdirectory(chapter_sorting)
add_subdirectory(chapter_tree) add_subdirectory(chapter_tree)
add_subdirectory(chapter_stack_and_queue)

@ -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}

@ -1,2 +1,3 @@
add_executable(array array.c) add_executable(array array.c)
add_executable(linked_list linked_list.c) add_executable(linked_list linked_list.c)
add_executable(list 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; i<listSize(l); i++)
newData[i] = l->nums[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 (; i<listSize(l)-1; i++) {
l->nums[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 (; i<l->size-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);
}

@ -0,0 +1,2 @@
add_executable(array_stack array_stack.c)
add_executable(linkedlist_stack linkedlist_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;
}

@ -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;
}

@ -6,3 +6,6 @@
#include "../include/include.h" #include "../include/include.h"
int main() {
return 0;
}

@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include <assert.h>
#include "list_node.h" #include "list_node.h"
#include "tree_node.h" #include "tree_node.h"

@ -127,6 +127,14 @@ static void printTree(TreeNode *root) {
printTreeHelper(root, NULL, false); printTreeHelper(root, NULL, false);
} }
/**
* @brief Print a stack
*
* @param head
*/
static void printStack(void *stack) {
}
#ifdef __cplusplus #ifdef __cplusplus
} }

Loading…
Cancel
Save