void DFS_stack(Vertex& TestVertex) { vector<bool> v1;//利用数组来表示是否被遍历过并初始化 v1.resize(MAXNODE); for (int i = 0; i < MAXNODE; i++) { v1[i] = false; } stack<int> s1;//利用栈s1来深度遍历图 //访问第一个元素然后入栈 s1.push(0); cout << TestVertex.head[s1.top()].Name << " "; v1[s1.top()] = true; NODE* NodeMove; //开始深度优先遍历 while (!s1.empty()) { NodeMove = TestVertex.head[s1.top()].next; while (NodeMove != NULL) { //如果该结点被遍历过 if (v1[NodeMove->pos] == true) { NodeMove = NodeMove->next; } else//如果没有被遍历过 { v1[NodeMove->pos] = true; cout << TestVertex.head[NodeMove->pos].Name << " "; s1.push(NodeMove->pos); } } s1.pop(); } }