mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
修改命名
This commit is contained in:
parent
e778cfd7c4
commit
004a92821c
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -8,6 +8,8 @@
|
||||
"queue.h": "c",
|
||||
"stdbool.h": "c",
|
||||
"stdint.h": "c",
|
||||
"stack.h": "c"
|
||||
"stack.h": "c",
|
||||
"list.h": "c",
|
||||
"stdio.h": "c"
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
| stack_init | 链表 | |
|
||||
| stack2_init | 动态数组 | |
|
||||
| **list** | | **列表**
|
||||
| list_init | 动态数组 | |
|
||||
| list_init2 | 动态数组 | |
|
||||
| **queue** | | **队列**
|
||||
| queue_init | 单向链表 | |
|
||||
| queue2_init | 数组 | FIFO/空/满 |
|
||||
|
@ -47,7 +47,11 @@ struct _list
|
||||
void (*print)(struct _list* self);
|
||||
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_
|
||||
|
@ -12,26 +12,28 @@ struct _stack_node
|
||||
struct _stack
|
||||
{
|
||||
struct _stack_node * _head;
|
||||
|
||||
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);
|
||||
bool (*peek)(struct _stack* self, void* obj);
|
||||
bool (*push)(struct _stack* self, void* obj);
|
||||
bool (*pop)(struct _stack* self, void* obj);
|
||||
|
||||
// base
|
||||
uint32_t(*size)(struct _stack* s);
|
||||
bool (*empty)(struct _stack* s);
|
||||
uint32_t(*size)(struct _stack* self);
|
||||
bool (*empty)(struct _stack* self);
|
||||
|
||||
// others
|
||||
bool (*clear)(struct _stack* s);
|
||||
void (*destory)(struct _stack* s);
|
||||
bool (*clear)(struct _stack* self);
|
||||
void (*destory)(struct _stack* self);
|
||||
|
||||
// print
|
||||
void (*print)(struct _stack* self);
|
||||
void (*print_obj)(void* obj);
|
||||
void (*print)(struct _stack* s);
|
||||
};
|
||||
typedef struct _stack* stack_t;
|
||||
|
||||
|
@ -9,6 +9,12 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "stack.h"
|
||||
#include "queue.h"
|
||||
#include "deque.h"
|
||||
#include "tree.h"
|
||||
|
||||
#if RBTREE == 1
|
||||
#include "rbtree.h"
|
||||
typedef prbtree_node_t stack_data_t;
|
||||
|
@ -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);
|
||||
|
||||
// 1. set attr
|
||||
list->_obj_size = obj_size;
|
||||
list->_size = 0;
|
||||
list->_capacity = 64;
|
||||
list->_capacity = capacity;
|
||||
list->_ratio = 2;
|
||||
|
||||
// 2. set function
|
||||
|
@ -27,8 +27,8 @@ int main()
|
||||
printf("----- unicstl test -----\n");
|
||||
// while (1)
|
||||
{
|
||||
// list_test();
|
||||
stack_test();
|
||||
list_test();
|
||||
// test_stack();
|
||||
// deque_test();
|
||||
// queue_test();
|
||||
|
||||
|
20
test/test.h
20
test/test.h
@ -2,23 +2,9 @@
|
||||
#ifndef _TEST_H_
|
||||
#define _TEST_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// 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
|
||||
#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
|
||||
|
||||
#include "unicstl.h"
|
||||
|
||||
/**
|
||||
* @brief test objects
|
||||
@ -41,7 +27,7 @@ void print_str(void* obj);
|
||||
*
|
||||
*/
|
||||
void list_test(void);
|
||||
void stack_test(void);
|
||||
void test_stack(void);
|
||||
void deque_test(void);
|
||||
|
||||
void queue_test(void);
|
||||
|
@ -1,10 +1,5 @@
|
||||
|
||||
#include "deque.h"
|
||||
|
||||
static void print_num(void* obj)
|
||||
{
|
||||
printf("(%2d ) ", *(int*)obj);
|
||||
}
|
||||
#include "test.h"
|
||||
|
||||
static void deque_test_num(void)
|
||||
{
|
||||
@ -174,18 +169,6 @@ static void deque_test_num(void)
|
||||
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)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
|
@ -1,12 +1,7 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022
|
||||
#include "list.h"
|
||||
|
||||
static void print_num(void* obj)
|
||||
{
|
||||
printf("(%2d )", *(int*)obj);
|
||||
}
|
||||
#include "test.h"
|
||||
|
||||
static void list_test_num(void)
|
||||
static void test_list_num(void)
|
||||
{
|
||||
int i = 0;
|
||||
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]);
|
||||
|
||||
struct _list list;
|
||||
list_init(&list, sizeof(int));
|
||||
list_init2(&list, sizeof(int), 64);
|
||||
list.print_obj = print_num;
|
||||
|
||||
printf("\n\n----- list_test_num -----\n");
|
||||
@ -138,20 +133,7 @@ static void list_test_num(void)
|
||||
list.destory(&list);
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
static void test_list_struct(void)
|
||||
{
|
||||
int i = 0;
|
||||
struct _student data[] = {
|
||||
@ -164,7 +146,7 @@ static void list_test_struct(void)
|
||||
int len = sizeof(data) / sizeof(data[0]);
|
||||
|
||||
struct _list list;
|
||||
list_init(&list, sizeof(struct _student));
|
||||
list_init2(&list, sizeof(struct _student), 64);
|
||||
list.print_obj = print_struct;
|
||||
|
||||
printf("\n\n----- list_test_num -----\n");
|
||||
@ -307,6 +289,6 @@ static void list_test_struct(void)
|
||||
|
||||
void list_test(void)
|
||||
{
|
||||
list_test_num();
|
||||
list_test_struct();
|
||||
test_list_num();
|
||||
test_list_struct();
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
#include "queue.h"
|
||||
#include "test.h"
|
||||
|
||||
#if QUEUE_TEST == 1
|
||||
|
@ -1,11 +1,7 @@
|
||||
#include "stack.h"
|
||||
|
||||
static void print_num(void* obj)
|
||||
{
|
||||
printf("(%2d ) ", *(int*)obj);
|
||||
}
|
||||
#include "test.h"
|
||||
|
||||
static void stack_test_num(void)
|
||||
static void test_stack_num(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
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));
|
||||
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
|
||||
s.peek(&s, &temp);
|
||||
|
||||
@ -66,12 +62,7 @@ static void stack_test_num(void)
|
||||
s.destory(&s);
|
||||
}
|
||||
|
||||
static void print_char(void* obj)
|
||||
{
|
||||
printf("(%2c ) ", *(char*)obj);
|
||||
}
|
||||
|
||||
static void stack_test_char(void)
|
||||
static void test_stack_char(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
char data[] = "abcdefghijk";
|
||||
@ -82,7 +73,7 @@ static void stack_test_char(void)
|
||||
stack_init2(&s, sizeof(char), 64);
|
||||
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
|
||||
s.peek(&s, &temp);
|
||||
|
||||
@ -131,20 +122,7 @@ static void stack_test_char(void)
|
||||
s.destory(&s);
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
static void test_stack_struct(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
struct _student data[] = {
|
||||
@ -159,7 +137,7 @@ static void stack_test_struct(void)
|
||||
stack_init(stack, sizeof(struct _student));
|
||||
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
|
||||
stack->peek(stack, &temp);
|
||||
|
||||
@ -221,9 +199,9 @@ static void stack_test_struct(void)
|
||||
stack_free(stack);
|
||||
}
|
||||
|
||||
void stack_test(void)
|
||||
void test_stack(void)
|
||||
{
|
||||
stack_test_num();
|
||||
stack_test_char();
|
||||
stack_test_struct();
|
||||
test_stack_num();
|
||||
test_stack_char();
|
||||
test_stack_struct();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user