From 940087e8204c9584a9a8b2dfd25b7ea03fa7ed76 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Sun, 6 Jan 2019 18:44:11 +0800 Subject: [PATCH] Added and Fixed. --- include/list/list.h | 8 +++++ include/list/list_expand.h | 48 +++++++++++++------------- include/list/list_expand_1.h | 6 ++-- src/list/list_expand_calculate.c | 6 ++-- src/list/list_expand_callback.c | 12 +++++-- src/list/list_expand_node.c | 58 +++++++++++++++++++++++++++++--- src/list/list_expand_update.c | 44 ++++++++++++++++++++++-- src/list/list_find.c | 28 ++++++++++++++- src/list/list_info.c | 15 ++++++++- src/list/list_init.c | 39 +++++++++++++++++++-- src/list/list_sort.c | 6 ++-- 11 files changed, 224 insertions(+), 46 deletions(-) diff --git a/include/list/list.h b/include/list/list.h index 534f0c5..e5919de 100644 --- a/include/list/list.h +++ b/include/list/list.h @@ -11,12 +11,20 @@ /* *初始化链表,并返回指向新链表的指针,参数if_sid指示是否为新链表分配ID号 *返回: 如果成功返回指向该节点的指针,如果失败则返回NULL.*/ +#ifdef id_enable extern List *initList(_Bool if_sid); +#else +extern List *initList(void); +#endif /* *初始化节点,并返回指向新链表的指针,参数if_sid指示是否为新节点分配ID号 *返回: 如果成功返回指向该节点的指针,如果失败则返回NULL.*/ +#ifdef id_enable extern Node *initNode(_Bool if_sid); +#else +extern Node *initNode(void); +#endif /*库中内部调用函数,旨在为新节点的储存值获取内存,并将相应的储存值储存在获取到的内存中,并指明储存值的类型.*/ extern int initMallocValueForNode(Node *,unsigned int,const void *); diff --git a/include/list/list_expand.h b/include/list/list_expand.h index 58926e5..5469a1e 100644 --- a/include/list/list_expand.h +++ b/include/list/list_expand.h @@ -1,4 +1,4 @@ -#ifndef list_expand_h +#ifndef list_expand_h #define list_expand_h #include @@ -12,7 +12,7 @@ /* *为新节点获取内存空间, 并使用整型值初始化新节点 *参数if_sid指示函数是否为节点获取ID - *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ + *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ extern Node *nodeWithInt(int, _Bool if_sid); /* @@ -30,23 +30,23 @@ extern Node *nodeWithULLInt(unsigned long long, _Bool if_sid); /* *为新节点获取内存空间, 并使用浮点值初始化新节点 *参数if_sid指示函数是否为节点获取ID - *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ + *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ extern Node *nodeWithDouble(double, _Bool if_sid); /* *为新节点获取内存空间, 并使用字符串值初始化新节点 *参数if_sid指示函数是否为节点获取ID - *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ + *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ extern Node *nodeWithString(const char *, _Bool if_sid); /* *为新节点获取内存空间, 并使用指针值初始化新节点 *参数if_sid指示函数是否为节点获取ID - *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ + *返回: 若成功则返回指向新节点的指针,若失败函数返回NULL.*/ extern Node *nodeWithPointer(const void *, _Bool if_sid); - + /************************************************ *嵌套节点的快速初始化: 为嵌套节点节点分配内存空间, * 并使用输入值对其进行初始化 @@ -55,33 +55,33 @@ extern Node *nodeWithPointer(const void *, _Bool if_sid); /* *为新嵌套节点获取内存空间 - *返回: 若成功指向新嵌套节点的指针,若失败函数返回NULL.*/ + *返回: 若成功指向新嵌套节点的指针,若失败函数返回NULL.*/ extern Node *nodeWithComplex(void); /* *用输入的值初始化嵌套节点 *参数: type指明所输入值的类型,value为指向所输入值的内存空间的指针 - *返回: 若成功则返回0,若失败函数返回-1.*/ + *返回: 若成功则返回0,若失败函数返回-1.*/ extern int addValueForComplex(Node *, int type, void *value); /* *用输入的整型值初始化嵌套节点 - *返回: 若成功则返回0,若失败函数返回-1.*/ + *返回: 若成功则返回0,若失败函数返回-1.*/ extern int addIntForComplex(Node *, int); /* *用输入的浮点值初始化嵌套节点 - *返回: 若成功则返回0,若失败函数返回-1.*/ + *返回: 若成功则返回0,若失败函数返回-1.*/ extern int addDoubleForComplex(Node *, double); /* *用输入的字符串初始化嵌套节点 - *返回: 若成功则返回0,若失败函数返回-1.*/ + *返回: 若成功则返回0,若失败函数返回-1.*/ extern int addStringForComplex(Node *, char *); /* *用输入的指针值初始化嵌套节点 - *返回: 若成功则返回0,若失败函数返回-1.*/ + *返回: 若成功则返回0,若失败函数返回-1.*/ extern int addPointerForComplex(Node *, void *); @@ -99,7 +99,7 @@ extern int updateValueWithIntForNode(Node *,int); /* *用输入的无符号长整型值更新节点中的值 *返回: 若成功则返回0,若失败函数返回-1.*/ -extern int updateValueWithULLIntForNode(Node *, unsigned long long); +extern int updateValueWithULLIntForNode(Node *, uint64_t); /* *用输入的浮点值更新节点中的值 @@ -114,7 +114,7 @@ extern int updateValueWithStringForNode(Node *,char *); /* *用输入的指针值更新节点中的值 *返回: 若成功则返回0,若失败函数返回-1.*/ -extern int updateValueWithPointerForNode(Node *,void *); +extern int updateValueWithPointerForNode(Node *,void *); @@ -125,23 +125,23 @@ extern int updateValueWithPointerForNode(Node *,void *); /* *通过输入的整型值,查找链表中多个含有该值的节点 - *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ + *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ List *mply_findByIntForNode(List*, int); /* *通过输入的浮点值,查找链表中多个含有该值的节点 - *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ + *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ List *mply_findByDoubleForNode(List*, double); /* *通过输入的字符串值,查找链表中多个含有该值的节点 - *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ + *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ List *mply_findByStringForNode(List*, char *); /* *通过输入的指针值,查找链表中多个含有该值的节点 - *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ -List *mply_findByPointerForNode(List*, void *); + *返回: 若成功则返回一个储存由这些指向这些节点指针组成的链表,若失败函数返回NULL.*/ +List *mply_findByPointerForNode(List*, void *); @@ -157,7 +157,7 @@ List *mply_findByPointerForNode(List*, void *); *参数: p_func为一个函数指针, 指向的回调函数的职能在于接受每个节点的类型(type)/值指针(value)/传入参数链表(args), 然后进行相关操作,并返回一个链表,其中包含返回状态,若有,也可以包含想要传递给母函数的值 expand_resources为一个链表,储存母函数想要传递给回调函数的值 - *返回: 如果回调函数有需要返回给母函数的值,则返回包含这些值的链表,否则返回NULL.*/ + *返回: 如果回调函数有需要返回给母函数的值,则返回包含这些值的链表,否则返回NULL.*/ extern List *listThrough(List *p_list, List *(*p_func)(unsigned int type, void *value, List *args), List *expand_resources); /* @@ -186,7 +186,7 @@ unsigned long long getInfoForListThrough(List *expand_resources, int type); /* *以字节为单位计算链表的大小 */ -unsigned long long calListMemory(List *); +uint64_t calListMemory(List *); @@ -197,7 +197,7 @@ unsigned long long calListMemory(List *); /* *返回链表长度*/ -extern unsigned long long len(List *p_list); +extern uint64_t len(List *p_list); /* *查询链表是否为空 @@ -283,5 +283,5 @@ extern Node *findByStringForNode(List *, char *); *通过指针值查找储存该值的单个节点. *返回: 如果成功返回指向该节点的指针,如果失败则返回NULL.*/ extern Node *findByPointerForNode(List *, void *); - -#endif + +#endif diff --git a/include/list/list_expand_1.h b/include/list/list_expand_1.h index 9a4193c..a8e4028 100644 --- a/include/list/list_expand_1.h +++ b/include/list/list_expand_1.h @@ -4,11 +4,11 @@ #include /************************************************ - *节点快速初始化相关宏 + *节点快速初始化相关宏,提供函数名较为简单的调用方式 ************************************************/ /**** - *不带ID相关 + *不带ID模块相关宏 */ /*直接获取一个以整型值初始化的节点不带ID的缩略*/ @@ -30,7 +30,7 @@ #define lnp(x) nodeWithPointer(x,0) /**** - *带ID相关 + *带ID模块的相关宏 */ /*直接获取一个以整型值初始化的带ID节点的缩略*/ diff --git a/src/list/list_expand_calculate.c b/src/list/list_expand_calculate.c index b685233..8f93edf 100644 --- a/src/list/list_expand_calculate.c +++ b/src/list/list_expand_calculate.c @@ -7,10 +7,10 @@ @param p_list 指向目标链表的指针 @return 以字节为单位返回链表及链表内包含的节点所占的内存空间 */ -unsigned long long calListMemory(List * p_list){ +uint64_t calListMemory(List * p_list){ Node *p_node = p_list->head; - unsigned long long nodes_size = 0LL; - unsigned long long list_size = sizeof(p_list); + uint64_t nodes_size = 0LL; + uint64_t list_size = sizeof(p_list); while(p_node != NULL){ nodes_size += sizeof(p_node); p_node = p_node->next; diff --git a/src/list/list_expand_callback.c b/src/list/list_expand_callback.c index c4a4341..6dca02e 100644 --- a/src/list/list_expand_callback.c +++ b/src/list/list_expand_callback.c @@ -3,10 +3,18 @@ #include #include -List *listThrough(List *p_list, List *(*p_func)(unsigned int, void *, List *), List *expand_resources) { + +/** + 顺序遍历链表的操作函数,不对用户开放。回调函数返回-1退出遍历,返回1继续向后遍历,返回其他值则暂停向下遍历。 + 回调函数接受一个整数、一个指向节点内容的指针、一个而外的参数链表,且回调函数需要返回一个链表,链表的第一个值作为函数的实际返回值。 + @param p_list 指向目标链表的指针 + @param p_func 指向回调函数的指针 + @return 返回参数链表 + */ +List *listThrough(List *p_list, List *(*p_func)(uint32_t, void *, List *), List *expand_resources) { Node *p_node = p_list->head; List *m_rtnlst = NULL; - unsigned long long index = 0; + uint64_t index = 0; insertInTail(expand_resources, nodeWithULLInt(index, 0)); insertInTail(expand_resources, nodeWithULLInt(p_list->length, 0)); while (p_node != NULL) { diff --git a/src/list/list_expand_node.c b/src/list/list_expand_node.c index 912bef2..b30777e 100644 --- a/src/list/list_expand_node.c +++ b/src/list/list_expand_node.c @@ -2,6 +2,14 @@ #include #include + +/** + 直接使用一个整型值初始化一个新的节点 + + @param m_int 整型值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ Node *nodeWithInt(int m_int, _Bool if_sid) { Node *p_node; int *p_int = (int *)malloc(sizeof(int)); @@ -14,21 +22,37 @@ Node *nodeWithInt(int m_int, _Bool if_sid) { return p_node; } -Node *nodeWithUInt(unsigned int m_uint, _Bool if_sid){ + +/** + 直接使用一个无符号整型值初始化一个新的节点 + + @param m_uint 无符号整型值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ +Node *nodeWithUInt(uint32_t m_uint, _Bool if_sid){ Node *p_node; - unsigned int *pu_int = (unsigned int *)malloc(sizeof(unsigned int)); + unsigned int *pu_int = (uint32_t *)malloc(sizeof(uint32_t)); if(pu_int == NULL){ return NULL; } *pu_int = m_uint; p_node = initNode(if_sid); - initMallocValueForNode(p_node, INT, (void *)pu_int); + initMallocValueForNode(p_node, UINT, (void *)pu_int); return p_node; } -Node *nodeWithULLInt(unsigned long long m_ullint, _Bool if_sid) { + +/** + 直接使用一个无符号长整型值(占用8个字节)初始化一个新的节点 + + @param m_ullint 无符号长整型值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ +Node *nodeWithULLInt(uint64_t m_ullint, _Bool if_sid) { Node *p_node; - unsigned long long *p_ullint = (unsigned long long *)malloc(sizeof(unsigned long long)); + unsigned long long *p_ullint = (uint64_t *)malloc(sizeof(uint64_t)); if(p_ullint == NULL){ return NULL; } @@ -38,6 +62,14 @@ Node *nodeWithULLInt(unsigned long long m_ullint, _Bool if_sid) { return p_node; } + +/** + 直接使用一个双精度浮点值初始化一个新的节点 + + @param m_double 双精度浮点值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ Node *nodeWithDouble(double m_double, _Bool if_sid) { Node *p_node; double *p_double = (double *)malloc(sizeof(double)); @@ -50,6 +82,14 @@ Node *nodeWithDouble(double m_double, _Bool if_sid) { return p_node; } + +/** + 直接使用一个字符串值初始化一个新的节点 + + @param m_string 字符串值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ Node *nodeWithString(const char *m_string, _Bool if_sid) { Node *p_node; char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); @@ -62,6 +102,14 @@ Node *nodeWithString(const char *m_string, _Bool if_sid) { return p_node; } + +/** + 直接使用一个指针值初始化一个新的节点 + + @param m_pointer 指针值 + @param if_sid 新的节点有无id + @return 返回指向新节点的指针 + */ Node *nodeWithPointer(const void *m_pointer, _Bool if_sid) { Node *p_node = initNode(if_sid); initMallocValueForNode(p_node, POINTER, m_pointer); diff --git a/src/list/list_expand_update.c b/src/list/list_expand_update.c index a89f2b3..9a2772a 100644 --- a/src/list/list_expand_update.c +++ b/src/list/list_expand_update.c @@ -2,6 +2,14 @@ #include #include + +/** + 用一个整型值更新一个现有节点的值 + + @param p_node 指向目标节点的指针 + @param value 整型值 + @return 操作成功则返回0 + */ int updateValueWithIntForNode(Node *p_node,int value){ int *p_value = (int *)malloc(sizeof(int)); if(p_value == NULL){ @@ -13,8 +21,16 @@ int updateValueWithIntForNode(Node *p_node,int value){ return 0; } -int updateValueWithULLIntForNode(Node *p_node, unsigned long long value){ - unsigned long long *p_value = (unsigned long long *)malloc(sizeof(unsigned long long)); + +/** + 用一个无符号长整型(8个字节)更新一个现有节点的值 + + @param p_node 指向目标节点的指针 + @param value 无符号长整型值 + @return 操作成功则返回0 + */ +int updateValueWithULLIntForNode(Node *p_node, uint64_t value){ + uint64_t *p_value = (uint64_t *)malloc(sizeof(uint64_t)); if(p_value == NULL){ return -1; } @@ -24,6 +40,14 @@ int updateValueWithULLIntForNode(Node *p_node, unsigned long long value){ return 0; } + +/** + 用一个双精度浮点值更新一个现有节点的值 + + @param p_node 指向目标节点的指针 + @param value 双精度浮点值 + @return 操作成功则返回0 + */ int updateValueWithDoubleForNode(Node *p_node, double value){ double *p_value = (double *)malloc(sizeof(double)); if(p_value == NULL){ @@ -35,6 +59,14 @@ int updateValueWithDoubleForNode(Node *p_node, double value){ return 0; } + +/** + 用一个字符串值更新一个现有节点的值 + + @param p_node 指向目标节点的指针 + @param string 字符串值 + @return 操作成功则返回0 + */ int updateValueWithStringForNode(Node *p_node, char *string){ char *p_value = (char *)malloc(sizeof(strlen(string)) + 1); if(p_value == NULL){ @@ -46,6 +78,14 @@ int updateValueWithStringForNode(Node *p_node, char *string){ return 0; } + +/** + 用一个指针值更新一个现有节点的值 + + @param p_node 指向目标节点的指针 + @param pointer 指针值 + @return 操作成功则返回0 + */ int updateValueWithPointerForNode(Node *p_node, void *pointer){ free(p_node->value); p_node->value = pointer; diff --git a/src/list/list_find.c b/src/list/list_find.c index f07911c..c159265 100644 --- a/src/list/list_find.c +++ b/src/list/list_find.c @@ -1,7 +1,16 @@ #include #include +//如果ID模块可能启用则编译以下代码 #ifdef id_enable + +/** + 通过ID查找链表中符合条件的第一个节点 + + @param p_list 指向目标链表的指针 + @param s_id ID值 + @return 成功返回指向符合条件的节点的指针,不成功则返回NULL + */ Node *findByIdForNode(List *p_list, SID * s_id) { Node *ph_node = p_list->head; Node *pt_node = p_list->tail; @@ -29,7 +38,16 @@ Node *findByIdForNode(List *p_list, SID * s_id) { } #endif -Node *findByValue(List *p_list, unsigned int type, const void *value) { + +/** + 通过值来查找链表中符合条件的第一个节点 + + @param p_list 指向目标链表的指针 + @param type 值的类型 + @param value 值 + @return 成功返回指向符合条件的节点的指针,不成功则返回NULL + */ +Node *findByValue(List *p_list, uint type, const void *value) { Node *p_node = p_list->head; while (p_node != NULL) { if (p_node->type != type) { @@ -64,6 +82,14 @@ Node *findByValue(List *p_list, unsigned int type, const void *value) { return NULL; } +/** + 通过值来查找链表中符合条件的所有节点 + + @param p_list 指向目标链表的指针 + @param type 值的类型 + @param value 值 + @return 返回含有所有符合条件的节点的重组链表 + */ List *mply_findByValue(List *p_list, unsigned int type, const void *value) { List *f_list = initList(0); Node *p_node = p_list->head; diff --git a/src/list/list_info.c b/src/list/list_info.c index c1ac1dd..2404b22 100644 --- a/src/list/list_info.c +++ b/src/list/list_info.c @@ -1,10 +1,23 @@ #include #include -unsigned long long len(List *p_list) { + +/** + 返回链表的长度 + + @param p_list 指向目标链表的指针 + @return 返回目标链表的长度 + */ +uint64_t len(List *p_list) { return p_list->length; } +/** + 检查链表是否为空 + + @param p_list 指向目标链表的指针 + @return 目标链表为空返回1,不为空返回0 + */ int isListEmpty(List *p_list) { if (p_list->head == NULL || p_list->tail == NULL)// If its head or tail is NULL,it would be thought as empty. return 1; // But we should ensure that both of them are NULL when we diff --git a/src/list/list_init.c b/src/list/list_init.c index 39e9413..cf7ecef 100644 --- a/src/list/list_init.c +++ b/src/list/list_init.c @@ -1,12 +1,25 @@ #include #include -Node *initNode(_Bool if_sid) { + +/** + 创建一个新的节点,并为其分配内存空间 + + @param if_sid 新的节点是否带ID + @return 返回指向新的节点的内存地址指针 + */ +#ifdef id_enable +Node *initNode(_Bool if_sid) +#else +Node *initNode(void) +#endif +{ Node *p_node = (Node *)malloc(sizeof(Node)); if(p_node == NULL){ return NULL; } Node *prec_node = NULL; +//如果ID模块可能启用则编译以下代码 #ifdef id_enable if (if_sid)p_node->s_id = getS_id(LIST_NODE, 1); else p_node->s_id = NULL; @@ -15,6 +28,7 @@ Node *initNode(_Bool if_sid) { p_node->last = NULL; p_node->type = VOID; p_node->f_number = 0; +// 链表内存安全模式相关操作 if (if_safeModeForNode) { if_safeModeForNode = 0; prec_node = initNode(0); @@ -25,12 +39,24 @@ Node *initNode(_Bool if_sid) { return p_node; } -List *initList(_Bool if_sid) { +/** + 创建一个新的链表,并为其分配内存空间 + + @param if_sid 新的链表是否带ID + @return 返回指向新的链表的内存地址指针 + */ +#ifdef id_enable +List *initList(_Bool if_sid) +#else +List *initList(void) +#endif +{ Node *p_node = NULL; List *p_list = (List *)malloc(sizeof(List)); if(p_list == NULL){ return NULL; } +//如果ID模块可能启用则编译以下代码 #ifdef id_enable if(if_sid) p_list->s_id = getS_id(LIST, 1); else p_list->s_id = NULL; @@ -39,6 +65,7 @@ List *initList(_Bool if_sid) { p_list->tail = NULL; p_list->length = 0; p_list->p_lq = NULL; +// 如果简单链表模式启用则编译以下代码 #ifdef list_simple_h p_list->s_head = NULL; #endif @@ -52,6 +79,14 @@ List *initList(_Bool if_sid) { return p_list; } +/** + 为新的节点设定初始值 + + @param p_node 指向目标节点的指针 + @param type 值得类型 + @param p_value 指向目标值的内存空间的指针 + @return 执行成功则返回0 + */ inline int initMallocValueForNode(Node *p_node, unsigned int type, const void *p_value) { p_node->type = type; p_node->value = (void *)p_value; diff --git a/src/list/list_sort.c b/src/list/list_sort.c index 0adea99..7b82d60 100644 --- a/src/list/list_sort.c +++ b/src/list/list_sort.c @@ -9,10 +9,10 @@ *内部函数: 通过节点中的值查找相关多个节点. *参数: type指明相关值的类型;value为指向储存相关值的内存的指针. *返回: 如果成功返回0,如果失败则返回-1.*/ -static int sortList(List *p_list, unsigned long long begin, unsigned long long end, int(*func)(Node *f_node, Node *s_node)); +static int sortList(List *p_list, uint64_t begin, uint64_t end, int(*func)(Node *f_node, Node *s_node)); /** - 链表排序函数,该函数不直接对用户开放 + 链表排序函数,该函数不直接对用户开放。采用快速排序的算法。 @param p_list 指向需要操作的链表的指针 @param begin 开始的节点的序号 @@ -20,7 +20,7 @@ static int sortList(List *p_list, unsigned long long begin, unsigned long long e @param func 指向判断条件的函数的函数指针,接受两个指向相关节点的指针,比较他们的大小并返回正负值。 @return 成功进行操作则返回0 */ -static int sortList(List *p_list, unsigned long long begin, unsigned long long end, int(*func)(Node *f_node, Node *s_node)){ +static int sortList(List *p_list, uint64_t begin, uint64_t end, int(*func)(Node *f_node, Node *s_node)){ unsigned long long target_index = begin; register Node *t_node = findByIndexForNode(p_list, target_index); register Node *i_node = NULL, *j_node = NULL;