mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-18 03:51:35 +08:00
graph的迭代器bfs遍历模式调试通过
This commit is contained in:
parent
98eb005a4a
commit
43706105bb
38
src/graph.c
38
src/graph.c
@ -855,6 +855,20 @@ const void *graph_iter_next(struct _iterator *iter)
|
|||||||
|
|
||||||
queue_t queue = self->queue;
|
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)
|
if (!queue->empty(queue) && node != NULL)
|
||||||
{
|
{
|
||||||
queue->pop(queue, &node);
|
queue->pop(queue, &node);
|
||||||
@ -868,33 +882,15 @@ const void *graph_iter_next(struct _iterator *iter)
|
|||||||
{
|
{
|
||||||
queue->push(queue, &target);
|
queue->push(queue, &target);
|
||||||
|
|
||||||
self->print_obj(node->obj);
|
// self->print_obj(node->obj);
|
||||||
printf(" -> ");
|
// printf(" -> ");
|
||||||
self->print_obj(target->obj);
|
// self->print_obj(target->obj);
|
||||||
}
|
}
|
||||||
cur_edge = cur_edge->next;
|
cur_edge = cur_edge->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
cur_node = node;
|
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;
|
iter->_cur_node = cur_node;
|
||||||
obj = cur_node->obj;
|
obj = cur_node->obj;
|
||||||
|
@ -141,25 +141,23 @@ void test_graph_iter(void)
|
|||||||
// graph->print(graph);
|
// graph->print(graph);
|
||||||
|
|
||||||
// test add_edge
|
// 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[0], &data[2], 13));
|
||||||
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 24));
|
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[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));
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
graph->print(graph);
|
graph->print(graph);
|
||||||
|
|
||||||
printf("test iter_vertex: \n");
|
|
||||||
iterator_t iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
|
iterator_t iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
|
||||||
TEST_ASSERT_NOT_NULL(iter_vertex);
|
TEST_ASSERT_NOT_NULL(iter_vertex);
|
||||||
|
|
||||||
printf("start\n");
|
|
||||||
while(iter_vertex->hasnext(iter_vertex))
|
while(iter_vertex->hasnext(iter_vertex))
|
||||||
{
|
{
|
||||||
printf("next : ");
|
|
||||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||||
printf("temp = %d\n", temp);
|
graph->print_obj(&temp);
|
||||||
}
|
}
|
||||||
printf("end\n");
|
|
||||||
|
|
||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
|
Loading…
Reference in New Issue
Block a user