UP
This commit is contained in:
32
lib/inc/list/array.h
Normal file
32
lib/inc/list/array.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef HW_LIB_ARRAY_H
|
||||
#define HW_LIB_ARRAY_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// 在数组的索引 index 处插入元素 num
|
||||
// nums: 操作数组
|
||||
// size: 数组大小
|
||||
// num: 操作数
|
||||
// index: 操作位置
|
||||
void insert(int *nums, int size, int num, int index);
|
||||
|
||||
// 删除索引 index 处的元素
|
||||
// nums: 操作数组
|
||||
// size: 数组大小
|
||||
// index: 索引位置
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
void removeItem(int *nums, int size, int index);
|
||||
|
||||
// 在数组中查找指定元素
|
||||
// nums: 操作数组
|
||||
// size: 数组大小
|
||||
// target: 查找元素
|
||||
int find(int *nums, int size, int target);
|
||||
|
||||
/* 扩展数组长度 */
|
||||
int *extend(int *nums, int size, int enlarge);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif //HW_LIB_ARRAY_H
|
148
lib/inc/list/list.h
Normal file
148
lib/inc/list/list.h
Normal file
@@ -0,0 +1,148 @@
|
||||
#ifndef HW_LIB_LIST_H
|
||||
#define HW_LIB_LIST_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct node { //节点结构
|
||||
void *data;
|
||||
struct node *next;
|
||||
} node;
|
||||
|
||||
typedef struct { //链表结构
|
||||
struct node *head;
|
||||
struct node *tail;
|
||||
long len;
|
||||
} List;
|
||||
|
||||
/**
|
||||
* @brief 初始化链表
|
||||
* @param list: [输入] 链表指针
|
||||
* @return void
|
||||
* @example list_init(&myList);
|
||||
*/
|
||||
extern void list_init(List *list);
|
||||
|
||||
/**
|
||||
* @brief 检查链表是否为空
|
||||
* @param list: [输入] 链表指针
|
||||
* @return bool 若链表为空返回true,否则返回false
|
||||
* @example if (list_is_empty(&myList)) { // do something }
|
||||
*/
|
||||
extern bool list_is_empty(List *list);
|
||||
|
||||
/**
|
||||
* @brief 在链表尾部插入元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param data: [输入] 待插入的数据指针
|
||||
* @return void
|
||||
* @example list_insert(&myList, newData);
|
||||
*/
|
||||
extern void list_insert(List *list, void *data);
|
||||
|
||||
/**
|
||||
* @brief 在链表头部插入元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param data: [输入] 待插入的数据指针
|
||||
* @return void
|
||||
* @example list_insert_at_head(&myList, newData);
|
||||
*/
|
||||
extern void list_insert_at_head(List *list, void *data);
|
||||
|
||||
/**
|
||||
* @brief 在链表尾部插入元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param data: [输入] 待插入的数据指针
|
||||
* @return void
|
||||
* @example list_insert_at_tail(&myList, newData);
|
||||
*/
|
||||
extern void list_insert_at_tail(List *list, void *data);
|
||||
|
||||
/**
|
||||
* @brief 在指定位置插入元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param data: [输入] 待插入的数据指针
|
||||
* @param idx: [输入] 插入位置索引
|
||||
* @return void
|
||||
* @example list_insert_at_index(&myList, newData, 2);
|
||||
*/
|
||||
extern void list_insert_at_index(List *list, void *data, long idx);
|
||||
|
||||
/**
|
||||
* @brief 删除链表中指定元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param key: [输入] 待删除的数据指针
|
||||
* @param compare: [输入] 比较函数指针
|
||||
* @return void* 返回被删除元素的数据,若未找到返回NULL
|
||||
* @example deletedData = list_delete(&myList, keyData, compareFunc);
|
||||
*/
|
||||
extern void *list_delete(List *list, void *key, int (*compare)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* @brief 在链表中查找指定元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param key: [输入] 待查找的数据指针
|
||||
* @param compare: [输入] 比较函数指针
|
||||
* @return void* 返回找到的元素的数据,若未找到返回NULL
|
||||
* @example foundData = list_search(&myList, keyData, compareFunc);
|
||||
*/
|
||||
extern void *list_search(List *list, void *key, int (*compare)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* @brief 对链表进行排序
|
||||
* @param list: [输入] 链表指针
|
||||
* @param compare: [输入] 比较函数指针
|
||||
* @return void
|
||||
* @example list_sort(&myList, compareFunc);
|
||||
*/
|
||||
extern void list_sort(List *list, int (*compare)(const void *, const void *));
|
||||
|
||||
/**
|
||||
* @brief 遍历链表并对每个元素执行指定操作
|
||||
* @param list: [输入] 链表指针
|
||||
* @param handle: [输入] 处理函数指针
|
||||
* @return void
|
||||
* @example list_traverse(&myList, handleFunc);
|
||||
*/
|
||||
extern void list_traverse(List *list, void (*handle)(void *));
|
||||
|
||||
/**
|
||||
* @brief 反转链表
|
||||
* @param list: [输入] 链表指针
|
||||
* @return void
|
||||
* @example list_reverse(&myList);
|
||||
*/
|
||||
extern void list_reverse(List *list);
|
||||
|
||||
/**
|
||||
* @brief 获取链表长度
|
||||
* @param list: [输入] 链表指针
|
||||
* @return long 返回链表中元素个数
|
||||
* @example length = list_get_lenth(&myList);
|
||||
*/
|
||||
extern long list_get_lenth(List *list);
|
||||
|
||||
/**
|
||||
* @brief 获取链表指定位置的元素
|
||||
* @param list: [输入] 链表指针
|
||||
* @param idx: [输入] 元素索引
|
||||
* @return void* 返回指定位置的元素指针,若索引无效返回NULL
|
||||
* @example element = list_get_element(&myList, 3);
|
||||
*/
|
||||
extern void *list_get_element(List *list, int idx);
|
||||
|
||||
/**
|
||||
* @brief 销毁链表
|
||||
* @param list: [输入] 链表指针
|
||||
* @param destroy: [输入] 数据销毁函数指针
|
||||
* @return void
|
||||
* @example list_destroy(&myList, destroyFunc);
|
||||
*/
|
||||
extern void list_destroy(List *list, void (*destroy)(void *data));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif //HW_LIB_LIST_H
|
@@ -44,9 +44,29 @@ typedef enum { // 定义枚举类型Type_t,包含不同数据类型
|
||||
#define TYPE_T(v) _Generic((v), \
|
||||
u8 *:0,u16 *:1,u32 *:2, \
|
||||
char *:3,short *:4,int *:5, \
|
||||
float *:6,double *:10, \
|
||||
float *:6,double *:7, \
|
||||
default: ((void)0))
|
||||
|
||||
#define MESSAGE(err) \
|
||||
( err==ERROR_NO_MEMORY ? MSG_NO_MEMORY \
|
||||
: err==ERROR_INVALID_INDEX ? MSG_INVALID_INDEX \
|
||||
: err==ERROR_INVALID_VALUE ? MSG_INVALID_VALUE \
|
||||
: 0 )
|
||||
|
||||
#define TYPE_U8 (unsigned char *)
|
||||
|
||||
#define TYPE_ptr(type) \
|
||||
(type==0 ? TYPE_U8 \
|
||||
: type==1 ? (unsigned short *) \
|
||||
: type==2 ? (unsigned int *) \
|
||||
: type==3 ? (char *) \
|
||||
: type==4 ? (short *) \
|
||||
: type==5 ? (int *) \
|
||||
: type==6 ? (float *) \
|
||||
: type==7 ? (double *) \
|
||||
: ((void)0))\
|
||||
|
||||
|
||||
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) // 计算数组的元素个数
|
||||
#define in , // 定义逗号为in
|
||||
|
Reference in New Issue
Block a user