mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
后续考虑matrix是利用list来实现还是直接函数指针实现。但考虑到用一维指针或者二维指针都有不方便的地方。
This commit is contained in:
parent
5fd74a4d15
commit
9e270b7425
@ -42,8 +42,9 @@ struct _graph_node
|
|||||||
{
|
{
|
||||||
void* obj;
|
void* obj;
|
||||||
struct _graph_node* next;
|
struct _graph_node* next;
|
||||||
struct _graph_edge* edgehead;
|
|
||||||
bool visited;
|
bool visited;
|
||||||
|
struct _graph_edge* edgehead;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _graph
|
struct _graph
|
||||||
@ -77,22 +78,16 @@ struct _graph
|
|||||||
bool (*del_edge)(struct _graph* self, void* from, void* to);
|
bool (*del_edge)(struct _graph* self, void* from, void* to);
|
||||||
bool (*find_edge)(struct _graph* self, void* from, void* to);
|
bool (*find_edge)(struct _graph* self, void* from, void* to);
|
||||||
|
|
||||||
// traverse
|
bool (*empty)(struct _graph* self);
|
||||||
bool (*dfs)(struct _graph* self, uint32_t idx);
|
bool (*full)(struct _graph* self);
|
||||||
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);
|
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t(*size)(struct _graph* self);
|
uint32_t(*size)(struct _graph* self);
|
||||||
uint32_t(*capacity)(struct _graph* self);
|
uint32_t(*capacity)(struct _graph* self);
|
||||||
bool (*clear)(struct _graph* self);
|
bool (*clear)(struct _graph* self);
|
||||||
bool (*empty)(struct _graph* self);
|
|
||||||
bool (*full)(struct _graph* self);
|
|
||||||
|
|
||||||
// iter
|
// 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
|
// config
|
||||||
compare_fun_t compare; // !!! you have to implement this function
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
14
src/graph.c
14
src/graph.c
@ -455,7 +455,7 @@ static void graph_print(struct _graph *self)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("vertex : \n");
|
printf("\nvertex : \n");
|
||||||
struct _graph_node *cur = self->_head->next;
|
struct _graph_node *cur = self->_head->next;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
@ -470,23 +470,21 @@ static void graph_print(struct _graph *self)
|
|||||||
{
|
{
|
||||||
if (cur->edgehead != NULL)
|
if (cur->edgehead != NULL)
|
||||||
{
|
{
|
||||||
// struct _graph_edge* edge = cur->edgehead->next;
|
|
||||||
struct _graph_edge *edge = cur->edgehead;
|
struct _graph_edge *edge = cur->edgehead;
|
||||||
while (edge != NULL)
|
while (edge != NULL)
|
||||||
{
|
{
|
||||||
struct _graph_node *target = (struct _graph_node *)edge->target;
|
struct _graph_node *target = (struct _graph_node *)edge->target;
|
||||||
|
|
||||||
printf("from ");
|
// printf("from ");
|
||||||
self->print_obj(cur->obj);
|
self->print_obj(cur->obj);
|
||||||
printf(" to ");
|
// printf(" to ");
|
||||||
|
printf("--> ");
|
||||||
self->print_obj(target->obj);
|
self->print_obj(target->obj);
|
||||||
printf(": %d \n", edge->weight);
|
printf(": %d \n", edge->weight);
|
||||||
|
|
||||||
edge = edge->next;
|
edge = edge->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// self->print_obj(cur->obj);
|
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
@ -1059,11 +1057,9 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
|
|
||||||
// others
|
// others
|
||||||
self->from_matrix = NULL;
|
self->from_matrix = NULL;
|
||||||
self->bfs = NULL;
|
|
||||||
self->dfs = NULL;
|
|
||||||
|
|
||||||
self->compare = NULL;
|
self->compare = NULL;
|
||||||
|
|
||||||
// -------------------- debug --------------------
|
// -------------------- debug --------------------
|
||||||
self->print_obj = NULL;
|
self->print_obj = NULL;
|
||||||
self->print = graph_print;
|
self->print = graph_print;
|
||||||
|
@ -103,7 +103,7 @@ void test_graph_add_edge(void)
|
|||||||
// test find_edge
|
// test find_edge
|
||||||
TEST_ASSERT_TRUE(graph->find_edge(graph, &data[0], &data[1]));
|
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[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[4], &data[3]));
|
||||||
TEST_ASSERT_FALSE(graph->find_edge(graph, &data[0], &temp));
|
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[7], &data[6], 87));
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[8], &data[2], 92));
|
||||||
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
// graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
iterator_t iter_vertex = NULL;
|
iterator_t iter_vertex = NULL;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user