mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 11:31:37 +08:00
目前edge打印还是有错误,from和to的顶点不对
This commit is contained in:
parent
c3ed2ab00d
commit
7ad5631aec
40
src/graph.c
40
src/graph.c
@ -625,9 +625,7 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
||||
return false;
|
||||
}
|
||||
|
||||
// add edge
|
||||
if (from_node->edge == NULL)
|
||||
{
|
||||
// from_node add edge
|
||||
struct _graph_edge* new_edge = graph_edge_new();
|
||||
if (new_edge == NULL)
|
||||
{
|
||||
@ -636,45 +634,33 @@ static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t w
|
||||
new_edge->weight = weight;
|
||||
new_edge->target = to_node;
|
||||
|
||||
if (from_node->edge == NULL)
|
||||
{
|
||||
from_node->edge = new_edge;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct _graph_edge* new_edge = graph_edge_new();
|
||||
if (new_edge == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
new_edge->weight = weight;
|
||||
new_edge->target = to_node;
|
||||
|
||||
new_edge->next = from_node->edge->next;
|
||||
from_node->edge->next = new_edge;
|
||||
}
|
||||
|
||||
// if graph is undirected
|
||||
if (to_node->edge == NULL)
|
||||
{
|
||||
to_node->edge = graph_edge_new();
|
||||
if (to_node->edge == NULL)
|
||||
struct _graph_edge* new_edge2 = graph_edge_new();
|
||||
if (new_edge2 == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
to_node->edge->weight = weight;
|
||||
to_node->edge->target = from_node;
|
||||
new_edge2->weight = weight;
|
||||
new_edge2->target = from_node;
|
||||
|
||||
if (to_node->edge == NULL)
|
||||
{
|
||||
to_node->edge = new_edge2;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct _graph_edge* new_edge = graph_edge_new();
|
||||
if (new_edge == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
new_edge->weight = weight;
|
||||
new_edge->target = from_node;
|
||||
|
||||
new_edge->next = to_node->edge->next;
|
||||
to_node->edge->next = new_edge;
|
||||
new_edge2->next = to_node->edge->next;
|
||||
to_node->edge->next = new_edge2;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -92,9 +92,9 @@ void test_graph_add_edge(void)
|
||||
graph->print(graph);
|
||||
|
||||
// test add_edge
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 0));
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 0));
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 0));
|
||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 55));
|
||||
// TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 66));
|
||||
// TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 77));
|
||||
graph->print(graph);
|
||||
|
||||
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||
|
Loading…
Reference in New Issue
Block a user