先添加通用列表的结构体,方便后续实现功能。

This commit is contained in:
建峰 2024-06-20 14:00:48 +08:00
parent fe82a85764
commit 2e31e1234d
4 changed files with 51 additions and 31 deletions

View File

@ -14,7 +14,7 @@
#define RAVLTREE 0 // avl tree by recursion #define RAVLTREE 0 // avl tree by recursion
#define QUEUE 0 #define QUEUE 0
#define STACK 1 #define STACK 1
#define LIST 0 #define LIST 1
// -------------------------------------------------- // --------------------------------------------------

View File

@ -167,6 +167,7 @@ void list_traversal_reversed(plist_t list, list_data_disp_t disp)
} }
#endif #endif
#if 0
bool list_init(list_t list) bool list_init(list_t list)
{ {
// list->capacity = 64; // list->capacity = 64;
@ -356,3 +357,6 @@ void list_test(void)
list_print(&list); list_print(&list);
list_destory(&list); list_destory(&list);
} }
#endif

View File

@ -39,35 +39,50 @@ void list_traversal_reversed(plist_t list, list_data_disp_t disp);
extern void list_test(void); extern void list_test(void);
#endif #endif
#ifdef LIST #ifdef LIST
typedef int list_data_t;
struct _list struct _list
{ {
list_data_t * array; // 数组(存储列表元素) void * obj;
int capacity; // 列表容量 uint32_t _obj_size; // 元素大小
int size; // 列表大小 uint32_t _size; // 栈大小
int extend_ratio; // 列表每次扩容的倍数 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); bool list_init(struct _list *l);
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);
#endif #endif

View File

@ -12,20 +12,21 @@ struct _stack_node
struct _stack struct _stack
{ {
struct _stack_node * _head; struct _stack_node * _head;
uint32_t _capacity; // ×ÜÈÝÁ¿ for array
uint32_t _ratio; // À©Õ¹±ÈÂÊ for array
uint32_t _size; // 栈大小 uint32_t _size; // 栈大小
uint32_t _obj_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 // base
uint32_t(*size)(struct _stack* s); uint32_t(*size)(struct _stack* s);
bool (*empty)(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 // others
bool (*clear)(struct _stack* s); bool (*clear)(struct _stack* s);
void (*destory)(struct _stack* s); void (*destory)(struct _stack* s);