Compare commits

..

No commits in common. "23fc67cb490f790ab6a8ef3e49e98ae05772be7b" and "c19a3378215ee0aa9aa9daec31ef023b8877db09" have entirely different histories.

17 changed files with 190 additions and 187 deletions

View File

@ -16,17 +16,17 @@
| stack_new | 链表 | |
| stack_new2 | 动态数组 | |
| **list** | | **列表**
| list_new2 | 动态数组 | |
| list_init2 | 动态数组 | |
| **queue** | | **队列**
| queue_new | 单向链表 | |
| queue_new2 | 数组 | FIFO/空/满 |
| queue_init | 单向链表 | |
| queue_init2 | 数组 | FIFO/空/满 |
| **deque** | |**双端队列** |
| deque_new | 双向循环链表 | |
| deque_init | 双向循环链表 | |
| **tree** | |**树** |
| tree_avl_new | 二叉搜索树 | AVL树 |
| tree_rb_new | 二叉搜索树 | 红黑树 |
| tree_avl_init | 二叉搜索树 | AVL树 |
| tree_rb_init | 二叉搜索树 | 红黑树 |
| **heap** | |**堆** |
| heap_new2 | 数组 | 最大堆/最小堆 |
| heap_init2 | 数组 | 最大堆/最小堆 |
## 特点
| 原理 | 说明 |

View File

@ -17,7 +17,8 @@ static void demo_deque_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(sizeof(int));
deque_t deque = deque_new();
deque_init(deque, sizeof(int));
deque->print_obj = print_num;
printf("\n\n----- demo_deque_num -----\n");
@ -188,7 +189,8 @@ static void demo_deque_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(sizeof(struct _student));
deque_t deque = deque_new();
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct;
printf("\n\n----- demo_deque_struct -----\n");

View File

@ -20,7 +20,8 @@ void demo_heap_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new2(sizeof(int), 64);
heap_t heap = heap_new();
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num;
heap->compare = compare_num;
@ -90,7 +91,8 @@ static void demo_heap_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new2(sizeof(struct _student), 64);
heap_t heap = heap_new();
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct;
heap->compare = compare_struct;

View File

@ -18,7 +18,8 @@ static void demo_list_num(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new2(sizeof(int), 64);
list_t list = list_new();
list_init2(list, sizeof(int), 64);
list->print_obj = print_num;
printf("\n\n----- list_demo_num -----\n");
@ -153,7 +154,8 @@ static void demo_list_struct(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new2(sizeof(struct _student), 64);
list_t list = list_new();
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct;
printf("\n\n----- list_demo_num -----\n");

View File

@ -38,7 +38,8 @@ void demo_avltree_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_avl_new(sizeof(int));
tree_t tree = tree_new();
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -209,7 +210,8 @@ void demo_rbtree_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(int));
tree_t tree = tree_new();
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -340,7 +342,8 @@ void demo_rbtree_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(struct _student));
tree_t tree = tree_new();
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;

View File

@ -66,9 +66,9 @@ struct _deque
};
typedef struct _deque* deque_t;
// create and free deque
deque_t deque_new(uint32_t obj_size);
bool deque_init(struct _deque* self, uint32_t obj_size);
deque_t deque_new(void);
void deque_free(deque_t* deque);
#endif

View File

@ -56,9 +56,9 @@ struct _heap
};
typedef struct _heap* heap_t;
// create and free heap
heap_t heap_new2(uint32_t obj_size, uint32_t capacity);
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity);
heap_t heap_new(void);
void heap_free(heap_t* heap);
#endif // _HEAP_H_

View File

@ -65,9 +65,9 @@ struct _list
};
typedef struct _list* list_t;
// create and free list
list_t list_new2(uint32_t obj_size, uint32_t capacity);
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_

View File

@ -125,10 +125,11 @@ struct _tree
};
typedef struct _tree* tree_t;
// create and free tree
tree_t tree_avl_new(uint32_t obj_size);
tree_t tree_rb_new(uint32_t obj_size);
// bst_tree
bool tree_avl_init(struct _tree *self, uint32_t obj_size);
bool tree_rb_init(struct _tree *self, uint32_t obj_size);
tree_t tree_new(void);
void tree_free(tree_t* tree);
#endif // _TREE_H_

View File

@ -10,7 +10,7 @@
*/
#include "deque.h"
static bool deque_push_back(struct _deque* self, void* obj)
bool deque_push_back(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* front = NULL;
@ -54,7 +54,7 @@ static bool deque_push_back(struct _deque* self, void* obj)
return true;
}
static bool deque_push_front(struct _deque* self, void* obj)
bool deque_push_front(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* front = NULL;
@ -98,7 +98,7 @@ static bool deque_push_front(struct _deque* self, void* obj)
return true;
}
static bool deque_pop_back(struct _deque* self, void* obj)
bool deque_pop_back(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* node = NULL;
@ -135,7 +135,7 @@ static bool deque_pop_back(struct _deque* self, void* obj)
return true;
}
static bool deque_pop_front(struct _deque* self, void* obj)
bool deque_pop_front(struct _deque* self, void* obj)
{
assert(self != NULL);
struct _deque_node* node = NULL;
@ -172,7 +172,7 @@ static bool deque_pop_front(struct _deque* self, void* obj)
return true;
}
static bool deque_back(struct _deque* self, void* obj)
bool deque_back(struct _deque* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -185,7 +185,7 @@ static bool deque_back(struct _deque* self, void* obj)
return true;
}
static bool deque_front(struct _deque* self, void* obj)
bool deque_front(struct _deque* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -198,27 +198,27 @@ static bool deque_front(struct _deque* self, void* obj)
return true;
}
static bool deque_insert(struct _deque* self, int index, void* obj)
bool deque_insert(struct _deque* self, int index, void* obj)
{
return true;
}
static bool deque_erase(struct _deque* self, int index, void* obj)
bool deque_erase(struct _deque* self, int index, void* obj)
{
return true;
}
static int deque_index(struct _deque* self, void* obj)
int deque_index(struct _deque* self, void* obj)
{
return -1;
}
static bool deque_remove(struct _deque* self, void* obj)
bool deque_remove(struct _deque* self, void* obj)
{
return true;
}
static bool deque_clear(struct _deque* self)
bool deque_clear(struct _deque* self)
{
while (!self->empty(self))
{
@ -227,7 +227,7 @@ static bool deque_clear(struct _deque* self)
return true;
}
static bool deque_get(struct _deque* self, int index, void* obj)
bool deque_get(struct _deque* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -243,7 +243,7 @@ static bool deque_get(struct _deque* self, int index, void* obj)
return true;
}
static bool deque_set(struct _deque* self, int index, void* obj)
bool deque_set(struct _deque* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -259,19 +259,19 @@ static bool deque_set(struct _deque* self, int index, void* obj)
return true;
}
static uint32_t deque_size(struct _deque* self)
uint32_t deque_size(struct _deque* self)
{
assert(self != NULL);
return self->_size;
}
static bool deque_empty(struct _deque* self)
bool deque_empty(struct _deque* self)
{
assert(self != NULL);
return !self->size(self);
}
static void deque_destory(struct _deque* self)
void deque_destory(struct _deque* self)
{
assert(self != NULL);
self->clear(self);
@ -282,7 +282,7 @@ static void deque_destory(struct _deque* self)
}
}
static void deque_print(struct _deque* self)
void deque_print(struct _deque* self)
{
assert(self != NULL);
@ -295,7 +295,7 @@ static void deque_print(struct _deque* self)
}
}
static bool deque_init(struct _deque* self, uint32_t obj_size)
bool deque_init(struct _deque* self, uint32_t obj_size)
{
// attribute
self->_obj_size = obj_size;
@ -328,19 +328,9 @@ static bool deque_init(struct _deque* self, uint32_t obj_size)
return true;
}
deque_t deque_new(uint32_t obj_size)
deque_t deque_new(void)
{
struct _deque* deque = NULL;
deque = (struct _deque*)malloc(sizeof(struct _deque));
if(deque != NULL)
{
if(deque_init(deque, obj_size) != true)
{
free(deque);
deque = NULL;
}
}
return deque;
return (struct _deque*)malloc(sizeof(struct _deque));
}
void deque_free(deque_t *deque)

View File

@ -27,7 +27,7 @@ static int parent(int i)
return (i-1) >> 1;
}
static bool heap_peek(struct _heap* self, void* obj)
bool heap_peek(struct _heap* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -101,7 +101,7 @@ static void heap_fixed_up(struct _heap* self, int i)
}
}
static bool heap_push(struct _heap* self, void* obj)
bool heap_push(struct _heap* self, void* obj)
{
assert(self != NULL);
if(self->size(self) > self->_capacity)
@ -174,7 +174,7 @@ static void heap_fixed_down(struct _heap* self, int i)
}
}
static bool heap_pop(struct _heap* self, void* obj)
bool heap_pop(struct _heap* self, void* obj)
{
assert(self != NULL);
if(self->empty(self))
@ -192,32 +192,32 @@ static bool heap_pop(struct _heap* self, void* obj)
return true;
}
static void heap_setmin(struct _heap* self, bool min_flag)
void heap_setmin(struct _heap* self, bool min_flag)
{
assert(self != NULL);
self->_min_flag = min_flag;
}
static uint32_t heap_size(struct _heap* self)
uint32_t heap_size(struct _heap* self)
{
assert(self != NULL);
return self->_size;
}
static bool heap_empty(struct _heap* self)
bool heap_empty(struct _heap* self)
{
assert(self != NULL);
return self->size(self) == 0;
}
static bool heap_clear(struct _heap* self)
bool heap_clear(struct _heap* self)
{
assert(self != NULL);
self->_size = 0;
return true;
}
static void heap_destory(struct _heap* self)
void heap_destory(struct _heap* self)
{
assert(self != NULL);
self->clear(self);
@ -227,7 +227,7 @@ static void heap_destory(struct _heap* self)
}
}
static void heap_print(struct _heap* self)
void heap_print(struct _heap* self)
{
assert(self != NULL);
assert(self->obj != NULL);
@ -243,7 +243,7 @@ static void heap_print(struct _heap* self)
}
}
static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
{
assert(self != NULL);
@ -271,19 +271,9 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity)
return true;
}
heap_t heap_new2(uint32_t obj_size, uint32_t capacity)
heap_t heap_new(void)
{
heap_t heap = NULL;
heap = (struct _heap*)malloc(sizeof(struct _heap));
if(heap != NULL)
{
if(heap_init2(heap, obj_size, capacity) != true)
{
free(heap);
heap = NULL;
}
}
return heap;
return (struct _heap*)malloc(sizeof(struct _heap));
}
void heap_free(heap_t* heap)

View File

@ -10,7 +10,7 @@
*/
#include "list.h"
static bool list_append(struct _list* self, void* obj)
bool list_append(struct _list* self, void* obj)
{
assert(self != NULL);
assert(self->obj != NULL);
@ -35,7 +35,7 @@ static bool list_append(struct _list* self, void* obj)
return true;
}
static bool list_insert(struct _list* self, int index, void* obj)
bool list_insert(struct _list* self, int index, void* obj)
{
assert(index >= 0 && index < (int)self->size(self));
@ -58,7 +58,7 @@ static bool list_insert(struct _list* self, int index, void* obj)
return true;
}
static bool list_pop(struct _list* self, int index, void* obj)
bool list_pop(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -85,25 +85,25 @@ static bool list_pop(struct _list* self, int index, void* obj)
return true;
}
static int list_index(struct _list* self, void* obj)
int list_index(struct _list* self, void* obj)
{
return 0;
}
static bool list_remove(struct _list* self, void* obj)
bool list_remove(struct _list* self, void* obj)
{
assert(self != NULL);
return true;
}
static bool list_clear(struct _list* self)
bool list_clear(struct _list* self)
{
assert(self != NULL);
self->_size = 0;
return true;
}
static bool list_get(struct _list* self, int index, void* obj)
bool list_get(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -118,7 +118,7 @@ static bool list_get(struct _list* self, int index, void* obj)
return true;
}
static bool list_set(struct _list* self, int index, void* obj)
bool list_set(struct _list* self, int index, void* obj)
{
assert(self != NULL);
assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self));
@ -131,29 +131,29 @@ static bool list_set(struct _list* self, int index, void* obj)
return true;
}
static uint32_t list_size(struct _list* self)
uint32_t list_size(struct _list* self)
{
return self->_size;
}
static bool list_empty(struct _list* self)
bool list_empty(struct _list* self)
{
assert(self != NULL);
return !self->size(self);
}
static bool list_reverse(struct _list* self)
bool list_reverse(struct _list* self)
{
return true;
}
static bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
bool list_sort(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2))
{
return true;
}
// free
static void list_destory(struct _list* self)
void list_destory(struct _list* self)
{
assert(self != NULL);
if (self->obj != NULL)
@ -163,7 +163,7 @@ static void list_destory(struct _list* self)
}
// print
static void list_print(struct _list* self)
void list_print(struct _list* self)
{
assert(self != NULL);
@ -179,18 +179,18 @@ static void list_print(struct _list* self)
}
}
static void* list_begin(struct _list* self)
void* list_begin(struct _list* self)
{
self->_cur = 0;
return self->obj;
}
static void* list_end(struct _list* self)
void* list_end(struct _list* self)
{
return (char*)self->obj + self->_size * self->_obj_size;
}
static void* list_next(struct _list* self)
void* list_next(struct _list* self)
{
void *obj = NULL;
// if add this, can't go to end
@ -202,7 +202,7 @@ static void* list_next(struct _list* self)
return obj;
}
static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
{
assert(list != NULL);
assert(obj_size > 0);
@ -250,19 +250,9 @@ static bool list_init2(struct _list* list, uint32_t obj_size, uint32_t capacity)
return true;
}
list_t list_new2(uint32_t obj_size, uint32_t capacity)
list_t list_new(void)
{
struct _list* list = NULL;
list = (struct _list*)calloc(1, sizeof(struct _list));
if(list != NULL)
{
if(list_init2(list, obj_size, capacity) != true)
{
free(list);
list = NULL;
}
}
return list;
return (struct _list*)calloc(1, sizeof(struct _list));
}
void list_free(list_t* list)

View File

@ -128,7 +128,7 @@ static struct _tree_node* tree_turn_right_then_left(struct _tree* self, struct _
return node;
}
static int32_t tree_height(struct _tree* self, struct _tree_node* root)
int32_t tree_height(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
@ -384,7 +384,7 @@ struct _tree_node* tree_find_pos(struct _tree* self, void* obj)
}
static bool tree_avl_insert(struct _tree* self, void* obj)
bool tree_avl_insert(struct _tree* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -506,7 +506,7 @@ static bool tree_avl_delete_double_child(struct _tree* self, struct _tree_node*
return true;
}
static bool tree_avl_delete(struct _tree* self, void* obj)
bool tree_avl_delete(struct _tree* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -538,7 +538,7 @@ static bool tree_avl_delete(struct _tree* self, void* obj)
return true;
}
static struct _tree_node* tree_find(struct _tree* self, void* obj)
struct _tree_node* tree_find(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* root = self->_root;
@ -560,7 +560,7 @@ static struct _tree_node* tree_find(struct _tree* self, void* obj)
return NULL;
}
static bool tree_clear(struct _tree* self)
bool tree_clear(struct _tree* self)
{
assert(self != NULL);
if (self->_root == NULL)
@ -592,20 +592,20 @@ static bool tree_clear(struct _tree* self)
return true;
}
static bool tree_empty(struct _tree* self)
bool tree_empty(struct _tree* self)
{
assert(self != NULL);
return !self->size(self);
}
static uint32_t tree_size(struct _tree* self)
uint32_t tree_size(struct _tree* self)
{
assert(self != NULL);
return self->_size;
}
// free
static void tree_destory(struct _tree* self)
void tree_destory(struct _tree* self)
{
assert(self != NULL);
self->clear(self);
@ -621,13 +621,13 @@ static void tree_destory(struct _tree* self)
}
}
static void tree_order(struct _tree* self, bool right_priority)
void tree_order(struct _tree* self, bool right_priority)
{
assert(self != NULL);
self->_right_priority = right_priority;
}
static void tree_preorder(struct _tree* self, struct _tree_node* root)
void tree_preorder(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
@ -710,7 +710,7 @@ static void tree_preorder(struct _tree* self, struct _tree_node* root)
#endif
}
static void tree_inorder(struct _tree* self, struct _tree_node* root)
void tree_inorder(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
@ -794,7 +794,7 @@ static void tree_inorder(struct _tree* self, struct _tree_node* root)
#endif
}
static void tree_postorder(struct _tree* self, struct _tree_node* root)
void tree_postorder(struct _tree* self, struct _tree_node* root)
{
#if 0
assert(self != NULL);
@ -889,7 +889,7 @@ static void tree_postorder(struct _tree* self, struct _tree_node* root)
}
// traversal breadth
static void tree_breadth(struct _tree* self, struct _tree_node* root)
void tree_breadth(struct _tree* self, struct _tree_node* root)
{
assert(self != NULL);
if (root == NULL)
@ -992,7 +992,7 @@ static struct _tree_node* tree_find_max(struct _tree* self, struct _tree_node* r
#endif
}
static bool tree_min(struct _tree* self, void* obj)
bool tree_min(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* node = tree_find_min(self, self->_root);
@ -1004,7 +1004,7 @@ static bool tree_min(struct _tree* self, void* obj)
return true;
}
static bool tree_max(struct _tree* self, void* obj)
bool tree_max(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* node = tree_find_max(self, self->_root);
@ -1018,13 +1018,13 @@ static bool tree_max(struct _tree* self, void* obj)
static rbt_color tree_color(struct _tree_node* node)
rbt_color tree_color(struct _tree_node* node)
{
assert(node != NULL);
return node->color;
}
static bool tree_set_color(struct _tree_node* node, rbt_color color)
bool tree_set_color(struct _tree_node* node, rbt_color color)
{
assert(node != NULL);
node->color = color;
@ -1106,7 +1106,7 @@ static struct _tree_node* tree_rb_turn_right(struct _tree* self, struct _tree_no
return node;
}
static bool tree_rb_insert(struct _tree* self, void* obj)
bool tree_rb_insert(struct _tree* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -1259,7 +1259,7 @@ static bool tree_rb_rebalance(struct _tree* self, struct _tree_node* node)
return true;
}
static bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
{
assert(self != NULL);
struct _tree_node* father = NULL;
@ -1388,7 +1388,7 @@ static bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
return true;
}
static bool tree_rb_delete(struct _tree* self, void* obj)
bool tree_rb_delete(struct _tree* self, void* obj)
{
assert(self != NULL);
assert(obj != NULL);
@ -1463,13 +1463,13 @@ static bool tree_rb_delete(struct _tree* self, void* obj)
return true;
}
static void tree_set_order(struct _tree* self, enum _order order)
void tree_set_order(struct _tree* self, enum _order order)
{
assert(self != NULL);
self->_order = order;
}
static void* tree_begin(struct _tree* self)
void* tree_begin(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
@ -1684,7 +1684,7 @@ static void* tree_begin(struct _tree* self)
}
}
static void* tree_next(struct _tree* self)
void* tree_next(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
@ -1845,7 +1845,7 @@ static void* tree_next(struct _tree* self)
}
}
static void* tree_end(struct _tree* self)
void* tree_end(struct _tree* self)
{
assert(self != NULL);
switch (self->_order)
@ -1888,7 +1888,9 @@ static void* tree_end(struct _tree* self)
}
}
static bool tree_avl_init(struct _tree* self, uint32_t obj_size)
bool tree_avl_init(struct _tree* self, uint32_t obj_size)
{
assert(self != NULL);
@ -1947,7 +1949,7 @@ done:
return true;
}
static bool tree_rb_init(struct _tree* self, uint32_t obj_size)
bool tree_rb_init(struct _tree* self, uint32_t obj_size)
{
assert(self != NULL);
self->_obj_size = obj_size;
@ -2004,33 +2006,9 @@ done:
return false;
}
tree_t tree_avl_new(uint32_t obj_size)
tree_t tree_new(void)
{
tree_t tree = NULL;
tree = (struct _tree*)malloc(sizeof(struct _tree));
if(tree != NULL)
{
if(tree_avl_init(tree, obj_size) != true)
{
free(tree);
tree = NULL;
}
}
return tree;
}
tree_t tree_rb_new(uint32_t obj_size)
{
tree_t tree = NULL;
tree = (struct _tree*)malloc(sizeof(struct _tree));
if(tree != NULL)
{
if(tree_rb_init(tree, obj_size) != true)
{
free(tree);
tree = NULL;
}
}
return tree;
return (struct _tree*)malloc(sizeof(struct _tree));
}
void tree_free(tree_t* tree)

View File

@ -17,8 +17,10 @@ static void test_deque_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(sizeof(int));
deque_t deque = deque_new();
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(int));
deque->print_obj = print_num;
for (i = 0; i < len; i++)
@ -146,8 +148,10 @@ static void test_deque_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
deque_t deque = deque_new(sizeof(struct _student));
deque_t deque = deque_new();
TEST_ASSERT_NOT_NULL(deque);
deque_init(deque, sizeof(struct _student));
deque->print_obj = print_struct;
for (i = 0; i < len; i++)

View File

@ -46,8 +46,10 @@ static void test_heap_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new2(sizeof(int), 64);
heap_t heap = heap_new();
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(int), 64);
heap->print_obj = print_num;
heap->compare = compare_num;
@ -93,8 +95,10 @@ static void test_heap_struct(void)
struct _student temp = {0};
uint32_t len = sizeof(data) / sizeof(data[0]);
heap_t heap = heap_new2(sizeof(struct _student), 64);
heap_t heap = heap_new();
TEST_ASSERT_NOT_NULL(heap);
heap_init2(heap, sizeof(struct _student), 64);
heap->print_obj = print_struct;
heap->compare = compare_struct;

View File

@ -10,20 +10,39 @@
*/
#include "test.h"
static void test_list_init2(void)
{
struct _list list;
// ------------------------------
#ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(&list, 0, 1));
TEST_ASSERT_FALSE(list_init2(&list, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(list_init2(&list, sizeof(int), 1));
list.destory(&list);
}
static void test_list_new(void)
{
list_t list = NULL;
list = list_new2(sizeof(int), 1);
list = list_new();
list_free(&list);
// ------------------------------
list = list_new();
TEST_ASSERT_NOT_NULL(list);
list_free(&list);
TEST_ASSERT_NULL(list);
#ifdef NDEBUG
TEST_ASSERT_FALSE(list_init2(NULL, sizeof(int), 1));
TEST_ASSERT_FALSE(list_init2(list, 0, 1));
TEST_ASSERT_FALSE(list_init2(list, sizeof(int), 0));
#endif
TEST_ASSERT_TRUE(list_init2(list, sizeof(int), 1));
list_free(&list);
TEST_ASSERT_NULL(list);
list_free(&list); // list_free(NULL);
TEST_ASSERT_NULL(list);
//
TEST_ASSERT_NULL(list_new2(0, 1));
TEST_ASSERT_NULL(list_new2(sizeof(int), 0));
}
@ -37,7 +56,8 @@ static void test_list_append(void)
list_t list = NULL;
// ------------------------------
list = list_new2(sizeof(int), len);
list = list_new();
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->empty(list));
for(i = 0; i < len; i++)
{
@ -53,7 +73,8 @@ static void test_list_append(void)
// ------------------------------
// if capacity is less than data len
list = list_new2(sizeof(int), len - 2);
list = list_new();
list_init2(list, sizeof(int), len - 2);
for(i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(list->append(list, &data[i]));
@ -73,7 +94,8 @@ static void test_list_pop(void)
list_t list = NULL;
// ------------------------------
list = list_new2(sizeof(int), len);
list = list_new();
list_init2(list, sizeof(int), len);
for(i = 0; i < len; i++)
{
list->append(list, &data[i]);
@ -98,7 +120,8 @@ static void test_list_clear(void)
list_t list = NULL;
// ------------------------------
list = list_new2(sizeof(int), len);
list = list_new();
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++)
{
@ -120,9 +143,10 @@ static void test_list_num(void)
int len = sizeof(data) / sizeof(data[0]);
list_t list = NULL;
list = list_new2(sizeof(int), 64);
list = list_new();
TEST_ASSERT_TRUE(list != NULL);
list_init2(list, sizeof(int), 64);
list->print_obj = print_num;
for (i = 0; i < len; i++)
@ -206,7 +230,8 @@ static void test_list_struct(void)
int index = 0;
int len = sizeof(data) / sizeof(data[0]);
list_t list = list_new2(sizeof(struct _student), 64);
list_t list = list_new();
list_init2(list, sizeof(struct _student), 64);
list->print_obj = print_struct;
for (i = 0; i < len; i++)
@ -297,7 +322,8 @@ static void test_list_iter(void)
list_t list = NULL;
// ------------------------------
list = list_new2(sizeof(int), len);
list = list_new();
list_init2(list, sizeof(int), len);
TEST_ASSERT_TRUE(list->clear(list));
for(i = 0; i < len; i++)
{
@ -332,6 +358,7 @@ void test_list(void)
{
UnitySetTestFile(__FILE__);
RUN_TEST(test_list_init2);
RUN_TEST(test_list_new);
RUN_TEST(test_list_append);
RUN_TEST(test_list_pop);

View File

@ -39,7 +39,8 @@ void test_avltree_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_avl_new(sizeof(int));
tree_t tree = tree_new, ;
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -210,7 +211,8 @@ void test_rbtree_num(void)
int temp = 0;
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(int));
tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -341,7 +343,8 @@ void test_rbtree_struct(void)
struct _student temp = { 0 };
uint32_t len = sizeof(data) / sizeof(data[0]);
tree_t tree = tree_rb_new(sizeof(struct _student));
tree_t tree = tree_new, ;
tree_rb_init(tree, sizeof(struct _student));
tree->print_obj = print_struct;
tree->compare = compare_struct;
@ -523,8 +526,10 @@ static void test_avltree_iter(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_avl_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -600,9 +605,10 @@ static void test_avltree_insert(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_avl_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -646,9 +652,10 @@ static void test_avltree_delete(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_avl_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_avl_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -746,9 +753,10 @@ static void test_rbtree_iter(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_rb_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -824,9 +832,10 @@ static void test_rbtree_insert(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_rb_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;
@ -870,9 +879,10 @@ static void test_rbtree_delete(void)
int * iter = NULL;
int count = 0;
tree_t tree = tree_rb_new(sizeof(int));
tree_t tree = tree_new();
TEST_ASSERT_NOT_NULL(tree);
tree_rb_init(tree, sizeof(int));
tree->print_obj = print_num;
tree->compare = compare_num;