diff --git a/.gitignore b/.gitignore index 1b5addf701a966b05198ee220b0021309deac10e..5711453935ddf77bf67f0d9b176c4aa3fad2da2c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.layout *.depend *.exe -build \ No newline at end of file +build +.vscode \ No newline at end of file diff --git a/yonglin_zhang/C/CMakeLists.txt b/yonglin_zhang/C/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..61b9e3c3d3f1a18957bd1f31f58ffc4abb122bc1 --- /dev/null +++ b/yonglin_zhang/C/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(dep) +add_subdirectory(link_list) \ No newline at end of file diff --git a/yonglin_zhang/C/dep/CMakeLists.txt b/yonglin_zhang/C/dep/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/yonglin_zhang/C/dep/alg_errno.h b/yonglin_zhang/C/dep/alg_errno.h new file mode 100644 index 0000000000000000000000000000000000000000..2d51eba4e60c226fe65dd733c7551a92c0b476fb --- /dev/null +++ b/yonglin_zhang/C/dep/alg_errno.h @@ -0,0 +1,28 @@ +/** + * \file alg_errno.h + * + * \brief 错误码表 + */ + +#ifndef ALG_ERRNO_H +#define ALG_ERRNO_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define ALG_OK 0x0000 /** < 操作成功 */ +#define ALG_ERR_BAD_INPUT_DATA 0x0001 /** < 输入数据不合法 */ +#define ALG_ERR_ALLOC_FAILED 0x0002 /** < 内存申请失败 */ +#define ALG_ERR_BUFFER_TOO_SMALL 0x0003 /** < buffer太小 */ +#define ALG_ERR_ALREADY_INIT 0x0004 /** < 重复初始化 */ +#define ALG_ERR_NOT_INIT 0x0005 /** < 没有初始化 */ +#define ALG_ERR_POS_NOT_IN_RANGE 0x0006 /** < 链表中没有该位置 */ +#define ALG_ERR_EMPTY_LIST 0x0007 /** < 链表为空 */ +#define ALG_ERR_NO_THIS_POINT 0x0008 /** < 没有包含该数据的节点 */ + +#ifdef __cplusplus +} +#endif + +#endif /* ALG_ERRNO_H */ diff --git a/yonglin_zhang/C/link_list/CMakeLists.txt b/yonglin_zhang/C/link_list/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..3b583cc47b9bfe83de3b6716157a51a2340496df --- /dev/null +++ b/yonglin_zhang/C/link_list/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.0) + +project(link_likt C) + +add_subdirectory(src) \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/include/link_list.h b/yonglin_zhang/C/link_list/include/link_list.h new file mode 100644 index 0000000000000000000000000000000000000000..9f3c85f97980967f18f95904a2531190b2b7812a --- /dev/null +++ b/yonglin_zhang/C/link_list/include/link_list.h @@ -0,0 +1,245 @@ +/** + * \file link_list.h + * + * \brief 链表接口头文件 + */ + +#ifndef LINK_LIST_H +#define LINK_LIST_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define DATA_TYPE int + +/** + * \brief 链表结构体。 + */ +typedef struct _node{ + DATA_TYPE data; + struct _node *next; +}node; + +/** 初始化操作 */ + +/** + * \brief 链表的初始化。 + * + * \note 此接口应当在链表使用前调用,且仅调用一次,并且传入的参数应当指向NULL,否则默认为有数据的节点,不进行初始化。 + * + * \param head[in/out] 链表的头节点的地址。 + * + * \return #ALG_OK 操作成功。 + * \return #ALG_ERR_BAD_INPUT_DATA 输入数据非法。 + * \return #ALG_ERR_ALLOC_FAILED 内存申请失败。 + * \return #ALG_ERR_ALREADY_INIT 重复初始化。 + */ +int init(node **head); + +/** 插入相关操作 */ + +/** + * \brief 添加一个节点(默认为头插法)。 + * + * \note 为了避免浅拷贝的情况出现,此处采用深拷贝模式。 + * + * \param head[in/out] 链表的头节点。 + * \param data[in] 待添加节点。 + * + * \return #ALG_OK 操作成功。 + */ +int add(node *head, node data); + +/** + * \brief 头插法添加一个节点。 + * + * + * \param head[in/out] 链表的头节点。 + * \param data[in] 待添加节点。 + * + * \return #ALG_OK 操作成功。 + */ +int add_head(node *head, node data); + +/** + * \brief 尾插法添加一个节点。 + * + * + * \param head[in/out] 链表的头节点。 + * \param data[in] 待添加节点。 + * + * \return #ALG_OK 操作成功。 + */ +int add_end(node *head, node data); + +/** + * \brief 在位置X处添加一个节点。 + * + * \note 头节点默认位置为0 + * + * \param head[in/out] 链表的头节点 + * + * \return #ALG_OK 操作成功。 + */ +int add_pos_x(node *head, node data, int pos); + +/** 删除操作 */ + +/** + * \brief 删除数据为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int delete_data_x(node *head, node data); + +/** + * \brief 删除位置为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int delete_pos_x(node *head, int pos); + +/** + * \brief 清空从位置X之后的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int delete_after_pos_x(node *head, int pos); + +/** + * \brief 清空从位置X之前的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int delete_before_pos_x(node *head, int pos); + +/** + * \brief 清空整个链表。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int clear(node *head); + + +/** 排序操作 */ + +/** + * \brief 反转链表。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int reverse(node *head); + +/** + * \brief 升序排序。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int ascend(); + +/** + * \brief 降序排序。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int descend(); + + +/** 查询操作 */ + + +/** + * \brief 打印整个链表。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int show_all(); + +/** + * \brief 查询数值为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int search_data_x(); + +/** + * \brief 查询位置为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int search_pos_x(); + +/** + * \brief 返回整个链表的长度。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int get_length(); + + +/** 更新操作 */ + + +/** + * \brief 更新数据为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int update_data_x(); + +/** + * \brief 更新位置为X的节点。 + * + * + * \param + * + * \return #ALG_OK 操作成功。 + */ +int update_pos_x(); + + +#ifdef __cplusplus +} +#endif + +#endif /* LINK_LIST_H */ \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/src/CMakeLists.txt b/yonglin_zhang/C/link_list/src/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..018b8dc1c04cade6c15057588b70f3f6d8f7cfbf --- /dev/null +++ b/yonglin_zhang/C/link_list/src/CMakeLists.txt @@ -0,0 +1,16 @@ +set(LIB_SOURCES + delete.c + init_and_insert.c + search.c + sort.c + update.c +) + +set(INCLUDE_DIRS + "${CMAKE_CURRENT_SOURCE_DIR}/../include" + "${CMAKE_CURRENT_SOURCE_DIR}/../../dep" +) + +include_directories(${INCLUDE_DIRS}) + +add_library(link_list SHARED ${LIB_SOURCES}) diff --git a/yonglin_zhang/C/link_list/src/delete.c b/yonglin_zhang/C/link_list/src/delete.c new file mode 100644 index 0000000000000000000000000000000000000000..bd965953d09d62afe90cacb5669f8d89251801c2 --- /dev/null +++ b/yonglin_zhang/C/link_list/src/delete.c @@ -0,0 +1,138 @@ +#include +#include +#include "link_list.h" +#include "alg_errno.h" + +int delete_data_x(node *head, node data) +{ + node *p = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head; + + while(p->next != NULL) { + if(p->next->data == data.data) { + node *t = p->next; + p->next = t->next; + return ALG_OK; + } + p = p->next; + } + + return ALG_ERR_NO_THIS_POINT; +} + +int delete_pos_x(node *head, int pos) +{ + int counter = 0; + node *p = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head; + while(p->next != NULL) { + counter++; + if(counter == pos) { + node *t = p->next; + p->next = t->next; + free(t); + return ALG_OK; + } + p = p->next; + } + + return ALG_ERR_POS_NOT_IN_RANGE; +} + +int delete_after_pos_x(node *head, int pos) +{ + node *p = NULL; + int counter = 0; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head; + while(p != NULL) { + counter++; + if(counter == pos) { + node *t = p->next; + while(t != NULL) { + node *temp = t; + t = t->next; + free(t); + } + p->next = NULL; + return ALG_OK; + } + p = p->next; + } + + return ALG_ERR_POS_NOT_IN_RANGE; +} + +int delete_before_pos_x(node *head, int pos) +{ + node *p = NULL; + int counter = 0; + int flag = 0; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head; + while(p != NULL) { + counter++; + if(counter == pos) { + flag = 1; + break; + } + } + + if(flag) { + p = head; + counter = 0; + while(head->next != NULL) { + counter++; + if(counter == pos) { + break; + } + else { + p = head->next; + head = head->next; + free(p); + } + } + return ALG_OK; + } + else { + return ALG_ERR_POS_NOT_IN_RANGE; + } +} + +int clear(node *head) +{ + node *p; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head->next; + head->next = NULL; + + while(p != NULL) { + node *t = p; + p = p->next; + free(t); + } + + return ALG_OK; +} \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/src/init_and_insert.c b/yonglin_zhang/C/link_list/src/init_and_insert.c new file mode 100644 index 0000000000000000000000000000000000000000..b7dfc1bd1bcd4b95c28093c8ad2d2b8e9d4cb2fc --- /dev/null +++ b/yonglin_zhang/C/link_list/src/init_and_insert.c @@ -0,0 +1,100 @@ +#include +#include +#include "link_list.h" +#include "alg_errno.h" + +int init(node **head) +{ + //传入空指针 + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + //传入的指针指向不为空则视为已经初始化过 + if((*head) != NULL) { + return ALG_ERR_ALREADY_INIT; + } + + //申请内存并初始化 + (*head) = (node*)malloc(sizeof(node)); + (*head)->next = NULL; + (*head)->data = -1; + + return ALG_OK; +} + +/* 采用深拷贝的方式进行,避免内存问题 */ +int add(node *head, node data) +{ + node *t = (node*)malloc(sizeof(node)); + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + t->next = head->next; + t->data = data.data; + + head->next = t; + + return ALG_OK; +} + +/* 头插法与add函数相同 */ +int add_head(node *head, node data) +{ + return add(head, data); +} + +int add_end(node *head, node data) +{ + node *t = NULL; + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + t = head; + while(t->next != NULL) { + t = t->next; + } + + node *temp = (node *)malloc(sizeof(node)); + temp->data = data.data; + temp->next = NULL; + head->next = temp; + return ALG_OK; +} + +/* 在链表X处插入数据 */ +int add_pos_x(node *head, node data, int pos) +{ + node *p; + int counter = 0; + + if(head == NULL || pos == 0) { //头节点位置为0 + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head; + while(p->next != NULL) { + counter++; + if(counter == pos) { + node *t = (node *)malloc(sizeof(node)); + t->next = p->next; + t->data = data.data; + p->next = t; + return ALG_OK; + } + p = p->next; + } + + if(p->next == NULL && (counter + 1) == pos) { + node *t = (node *)malloc(sizeof(node)); + t->next = p->next; + t->data = data.data; + p->next = t; + return ALG_OK; + } + + return ALG_ERR_POS_NOT_IN_RANGE; +} \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/src/search.c b/yonglin_zhang/C/link_list/src/search.c new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/yonglin_zhang/C/link_list/src/sort.c b/yonglin_zhang/C/link_list/src/sort.c new file mode 100644 index 0000000000000000000000000000000000000000..42c3f77ba21c350477a51909deed335344187ac7 --- /dev/null +++ b/yonglin_zhang/C/link_list/src/sort.c @@ -0,0 +1,5 @@ +#include +#include +#include "link_list.h" +#include "alg_errno.h" + diff --git a/yonglin_zhang/C/link_list/src/update.c b/yonglin_zhang/C/link_list/src/update.c new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/yonglin_zhang/CMakeLists.txt b/yonglin_zhang/CMakeLists.txt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e0c5cad5b58745c36951103617d1c74f7f8adc9c 100644 --- a/yonglin_zhang/CMakeLists.txt +++ b/yonglin_zhang/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(C) \ No newline at end of file