From 9e270b7425300806192eeac58adb277af7792846 Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 27 Apr 2025 00:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=BB=AD=E8=80=83=E8=99=91matrix?= =?UTF-8?q?=E6=98=AF=E5=88=A9=E7=94=A8list=E6=9D=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=BF=98=E6=98=AF=E7=9B=B4=E6=8E=A5=E5=87=BD=E6=95=B0=E6=8C=87?= =?UTF-8?q?=E9=92=88=E5=AE=9E=E7=8E=B0=E3=80=82=E4=BD=86=E8=80=83=E8=99=91?= =?UTF-8?q?=E5=88=B0=E7=94=A8=E4=B8=80=E7=BB=B4=E6=8C=87=E9=92=88=E6=88=96?= =?UTF-8?q?=E8=80=85=E4=BA=8C=E7=BB=B4=E6=8C=87=E9=92=88=E9=83=BD=E6=9C=89?= =?UTF-8?q?=E4=B8=8D=E6=96=B9=E4=BE=BF=E7=9A=84=E5=9C=B0=E6=96=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/graph.h | 15 +++++---------- src/graph.c | 14 +++++--------- test/test_graph.c | 4 ++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/include/graph.h b/include/graph.h index 40242e0..c6a1eee 100644 --- a/include/graph.h +++ b/include/graph.h @@ -42,8 +42,9 @@ struct _graph_node { void* obj; struct _graph_node* next; - struct _graph_edge* edgehead; + bool visited; + struct _graph_edge* edgehead; }; struct _graph @@ -77,22 +78,16 @@ struct _graph bool (*del_edge)(struct _graph* self, void* from, void* to); bool (*find_edge)(struct _graph* self, void* from, void* to); - // traverse - bool (*dfs)(struct _graph* self, uint32_t idx); - bool (*bfs)(struct _graph* self, uint32_t idx); - - bool (*get)(struct _graph* self, uint32_t idx, void* obj); - bool (*remove)(struct _graph* self, uint32_t idx); + bool (*empty)(struct _graph* self); + bool (*full)(struct _graph* self); // base uint32_t(*size)(struct _graph* self); uint32_t(*capacity)(struct _graph* self); bool (*clear)(struct _graph* self); - bool (*empty)(struct _graph* self); - bool (*full)(struct _graph* self); // iter - iterator_t (*iter)(struct _graph* self, enum _graph_search search_type, void *obj); + iterator_t (*iter)(struct _graph* self, enum _graph_search search, void *obj); // config compare_fun_t compare; // !!! you have to implement this function diff --git a/src/graph.c b/src/graph.c index 545f334..080b2b3 100644 --- a/src/graph.c +++ b/src/graph.c @@ -455,7 +455,7 @@ static void graph_print(struct _graph *self) return; } - printf("vertex : \n"); + printf("\nvertex : \n"); struct _graph_node *cur = self->_head->next; while (cur != NULL) { @@ -470,23 +470,21 @@ static void graph_print(struct _graph *self) { if (cur->edgehead != NULL) { - // struct _graph_edge* edge = cur->edgehead->next; struct _graph_edge *edge = cur->edgehead; while (edge != NULL) { struct _graph_node *target = (struct _graph_node *)edge->target; - printf("from "); + // printf("from "); self->print_obj(cur->obj); - printf(" to "); + // printf(" to "); + printf("--> "); self->print_obj(target->obj); printf(": %d \n", edge->weight); edge = edge->next; } } - - // self->print_obj(cur->obj); cur = cur->next; } printf("\n"); @@ -1059,11 +1057,9 @@ static bool graph_init(struct _graph *self, uint32_t obj_size) // others self->from_matrix = NULL; - self->bfs = NULL; - self->dfs = NULL; + self->compare = NULL; - // -------------------- debug -------------------- self->print_obj = NULL; self->print = graph_print; diff --git a/test/test_graph.c b/test/test_graph.c index dd66780..0d2b43c 100644 --- a/test/test_graph.c +++ b/test/test_graph.c @@ -103,7 +103,7 @@ void test_graph_add_edge(void) // test find_edge TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1])); TEST_ASSERT_TRUE(graph->find_edge(graph, &data[1], &data[3])); - TEST_ASSERT_TRUE(graph->find_edge(graph, &data[6], &data[5])); // undirected graph + // TEST_ASSERT_TRUE(graph->find_edge(graph, &data[6], &data[5])); // undirected graph TEST_ASSERT_FALSE(graph->find_edge(graph, &data[4], &data[3])); TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &temp)); @@ -149,7 +149,7 @@ void test_graph_iter(void) TEST_ASSERT_TRUE(graph->add_edge(graph, &data[7], &data[6], 87)); TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92)); TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0)); - // graph->print(graph); + graph->print(graph); iterator_t iter_vertex = NULL;