void BFS(int matrix[][N], int v, int visited[]) { int u; linkqueue *lq; lq = CreateEmptyLinkqueue(); printf("V%d ", v); visited[v] = 1; EnLinkqueue(lq, v); while ( ! EmptyLinkqueue(lq) ) { v = DeLinkqueue(lq); u = FirstAdj(matrix, v); while (u >= 0) { if ( ! visited[u] ) { printf("V%d ", u); visited[u] = 1; EnLinkqueue(lq, u); } u = NextAdj(matrix, v, u); } } return; }
void DFS(VNode G[], int v) { int w; printf("%d", G[v].data); visited[v] = 1; w = FirstAdj(G, v); while (w != -1) { if (visited[w] == 0) DFS(G, w); w = NextAdj(G, v); } }
void DFS(int matrix[][N], int v, int visited[]) { int u; printf("V%d ", v); visited[v] = 1; u = FirstAdj(matrix, v); while (u >= 0) { if ( ! visited[u] ) DFS(matrix, u, visited); u = NextAdj(matrix, v, u); } return; }
// 逻辑估计有问题 void BFS(VNode G[], int v) { int w; LinkQueue *q = NULL; initQueue(q); //visit(v); visited[v] = 1; EnQueue(q, v); while (!emptyQ(q)) { DeQueue(q, &v); w = FirstAdj(G, v); while (w != -1) { if (visited[w] == 0) { EnQueue(q, w); visited[w] = 1; } w = NextAdj(G, v); } } }