-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDFS.cpp
More file actions
25 lines (23 loc) · 841 Bytes
/
DFS.cpp
File metadata and controls
25 lines (23 loc) · 841 Bytes
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
#include "DFS.h"
#include <iostream>
using namespace std;
void DFS(ALGraph G, int v) {
SqStack S;
InitStack(S); // 初始化栈
Push(S, v); // 入栈v
while (!StackEmpty(S)) { // 栈不空
int k;
Pop(S, k); // 出栈,元素k出栈
if (!visited[k]) {
cout << k; // 访问节点k
visited[k] = true;
ArcNode *p = G.vertices[k].firstarc; // p指向k的第一条边
while (p != NULL) {
int w = p->adjvex; // w是k的邻接点
if (!visited[w]) Push(S, w); // 如果w未被访问,则入栈
p = p->nextarc; // p指向下一条边
}
}
}
cout << endl;
}