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 35 36 37
| #include "graph.hpp" #include <stack>
void dfs(Node *node) { if (node == nullptr) { return; } stack<Node *> sta; unordered_set<Node *> hashset; sta.push(node); hashset.insert(node); cout << node->value << endl; while (!sta.empty()) { Node *cur = sta.top(); sta.pop(); for (auto &next : cur->nexts) { if (hashset.find(next) == hashset.end()) { sta.push(cur); sta.push(next); hashset.insert(next); cout << next->value << endl; break; } } } }
int main() { }
|