From deb5ee1f59036d5dc6e92a09f5b4ae952c4a5bd6 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Wed, 19 Jun 2024 18:30:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=83=B3=E6=8A=8Astack=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=88=90=E9=80=9A=E7=94=A8=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datastruct/common.h | 3 +- datastruct/config.h | 6 ++-- datastruct/list.h | 2 +- datastruct/main.c | 8 ++---- datastruct/stack.c | 67 ++++++++++++++++++++++++++++++++++++++++++++- datastruct/stack.h | 35 ++++++++++++----------- datastruct/test.h | 2 +- 7 files changed, 92 insertions(+), 31 deletions(-) diff --git a/datastruct/common.h b/datastruct/common.h index 58c4db7..eb33d9d 100644 --- a/datastruct/common.h +++ b/datastruct/common.h @@ -11,7 +11,7 @@ #include "config.h" -// --------------------------------------- + #if RBTREE == 1 #include "rbtree.h" typedef prbtree_node_t stack_data_t; @@ -28,6 +28,5 @@ typedef int list_data_t; #endif - #endif // _DEMO_H_ diff --git a/datastruct/config.h b/datastruct/config.h index c18c13d..bf1b95a 100644 --- a/datastruct/config.h +++ b/datastruct/config.h @@ -9,12 +9,12 @@ // optional -#define RBTREE 1 +#define RBTREE 0 #define AVLTREE 0 #define RAVLTREE 0 // avl tree by recursion -#define QUEUE 1 +#define QUEUE 0 #define STACK 1 -#define LIST 1 +#define LIST 0 // -------------------------------------------------- diff --git a/datastruct/list.h b/datastruct/list.h index e83257a..67bc5c2 100644 --- a/datastruct/list.h +++ b/datastruct/list.h @@ -4,7 +4,7 @@ #include "common.h" // #define LINK_LIST 1 -#define LIST 1 +// #define LIST 1 #ifdef LINK_LIST #define LIST_TEST diff --git a/datastruct/main.c b/datastruct/main.c index 2f894cf..398e5bb 100644 --- a/datastruct/main.c +++ b/datastruct/main.c @@ -3,14 +3,12 @@ int main() { - list_test(); - // while (1) { - //stack_test(); - //queue_test(); // list_test(); - //tree_test(); + stack_test(); + // queue_test(); + // tree_test(); // rbtree_test(); } return 0; diff --git a/datastruct/stack.c b/datastruct/stack.c index 95a6cda..df7739c 100644 --- a/datastruct/stack.c +++ b/datastruct/stack.c @@ -1,8 +1,9 @@ #include "stack.h" -#if STACK == 1 +#if STACK == 1 +#if 0 bool stack_init(pstack_t *head) { *head = (pstack_t)malloc(sizeof(stack_t)); @@ -111,5 +112,69 @@ uint32_t stack_get_size(pstack_t head) { return head->size; } +#endif + +uint32_t stack_capacity(struct _stack* s) +{ + assert(s != NULL); + return s->_capacity; +} + +uint32_t stack_size(struct _stack* s) +{ + assert(s != NULL); + return s->_size; +} + +bool stack_empty(struct _stack* s) +{ + assert(s != NULL); + return !stack_size(s); +} + +bool stack_clear(struct _stack* s) +{ + assert(s != NULL); + return true; +} + +bool stack_peek(struct _stack* s, void* obj) +{ + return true; +} + +bool stack_push(struct _stack* s, void* obj) +{ + assert(s != NULL); + assert(obj != NULL); + return true; +} + +bool stack_pop(struct _stack* s, void* obj) +{ + return true; +} + +bool stack_init(struct _stack* s, uint32_t obj_size) +{ + // 1. set attr + s->_obj_size = obj_size; + s->_size = 0; + // s->capacity = 64; + + // 2. set function + s->clear = stack_clear; + s->empty = stack_empty; + s->peek = stack_peek; + s->pop = stack_pop; + s->push = stack_push; + s->size = stack_size; + + // 3. set node + s->_node->obj = NULL; + s->_node->next = NULL; + + return true; +} #endif diff --git a/datastruct/stack.h b/datastruct/stack.h index 1ea9cfe..ddd339a 100644 --- a/datastruct/stack.h +++ b/datastruct/stack.h @@ -5,30 +5,29 @@ #if STACK == 1 -// typedef int stack_data_t; - -typedef struct _stack_node_t +struct _stack_node { - stack_data_t data; - struct _stack_node_t * next; -} stack_node_t, *pstack_node_t; + void * obj; + struct _stack_node * next; +}; -typedef struct _stack_t +struct _stack { - struct _stack_node_t * top; - uint32_t size; -} stack_t, *pstack_t; + struct _stack_node * _node; + uint32_t _capacity; // 总容量 + uint32_t _size; // 栈大小 + uint32_t _obj_size; // 元素大小 + bool (*empty)(struct _stack* s); + bool (*clear)(struct _stack* s); + uint32_t(*size)(struct _stack* s); -bool stack_init(pstack_t *head); -void stack_destroy(pstack_t *head); -bool stack_empty(pstack_t head); -void stack_clear(pstack_t head); -uint32_t stack_get_size(pstack_t head); + bool (*peek)(struct _stack* s, void *obj); + bool (*push)(struct _stack* s, void* obj); + bool (*pop)(struct _stack* s, void* obj); +}; -bool stack_push(pstack_t head, stack_data_t data); -void stack_pop(pstack_t head, stack_data_t *data); -bool stack_get_top(pstack_t head, stack_data_t *data); +bool stack_init(struct _stack* s, uint32_t type_size); #endif diff --git a/datastruct/test.h b/datastruct/test.h index c5b77e8..c9d4f28 100644 --- a/datastruct/test.h +++ b/datastruct/test.h @@ -8,7 +8,7 @@ #include "common.h" // It is recommended to keep one at the same time -#define RBTREE_TEST 1 +#define RBTREE_TEST 0 #define AVLTREE_TEST 0 #define RAVLTRE_TEST 0 #define STACK_TEST 0