mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
实测queue入队和出队元素为结构体通过
This commit is contained in:
parent
6500d9dac4
commit
785482d7fb
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -10,7 +10,7 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/build/release/bin/test.exe",
|
"program": "${workspaceFolder}/build/release/bin/test.exe",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": true,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
"environment": [],
|
"environment": [],
|
||||||
"externalConsole": false,
|
"externalConsole": false,
|
||||||
|
37
src/tree.c
37
src/tree.c
@ -9,6 +9,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
#if RAVLTREE == 1
|
#if RAVLTREE == 1
|
||||||
// function declare
|
// function declare
|
||||||
@ -1681,7 +1682,43 @@ void tree_avl_postorder(struct _tree* self, struct _tree_node* root)
|
|||||||
// traversal breadth
|
// traversal breadth
|
||||||
void tree_avl_breadth(struct _tree* self, struct _tree_node* root)
|
void tree_avl_breadth(struct _tree* self, struct _tree_node* root)
|
||||||
{
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
struct _tree_node* node = self->_root;
|
||||||
|
queue_t queue = queue_new();
|
||||||
|
queue_init(queue, self->_obj_size);
|
||||||
|
|
||||||
|
if(node != NULL)
|
||||||
|
{
|
||||||
|
queue->push(queue, node);
|
||||||
|
while(!queue->empty(queue))
|
||||||
|
{
|
||||||
|
queue->pop(queue, node);
|
||||||
|
if(!self->_right_priority)
|
||||||
|
{
|
||||||
|
if(node->left != NULL)
|
||||||
|
{
|
||||||
|
queue->push(queue, node->left);
|
||||||
|
}
|
||||||
|
if(node->right != NULL)
|
||||||
|
{
|
||||||
|
queue->push(queue, node->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(node->right != NULL)
|
||||||
|
{
|
||||||
|
queue->push(queue, node->right);
|
||||||
|
}
|
||||||
|
if(node->left != NULL)
|
||||||
|
{
|
||||||
|
queue->push(queue, node->left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self->print_obj(node->obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue_free(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tree_avl_init(struct _tree *self, uint32_t obj_size)
|
bool tree_avl_init(struct _tree *self, uint32_t obj_size)
|
||||||
|
@ -38,8 +38,8 @@ int main()
|
|||||||
// test_list();
|
// test_list();
|
||||||
// test_stack();
|
// test_stack();
|
||||||
// test_deque();
|
// test_deque();
|
||||||
// test_queue();
|
test_queue();
|
||||||
test_tree();
|
// test_tree();
|
||||||
|
|
||||||
// rbtree_test();
|
// rbtree_test();
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ static void test_queue_char(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("----- after pop -----\n");
|
printf("----- after pop -----\n");
|
||||||
for (i = 0; i < len + 1; i++)
|
while(!queue.empty(&queue))
|
||||||
{
|
{
|
||||||
if (true == queue.pop(&queue, &temp))
|
if (true == queue.pop(&queue, &temp))
|
||||||
{
|
{
|
||||||
@ -163,15 +163,31 @@ static void test_queue_char(void)
|
|||||||
|
|
||||||
printf("size = %2d\n", queue.size(&queue));
|
printf("size = %2d\n", queue.size(&queue));
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
|
||||||
|
if (true == queue.pop(&queue, &temp))
|
||||||
|
{
|
||||||
|
printf("pop = ");
|
||||||
|
queue.print_obj(&temp);
|
||||||
|
|
||||||
|
if (true == queue.front(&queue, &temp))
|
||||||
{
|
{
|
||||||
printf("pop failed! because it is empty\n");
|
printf("front = ");
|
||||||
|
queue.print_obj(&temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (queue.empty(&queue))
|
if (queue.back(&queue, &temp))
|
||||||
{
|
{
|
||||||
printf("----- empty -----\n");
|
printf("back = ");
|
||||||
|
queue.print_obj(&temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("size = %2d\n", queue.size(&queue));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queue.empty(&queue))
|
||||||
|
{
|
||||||
|
printf("----- empty -----\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("----- print -----\n");
|
printf("----- print -----\n");
|
||||||
@ -181,8 +197,99 @@ static void test_queue_char(void)
|
|||||||
queue.destory(&queue);
|
queue.destory(&queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_queue_struct(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
struct _student data[] = {
|
||||||
|
{"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004},
|
||||||
|
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||||
|
};
|
||||||
|
struct _student temp;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||||
|
|
||||||
|
queue_t queue = queue_new();
|
||||||
|
queue_init(queue, sizeof(struct _student));
|
||||||
|
queue->print_obj = print_struct;
|
||||||
|
|
||||||
|
printf("\n\n----- test_queue_struct -----\n");
|
||||||
|
|
||||||
|
printf("----- after push-----\n");
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(queue->push(queue, &data[i]))
|
||||||
|
{
|
||||||
|
queue->front(queue, &temp);
|
||||||
|
printf("front = ");
|
||||||
|
queue->print_obj(&temp);
|
||||||
|
|
||||||
|
queue->back(queue, &temp);
|
||||||
|
printf("\tback = ");
|
||||||
|
queue->print_obj(&temp);
|
||||||
|
|
||||||
|
printf("\tsize = %2d\n", queue->size(queue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("----- print -----\n");
|
||||||
|
queue->print(queue);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
queue->clear(queue);
|
||||||
|
if (queue->empty(queue))
|
||||||
|
{
|
||||||
|
printf("----- empty -----\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----- push -----\n");
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----- after pop -----\n");
|
||||||
|
while(!queue->empty(queue))
|
||||||
|
{
|
||||||
|
if (true == queue->pop(queue, &temp))
|
||||||
|
{
|
||||||
|
printf("pop = ");
|
||||||
|
queue->print_obj(&temp);
|
||||||
|
|
||||||
|
if (true == queue->front(queue, &temp))
|
||||||
|
{
|
||||||
|
printf("front = ");
|
||||||
|
queue->print_obj(&temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queue->back(queue, &temp))
|
||||||
|
{
|
||||||
|
printf("back = ");
|
||||||
|
queue->print_obj(&temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("size = %2d\n", queue->size(queue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!queue->pop(queue, &temp))
|
||||||
|
{
|
||||||
|
printf("----- pop failed! -----\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queue->empty(queue))
|
||||||
|
{
|
||||||
|
printf("----- empty -----\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----- print -----\n");
|
||||||
|
queue->print(queue);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
queue_free(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_queue(void)
|
void test_queue(void)
|
||||||
{
|
{
|
||||||
test_queue_num();
|
test_queue_num();
|
||||||
test_queue_char();
|
test_queue_char();
|
||||||
|
test_queue_struct();
|
||||||
}
|
}
|
||||||
|
@ -299,6 +299,10 @@ void test_tree_num(void)
|
|||||||
tree->postorder(tree, tree->_root);
|
tree->postorder(tree, tree->_root);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
printf("----- breadth -----\n");
|
||||||
|
tree->breadth(tree, tree->_root);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
printf("----- right priority -----\n");
|
printf("----- right priority -----\n");
|
||||||
tree->order(tree, true);
|
tree->order(tree, true);
|
||||||
|
|
||||||
@ -314,6 +318,10 @@ void test_tree_num(void)
|
|||||||
tree->postorder(tree, tree->_root);
|
tree->postorder(tree, tree->_root);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
printf("----- breadth -----\n");
|
||||||
|
tree->breadth(tree, tree->_root);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
tree_free(tree);
|
tree_free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user