// MT2Q1.cpp #include #include template class Node { public: Node(const T& t=0) : t_(t), marked_(false) {}; T& value() { return t_; } bool& marked() { return marked_; } Node& add(Node* pNode) { children_.push_back(pNode); return *this; } std::vector< Node* >& children() { return children_; } // Compiler generated copy and assignment are shallow. // That makes sense for a Node class. private: std::vector< Node* > children_; T t_; bool marked_; }; template void walkGraph(Node* pNode) { std::cout << "\n " << pNode->value(); pNode->marked() = true; for(size_t i=0; ichildren().size(); ++i) { Node* child = pNode->children()[i]; if(!child->marked()) walkGraph(child); } } void main() { Node n0(0); Node n1(1); Node n2(2); Node n3(3); n0.add(&n2).add(&n1); n2.add(&n1).add(&n3); n3.add(&n0); walkGraph(&n0); std::cout << "\n\n"; }