mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-05 08:26:54 +08:00
思路楼乱了
This commit is contained in:
parent
43b237487a
commit
5e83dc2917
82
src/graph.c
82
src/graph.c
@ -851,7 +851,7 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
struct _graph_node *cur_node = iter->_cur_node;
|
||||
struct _graph_edge *cur_edge = cur_node->edgehead;
|
||||
struct _graph_node *target = NULL;
|
||||
struct _graph_node* node = cur_node;
|
||||
struct _graph_node *node = cur_node;
|
||||
|
||||
queue_t queue = self->queue;
|
||||
|
||||
@ -860,7 +860,7 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
cur_node = self->_head->next;
|
||||
while (cur_node != NULL)
|
||||
{
|
||||
if(cur_node->visited != true)
|
||||
if (cur_node->visited != true)
|
||||
{
|
||||
queue->push(queue, &cur_node);
|
||||
break;
|
||||
@ -878,10 +878,10 @@ const void *graph_iter_next(struct _iterator *iter)
|
||||
while (cur_edge != NULL)
|
||||
{
|
||||
target = cur_edge->target;
|
||||
if(target != NULL && target->visited != true)
|
||||
if (target != NULL && target->visited != true)
|
||||
{
|
||||
queue->push(queue, &target);
|
||||
|
||||
|
||||
// self->print_obj(node->obj);
|
||||
// printf(" -> ");
|
||||
// self->print_obj(target->obj);
|
||||
@ -899,43 +899,75 @@ 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_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)
|
||||
|
||||
// while (!self->stack->empty(self->stack) || cur_node != NULL)
|
||||
do
|
||||
{
|
||||
if (cur_edge != NULL)
|
||||
if(cur_node == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(cur_node->visited == true)
|
||||
{
|
||||
stack->pop(stack, &cur_node);
|
||||
cur_node->visited = true;
|
||||
node = cur_node;
|
||||
break;
|
||||
}
|
||||
|
||||
bool accessible_vertex_exists = false;
|
||||
|
||||
// 1. add cur_node to stack
|
||||
stack->push(stack, &cur_node);
|
||||
|
||||
// 2. find the first unvisited vertex
|
||||
cur_edge = cur_node->edgehead;
|
||||
while(cur_edge != NULL)
|
||||
{
|
||||
target = cur_edge->target;
|
||||
if(target != NULL && target->visited != true)
|
||||
{
|
||||
stack->push(stack, &target);
|
||||
accessible_vertex_exists = true;
|
||||
|
||||
cur_node = target;
|
||||
break;
|
||||
}
|
||||
cur_edge = cur_edge->next;
|
||||
}
|
||||
else
|
||||
|
||||
iterator_t iter = stack->iter(stack);
|
||||
struct _graph_node *temp_node = NULL;
|
||||
while (iter->hasnext(iter))
|
||||
{
|
||||
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");
|
||||
temp_node = *(struct _graph_node **)iter->next(iter);
|
||||
self->print_obj(temp_node->obj);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
self->stack->pop(self->stack, &cur_node);
|
||||
node = cur_node;
|
||||
cur_node = cur_node->next;
|
||||
if(accessible_vertex_exists != true ||
|
||||
cur_edge == NULL)
|
||||
{
|
||||
// if no accessible vertex, pop from stack
|
||||
stack->pop(stack, &node);
|
||||
node->visited = true;
|
||||
|
||||
stack->peek(stack, &cur_node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
iter->_cur_node = node;
|
||||
obj = cur_node->obj;
|
||||
}while(!stack->empty(stack));
|
||||
|
||||
printf("--- end ---\n");
|
||||
iter->_cur_node = cur_node;
|
||||
printf("--- end1 ---\n");
|
||||
obj = node->obj;
|
||||
printf("--- end2 ---\n");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -166,7 +166,7 @@ void test_graph_iter(void)
|
||||
while(iter_vertex->hasnext(iter_vertex))
|
||||
{
|
||||
temp = *(int *)iter_vertex->next(iter_vertex);
|
||||
// graph->print_obj(&temp);
|
||||
printf("temp = %d\n", temp);
|
||||
}
|
||||
|
||||
graph_free(&graph);
|
||||
|
Loading…
Reference in New Issue
Block a user