修改命名

This commit is contained in:
建峰 2024-06-23 02:14:42 +08:00
parent e778cfd7c4
commit 004a92821c
12 changed files with 52 additions and 110 deletions

View File

@ -8,6 +8,8 @@
"queue.h": "c", "queue.h": "c",
"stdbool.h": "c", "stdbool.h": "c",
"stdint.h": "c", "stdint.h": "c",
"stack.h": "c" "stack.h": "c",
"list.h": "c",
"stdio.h": "c"
} }
} }

View File

@ -12,7 +12,7 @@
| stack_init | 链表 | | | stack_init | 链表 | |
| stack2_init | 动态数组 | | | stack2_init | 动态数组 | |
| **list** | | **列表** | **list** | | **列表**
| list_init | 动态数组 | | | list_init2 | 动态数组 | |
| **queue** | | **队列** | **queue** | | **队列**
| queue_init | 单向链表 | | | queue_init | 单向链表 | |
| queue2_init | 数组 | FIFO/空/满 | | queue2_init | 数组 | FIFO/空/满 |

View File

@ -47,7 +47,11 @@ struct _list
void (*print)(struct _list* self); void (*print)(struct _list* self);
void (*print_obj)(void* obj); void (*print_obj)(void* obj);
}; };
typedef struct _list* list_t;
bool list_init(struct _list* list, uint32_t obj_size); bool list_init2(struct _list* self, uint32_t obj_size, uint32_t capacity);
list_t list_new(void);
void list_free(list_t list);
#endif // _LIST_H_ #endif // _LIST_H_

View File

@ -12,26 +12,28 @@ struct _stack_node
struct _stack struct _stack
{ {
struct _stack_node * _head; struct _stack_node * _head;
uint32_t _size; // 栈大小 uint32_t _size; // 栈大小
uint32_t _obj_size; // 元素大小 uint32_t _obj_size; // 元素大小
// only for array mode
uint32_t _capacity; // 总容量 uint32_t _capacity; // 总容量
uint32_t _ratio; // 扩展比率 uint32_t _ratio; // 扩展比率
// kernel // kernel
bool (*peek)(struct _stack* s, void* obj); bool (*peek)(struct _stack* self, void* obj);
bool (*push)(struct _stack* s, void* obj); bool (*push)(struct _stack* self, void* obj);
bool (*pop)(struct _stack* s, void* obj); bool (*pop)(struct _stack* self, void* obj);
// base // base
uint32_t(*size)(struct _stack* s); uint32_t(*size)(struct _stack* self);
bool (*empty)(struct _stack* s); bool (*empty)(struct _stack* self);
// others // others
bool (*clear)(struct _stack* s); bool (*clear)(struct _stack* self);
void (*destory)(struct _stack* s); void (*destory)(struct _stack* self);
// print
void (*print)(struct _stack* self);
void (*print_obj)(void* obj); void (*print_obj)(void* obj);
void (*print)(struct _stack* s);
}; };
typedef struct _stack* stack_t; typedef struct _stack* stack_t;

View File

@ -9,6 +9,12 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "list.h"
#include "stack.h"
#include "queue.h"
#include "deque.h"
#include "tree.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;

View File

@ -170,14 +170,14 @@ void list_print(struct _list* self)
} }
} }
bool list_init(struct _list* list, uint32_t obj_size) bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
{ {
assert(list != NULL); assert(list != NULL);
// 1. set attr // 1. set attr
list->_obj_size = obj_size; list->_obj_size = obj_size;
list->_size = 0; list->_size = 0;
list->_capacity = 64; list->_capacity = capacity;
list->_ratio = 2; list->_ratio = 2;
// 2. set function // 2. set function

View File

@ -27,8 +27,8 @@ int main()
printf("----- unicstl test -----\n"); printf("----- unicstl test -----\n");
// while (1) // while (1)
{ {
// list_test(); list_test();
stack_test(); // test_stack();
// deque_test(); // deque_test();
// queue_test(); // queue_test();

View File

@ -2,23 +2,9 @@
#ifndef _TEST_H_ #ifndef _TEST_H_
#define _TEST_H_ #define _TEST_H_
#include "common.h" #define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
// It is recommended to keep one at the same time
#define RBTREE_TEST 0
#define AVLTREE_TEST 0
#define RAVLTRE_TEST 0
#define STACK_TEST 0
#define QUEUE_TEST 0
#define LIST_TEST 0
#if AVLTREE == 1 || RAVLTREE == 1 || RBTREE == 1
#if QUEUE_TEST == 1 || STACK_TEST == 1
#error "When use the tree, you can't use the base data type of stack or queue! "
#endif
#endif
#include "unicstl.h"
/** /**
* @brief test objects * @brief test objects
@ -41,7 +27,7 @@ void print_str(void* obj);
* *
*/ */
void list_test(void); void list_test(void);
void stack_test(void); void test_stack(void);
void deque_test(void); void deque_test(void);
void queue_test(void); void queue_test(void);

View File

@ -1,10 +1,5 @@
#include "deque.h" #include "test.h"
static void print_num(void* obj)
{
printf("(%2d ) ", *(int*)obj);
}
static void deque_test_num(void) static void deque_test_num(void)
{ {
@ -174,18 +169,6 @@ static void deque_test_num(void)
dq.destory(&dq); dq.destory(&dq);
} }
struct _student
{
char name[16];
int id;
};
static void print_struct(void* obj)
{
struct _student* student = (struct _student*)obj;
printf("(%4d:%-8s) ", student->id, student->name);
}
static void deque_test_struct(void) static void deque_test_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;

View File

@ -1,12 +1,7 @@
#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
#include "list.h"
static void print_num(void* obj) #include "test.h"
{
printf("(%2d )", *(int*)obj);
}
static void list_test_num(void) static void test_list_num(void)
{ {
int i = 0; int i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -15,7 +10,7 @@ static void list_test_num(void)
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
struct _list list; struct _list list;
list_init(&list, sizeof(int)); list_init2(&list, sizeof(int), 64);
list.print_obj = print_num; list.print_obj = print_num;
printf("\n\n----- list_test_num -----\n"); printf("\n\n----- list_test_num -----\n");
@ -138,20 +133,7 @@ static void list_test_num(void)
list.destory(&list); list.destory(&list);
} }
static void test_list_struct(void)
struct _student
{
char name[16];
int id;
};
static void print_struct(void* obj)
{
struct _student* student = (struct _student*)obj;
printf("(%4d:%-8s) ", student->id, student->name);
}
static void list_test_struct(void)
{ {
int i = 0; int i = 0;
struct _student data[] = { struct _student data[] = {
@ -164,7 +146,7 @@ static void list_test_struct(void)
int len = sizeof(data) / sizeof(data[0]); int len = sizeof(data) / sizeof(data[0]);
struct _list list; struct _list list;
list_init(&list, sizeof(struct _student)); list_init2(&list, sizeof(struct _student), 64);
list.print_obj = print_struct; list.print_obj = print_struct;
printf("\n\n----- list_test_num -----\n"); printf("\n\n----- list_test_num -----\n");
@ -307,6 +289,6 @@ static void list_test_struct(void)
void list_test(void) void list_test(void)
{ {
list_test_num(); test_list_num();
list_test_struct(); test_list_struct();
} }

View File

@ -1,5 +1,4 @@
#include "queue.h"
#include "test.h" #include "test.h"
#if QUEUE_TEST == 1 #if QUEUE_TEST == 1

View File

@ -1,11 +1,7 @@
#include "stack.h"
static void print_num(void* obj) #include "test.h"
{
printf("(%2d ) ", *(int*)obj);
}
static void stack_test_num(void) static void test_stack_num(void)
{ {
uint32_t i = 0; uint32_t i = 0;
int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int data[] = { 1,2,3,4,5,6,7,8,9,10 };
@ -16,7 +12,7 @@ static void stack_test_num(void)
stack_init(&s, sizeof(int)); stack_init(&s, sizeof(int));
s.print_obj = print_num; s.print_obj = print_num;
printf("\n\n----- stack_test_num -----\n"); printf("\n\n----- test_stack_num -----\n");
// get top if stack is empty // get top if stack is empty
s.peek(&s, &temp); s.peek(&s, &temp);
@ -66,12 +62,7 @@ static void stack_test_num(void)
s.destory(&s); s.destory(&s);
} }
static void print_char(void* obj) static void test_stack_char(void)
{
printf("(%2c ) ", *(char*)obj);
}
static void stack_test_char(void)
{ {
uint32_t i = 0; uint32_t i = 0;
char data[] = "abcdefghijk"; char data[] = "abcdefghijk";
@ -82,7 +73,7 @@ static void stack_test_char(void)
stack_init2(&s, sizeof(char), 64); stack_init2(&s, sizeof(char), 64);
s.print_obj = print_char; s.print_obj = print_char;
printf("\n\n----- stack_test_char -----\n"); printf("\n\n----- test_stack_char -----\n");
// get top if stack is empty // get top if stack is empty
s.peek(&s, &temp); s.peek(&s, &temp);
@ -131,20 +122,7 @@ static void stack_test_char(void)
s.destory(&s); s.destory(&s);
} }
static void test_stack_struct(void)
struct _student
{
char name[16];
int id;
};
static void print_struct(void* obj)
{
struct _student* student = (struct _student*)obj;
printf("(%2d:%-5s ) ", student->id, student->name);
}
static void stack_test_struct(void)
{ {
uint32_t i = 0; uint32_t i = 0;
struct _student data[] = { struct _student data[] = {
@ -159,7 +137,7 @@ static void stack_test_struct(void)
stack_init(stack, sizeof(struct _student)); stack_init(stack, sizeof(struct _student));
stack->print_obj = print_struct; stack->print_obj = print_struct;
printf("\n\n----- stack_test_struct -----\n"); printf("\n\n----- test_stack_struct -----\n");
// get top if stack is empty // get top if stack is empty
stack->peek(stack, &temp); stack->peek(stack, &temp);
@ -221,9 +199,9 @@ static void stack_test_struct(void)
stack_free(stack); stack_free(stack);
} }
void stack_test(void) void test_stack(void)
{ {
stack_test_num(); test_stack_num();
stack_test_char(); test_stack_char();
stack_test_struct(); test_stack_struct();
} }