后续考虑matrix是利用list来实现还是直接函数指针实现。但考虑到用一维指针或者二维指针都有不方便的地方。

This commit is contained in:
建峰 2025-04-27 00:16:44 +08:00
parent 5fd74a4d15
commit 9e270b7425
3 changed files with 12 additions and 21 deletions

View File

@ -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

View File

@ -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;

View File

@ -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;