mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
无边的节点没有被压到栈中去
This commit is contained in:
parent
43706105bb
commit
43b237487a
37
src/graph.c
37
src/graph.c
@ -899,6 +899,43 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
case GRAPH_DFS:
|
||||
{
|
||||
// self->stack->push(self->stack, iter->_cur_node);
|
||||
struct _graph_node* cur_node = self->_iter._cur_node;
|
||||
struct _graph_node* node = NULL;
|
||||
struct _graph_edge *cur_edge = cur_node->edgehead;
|
||||
struct _graph_node *target = NULL;
|
||||
|
||||
stack_t stack = self->stack;
|
||||
|
||||
while (!self->stack->empty(self->stack) || cur_node != NULL)
|
||||
{
|
||||
if (cur_edge != NULL)
|
||||
{
|
||||
target = cur_edge->target;
|
||||
if(target != NULL && target->visited != true)
|
||||
{
|
||||
stack->push(stack, &target);
|
||||
}
|
||||
cur_edge = cur_edge->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
iterator_t iter = stack->iter(stack);
|
||||
while(iter->hasnext(iter))
|
||||
{
|
||||
struct _graph_node* g_node = *(struct _graph_node **)iter->next(iter);
|
||||
self->print_obj(g_node->obj);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
self->stack->pop(self->stack, &cur_node);
|
||||
node = cur_node;
|
||||
cur_node = cur_node->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
iter->_cur_node = node;
|
||||
obj = cur_node->obj;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -146,17 +146,27 @@ void test_graph_iter(void)
|
||||
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_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 = graph->iter(graph, GRAPH_BFS, &data[0]);
|
||||
iterator_t iter_vertex = NULL;
|
||||
|
||||
iter_vertex = graph->iter(graph, GRAPH_BFS, &data[0]);
|
||||
TEST_ASSERT_NOT_NULL(iter_vertex);
|
||||
while(iter_vertex->hasnext(iter_vertex))
|
||||
{
|
||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||
graph->print_obj(&temp);
|
||||
// graph->print_obj(&temp);
|
||||
}
|
||||
|
||||
iter_vertex = graph->iter(graph, GRAPH_DFS, &data[0]);
|
||||
TEST_ASSERT_NOT_NULL(iter_vertex);
|
||||
while(iter_vertex->hasnext(iter_vertex))
|
||||
{
|
||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||
// graph->print_obj(&temp);
|
||||
}
|
||||
|
||||
graph_free(&graph);
|
||||
|
Loading…
Reference in New Issue
Block a user