From 2e31e1234de8d69c7ea4872c5cefd6e240804185 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 20 Jun 2024 14:00:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E6=B7=BB=E5=8A=A0=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=9A=84=E7=BB=93=E6=9E=84=E4=BD=93=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=90=8E=E7=BB=AD=E5=AE=9E=E7=8E=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datastruct/config.h | 2 +- datastruct/list.c | 4 +++ datastruct/list.h | 61 ++++++++++++++++++++++++++++----------------- datastruct/stack.h | 15 +++++------ 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/datastruct/config.h b/datastruct/config.h index bf1b95a..c3252e6 100644 --- a/datastruct/config.h +++ b/datastruct/config.h @@ -14,7 +14,7 @@ #define RAVLTREE 0 // avl tree by recursion #define QUEUE 0 #define STACK 1 -#define LIST 0 +#define LIST 1 // -------------------------------------------------- diff --git a/datastruct/list.c b/datastruct/list.c index be40d22..a2bd94e 100644 --- a/datastruct/list.c +++ b/datastruct/list.c @@ -167,6 +167,7 @@ void list_traversal_reversed(plist_t list, list_data_disp_t disp) } #endif +#if 0 bool list_init(list_t list) { // list->capacity = 64; @@ -356,3 +357,6 @@ void list_test(void) list_print(&list); list_destory(&list); } + +#endif + diff --git a/datastruct/list.h b/datastruct/list.h index 67bc5c2..8cf9a8f 100644 --- a/datastruct/list.h +++ b/datastruct/list.h @@ -39,35 +39,50 @@ void list_traversal_reversed(plist_t list, list_data_disp_t disp); extern void list_test(void); #endif - - #ifdef LIST -typedef int list_data_t; - struct _list { - list_data_t * array; // 数组(存储列表元素) - int capacity; // 列表容量 - int size; // 列表大小 - int extend_ratio; // 列表每次扩容的倍数 + void * obj; + uint32_t _obj_size; // 元素大小 + uint32_t _size; // 栈大小 + uint32_t _capacity; // 总容量 + uint32_t _ratio; // 扩展比率 + + // kernel + bool (*append)(struct _list* self, void* obj); + + bool (*clear)(struct _list* self); + + bool (*empty)(struct _list* self); + + uint32_t (*index)(struct _list* self, void* obj); + + bool (*insert)(struct _list* self, uint32_t index, void* obj); + + bool (*pop)(struct _list* self, uint32_t index, void* obj); + + bool (*at)(struct _list* self, uint32_t index); + + bool (*set)(struct _list* self, uint32_t index, void* obj); + + uint32_t(*size)(struct _list* self); + + bool (*remove)(struct _list* self, uint32_t index); + + bool (*reverse)(struct _list* self); + + bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)); + + // free + void (*destory)(struct _list* self); + + // print + void (*print)(struct _list* self); + void (*print_obj)(void* obj); }; -typedef struct _list * list_t; -bool list_init(list_t list); -void list_destory(list_t list); - -bool list_empty(list_t list); -int list_size(list_t list); - -bool list_insert(list_t list, int index, list_data_t data); -bool list_append(list_t list, list_data_t data); -list_data_t list_delete(list_t list, int index); -bool list_clear(list_t list); -list_data_t list_get(list_t list, int index); -bool list_set(list_t list, int index, list_data_t data); - -extern void list_test(void); +bool list_init(struct _list *l); #endif diff --git a/datastruct/stack.h b/datastruct/stack.h index e18dc0b..642e451 100644 --- a/datastruct/stack.h +++ b/datastruct/stack.h @@ -12,20 +12,21 @@ struct _stack_node struct _stack { struct _stack_node * _head; - uint32_t _capacity; // 总容量 for array - uint32_t _ratio; // 扩展比率 for array uint32_t _size; // 栈大小 uint32_t _obj_size; // 元素大小 + // only for array mode + uint32_t _capacity; // 总容量 + uint32_t _ratio; // 扩展比率 + + // kernel + bool (*peek)(struct _stack* s, void* obj); + bool (*push)(struct _stack* s, void* obj); + bool (*pop)(struct _stack* s, void* obj); // base uint32_t(*size)(struct _stack* s); bool (*empty)(struct _stack* s); - // kernel - bool (*peek)(struct _stack* s, void *obj); - bool (*push)(struct _stack* s, void* obj); - bool (*pop)(struct _stack* s, void* obj); - // others bool (*clear)(struct _stack* s); void (*destory)(struct _stack* s);