深度优先搜索非递归实现

void DFS(AdjList G, int v0) {
    InitStack(&S);
    Push(&S, v0);
    while (!IsEmpty(S)) {
        Pop(&S, &v);
        if (!visited[v])
        {
            visit(v);
            visited[v] = true;
            for (w = FirstNeighbor(G, v); w >= 0; w = NextNeightbor(G, v, w)) {
                if (!visited[w])
                    Push(&S, w);
            }
        }
    }
}

 

posted @ 2021-08-21 17:29  #Lorraine#  阅读(39)  评论(0编辑  收藏  举报