AdjMatrix

I'm trying to implement adj matrix, not knowing dimension.
Target is to clear a flag using method clearVisited() of my class Component

1
2
3
4
5
6
7
8
9
10
11
  void Circuit :: clearVisit(Component* node){
    node->clearVisited();
    list<Component*>* AdjList = new list<Component*>;
    list<Component*>::iterator i;
    if(node->leftChild()!= nullptr) AdjList->push_front(node->leftChild());
    if(node->rightChild()!= nullptr) AdjList->push_front(node->rightChild());
    for(i=AdjList->begin();i!=AdjList->end();++i){
        //??
        clearVisit(/*?*/)
    }
}

in ?? I want to put I have to explore node if it is visited (I have also function Component::getVisited())
if(AdjList->front()->getVisited()) clearVisit(AdjList->front());
ii right?
it doesn't work.
Anybody could help me?
Tell us more about the Component graph.
- I see that it's a circuit. Does that mean the graph can have cycles?
- Do you have a separate collection of the nodes in the graph? That would make it trivial.
I see that it's a circuit. Does that mean the graph can have cycles?


Yes, it could have.

Do you have a separate collection of the nodes in the graph? That would make it trivial.


No, I have only the head of the circuit (root), that's for me is the output of the circuit(graph)
I don't know how to use iterators to take the elements of Adjlist
ok. I'm not understanding if it works.
1
2
3
4
5
6
7
8
9
10
void Circuit :: clearVisit(Component* node){
    node->clearVisited();
    list<Component*>* AdjList = new list<Component*>;
    list<Component*>::iterator it;
    if(node->leftChild()!= nullptr) AdjList->push_front(node->leftChild());
    if(node->rightChild()!= nullptr) AdjList->push_front(node->rightChild());
    for(it=AdjList->begin();it!=AdjList->end();++it){
        if ((*it)->getVisited()) clearVisit(AdjList->front());        
    }
}


This code access to all nodes to do node->clear visited ?
Normally you use a "visited" flag to indicate that you have processed a node. In this case, it's already set and you want clear it.

1
2
3
4
5
6
7
8
9
10
// Clear the visited flag. This assumes that upon the first call, the visited flag is set in all nodes.
void Circuit :: clearVisit(Component* node){
    if (node == nullptr) return;      // empty node
    if (!node->getVisited()) return;  // flag was already cleared by a recursive call
    node->clearVisited();             // Clear the flag on this node. This line and the line
                                      // above prevent infinite recursion
                                      // when the circuit has loops
    clearVisited(node->leftChild);    // recursive call to (possibly null) left child
    clearVisited(node->rightChild);   // recursive call to (possibly null) right child
}
I'm in trouble..

I want to clear visited flag for all nodes, but I don't know it they are all visited.
Your code "touch" all nodes and clear visited flags?
Can you show the full code and the full problem description if this is a homework assignment. Sometimes students omit details that turn out to be important.
Topic archived. No new replies allowed.