graph的迭代器bfs遍历模式调试通过

This commit is contained in:
建峰 2025-04-26 15:12:43 +08:00
parent 98eb005a4a
commit 43706105bb
2 changed files with 22 additions and 28 deletions

View File

@ -855,6 +855,20 @@ const void *graph_iter_next(struct _iterator *iter)
queue_t queue = self->queue;
if (queue->empty(queue))
{
cur_node = self->_head->next;
while (cur_node != NULL)
{
if(cur_node->visited != true)
{
queue->push(queue, &cur_node);
break;
}
cur_node = cur_node->next;
}
}
if (!queue->empty(queue) && node != NULL)
{
queue->pop(queue, &node);
@ -868,33 +882,15 @@ const void *graph_iter_next(struct _iterator *iter)
{
queue->push(queue, &target);
self->print_obj(node->obj);
printf(" -> ");
self->print_obj(target->obj);
// self->print_obj(node->obj);
// printf(" -> ");
// self->print_obj(target->obj);
}
cur_edge = cur_edge->next;
}
cur_node = node;
}
else
{
// if queue is empty, find next unvisited node
cur_node = self->_head->next;
while (cur_node != NULL)
{
if(cur_node->visited != true)
{
break;
}
cur_node = cur_node->next;
}
if(cur_node != NULL)
{
queue->push(queue, &cur_node);
iter->_cur_node = cur_node;
}
}
iter->_cur_node = cur_node;
obj = cur_node->obj;

View File

@ -141,25 +141,23 @@ void test_graph_iter(void)
// graph->print(graph);
// test add_edge
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[5], 12));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 12));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 13));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[5], &data[6], 67));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[7], &data[6], 24));
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[9], &data[2], 67));
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
graph->print(graph);
printf("test iter_vertex: \n");
iterator_t iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
TEST_ASSERT_NOT_NULL(iter_vertex);
printf("start\n");
while(iter_vertex->hasnext(iter_vertex))
{
printf("next : ");
temp = *(int *)iter_vertex->next(iter_vertex);
printf("temp = %d\n", temp);
graph->print_obj(&temp);
}
printf("end\n");
graph_free(&graph);
TEST_ASSERT_NULL(graph);