想把stack修改成通用的

This commit is contained in:
建峰 2024-06-19 18:30:15 +08:00
parent fcce7a9202
commit deb5ee1f59
7 changed files with 92 additions and 31 deletions

View File

@ -11,7 +11,7 @@
#include "config.h" #include "config.h"
// ---------------------------------------
#if RBTREE == 1 #if RBTREE == 1
#include "rbtree.h" #include "rbtree.h"
typedef prbtree_node_t stack_data_t; typedef prbtree_node_t stack_data_t;
@ -28,6 +28,5 @@
typedef int list_data_t; typedef int list_data_t;
#endif #endif
#endif // _DEMO_H_ #endif // _DEMO_H_

View File

@ -9,12 +9,12 @@
// optional // optional
#define RBTREE 1 #define RBTREE 0
#define AVLTREE 0 #define AVLTREE 0
#define RAVLTREE 0 // avl tree by recursion #define RAVLTREE 0 // avl tree by recursion
#define QUEUE 1 #define QUEUE 0
#define STACK 1 #define STACK 1
#define LIST 1 #define LIST 0
// -------------------------------------------------- // --------------------------------------------------

View File

@ -4,7 +4,7 @@
#include "common.h" #include "common.h"
// #define LINK_LIST 1 // #define LINK_LIST 1
#define LIST 1 // #define LIST 1
#ifdef LINK_LIST #ifdef LINK_LIST
#define LIST_TEST #define LIST_TEST

View File

@ -3,13 +3,11 @@
int main() int main()
{ {
list_test();
// while (1) // while (1)
{ {
//stack_test();
//queue_test();
// list_test(); // list_test();
stack_test();
// queue_test();
// tree_test(); // tree_test();
// rbtree_test(); // rbtree_test();
} }

View File

@ -1,8 +1,9 @@
#include "stack.h" #include "stack.h"
#if STACK == 1
#if STACK == 1
#if 0
bool stack_init(pstack_t *head) bool stack_init(pstack_t *head)
{ {
*head = (pstack_t)malloc(sizeof(stack_t)); *head = (pstack_t)malloc(sizeof(stack_t));
@ -111,5 +112,69 @@ uint32_t stack_get_size(pstack_t head)
{ {
return head->size; 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 #endif

View File

@ -5,30 +5,29 @@
#if STACK == 1 #if STACK == 1
// typedef int stack_data_t; struct _stack_node
typedef struct _stack_node_t
{ {
stack_data_t data; void * obj;
struct _stack_node_t * next; struct _stack_node * next;
} stack_node_t, *pstack_node_t; };
typedef struct _stack_t struct _stack
{ {
struct _stack_node_t * top; struct _stack_node * _node;
uint32_t size; uint32_t _capacity; // 总容量
} stack_t, *pstack_t; 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); bool (*peek)(struct _stack* s, void *obj);
void stack_destroy(pstack_t *head); bool (*push)(struct _stack* s, void* obj);
bool stack_empty(pstack_t head); bool (*pop)(struct _stack* s, void* obj);
void stack_clear(pstack_t head); };
uint32_t stack_get_size(pstack_t head);
bool stack_push(pstack_t head, stack_data_t data); bool stack_init(struct _stack* s, uint32_t type_size);
void stack_pop(pstack_t head, stack_data_t *data);
bool stack_get_top(pstack_t head, stack_data_t *data);
#endif #endif

View File

@ -8,7 +8,7 @@
#include "common.h" #include "common.h"
// It is recommended to keep one at the same time // It is recommended to keep one at the same time
#define RBTREE_TEST 1 #define RBTREE_TEST 0
#define AVLTREE_TEST 0 #define AVLTREE_TEST 0
#define RAVLTRE_TEST 0 #define RAVLTRE_TEST 0
#define STACK_TEST 0 #define STACK_TEST 0