From 4c68d1abdcb4f145e57a6b77a4dad5628d4441ef Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 5 Sep 2024 16:13:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=81=8D=E5=8E=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/graph.h | 3 +++ src/graph.c | 42 ++++++++++++++++++++++++++++++++++++++++++ test/test_graph.c | 4 ++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/include/graph.h b/include/graph.h index ce6dd65..f18669e 100644 --- a/include/graph.h +++ b/include/graph.h @@ -43,6 +43,9 @@ struct _graph { bool (*empty)(struct _graph *self); bool (*full)(struct _graph *self); + bool (*dfs)(struct _graph *self, uint32_t idx); + bool (*bfs)(struct _graph *self, uint32_t idx); + // bool (*clear)(struct _graph *self); void (*destory)(struct _graph *self); diff --git a/src/graph.c b/src/graph.c index add26d2..f654c7c 100644 --- a/src/graph.c +++ b/src/graph.c @@ -9,6 +9,8 @@ * */ #include "graph.h" +#include "queue.h" +#include "stack.h" static uint32_t graph_size(struct _graph* self) { @@ -126,6 +128,44 @@ static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, u return true; } +static bool graph_bfs(struct _graph *self, uint32_t idx) +{ + if(self == NULL || self->_head == NULL) + { + return false; + } + + if(idx >= self->_size || idx >= self->_capacity) + { + return false; + } + + queue_t queue = queue_new(); + queue_init(queue, sizeof(int)); +#if 0 + queue->push(queue, &node); + while (!queue->empty(queue)) + { + queue->pop(queue, &node); + if (node->left != NULL) + { + queue->push(queue, &node->left); + } + if (node->right != NULL) + { + queue->push(queue, &node->right); + } + } +#endif + queue_free(&queue); + return true; +} + +static bool graph_dfs(struct _graph *self, uint32_t idx) +{ + +} + static void graph_init2(struct _graph *self) { if(self == NULL || self->_head == NULL) @@ -169,6 +209,8 @@ graph_t graph_new2(uint32_t obj_size, uint32_t capacity) graph->clear = graph_clear; graph->from_matrix = graph_from_matrix; + graph->bfs = graph_bfs; + graph->dfs = graph_dfs; graph->print_obj = NULL; graph->print = graph_print; diff --git a/test/test_graph.c b/test/test_graph.c index 390abff..a3e7f2d 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -47,9 +47,9 @@ void test_graph_from_matrix(void) TEST_ASSERT_NOT_NULL(graph); graph->print_obj = print_num; - graph->print(graph); + // graph->print(graph); graph->from_matrix(graph, vertexs, matrix, size); - graph->print(graph); + // graph->print(graph); graph_free(&graph); TEST_ASSERT_NULL(graph);