1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| void bfs(Graph *graph,QueueNode* Q,char start,int *visited){ struct AdjNode* adjnode=findNode(graph,start); if(!adjnode){ return; } enqueue(Q,adjnode); while(!isEmpty(Q)){ struct QueueNode* qnode=dequeue(Q); struct AdjNode* node=qnode->node; for(;node;node=node->next){ int index=findNodeIndex(graph,node->vtxname); if(visited[index]!=1){ printf("%c->",graph->list[index]->vtxname); enqueue(Q,findNode(graph,node->vtxname)); visited[index]=1; } } } printf("\n"); } void graphBfs(Graph *graph,char start){ int visited[graph->vtxnum]; memset(visited, 0, sizeof(int) * graph->vtxnum); QueueNode *Q=initQueue(); bfs(graph,Q,start,visited); for(int i=0;i<graph->vtxnum;i++){ if(visited[i]==0){ bfs(graph,Q,graph->list[i]->vtxname,visited); } } }
|