There is something very unclear to me to when a function should get a pointer or a reference .
Lets say im implementing BFS . here is my implementation :
// Assuming There is a class Node :
class Node {
public:
int val;
bool visited;
list<Node*> neighbours;
};
void BFS (Node* root) {
if(root == NULL) {
return ;
}
queue<Node*> Q;
Q.push(root);
while(!Q.empty()){
Node* temp = Q.front();
Q.pop();
for(list<Node*>::iterator it = root->neighbours.begin() ; it != root->neighbours.end() ; it++){
if((*it)->visited == true) continue;
Q.push((*it));
(*it)->visited = true;
}
if(!Q.empty()){
cout << temp->val << ",";
} else {
cout << temp->val << endl;
}
}
}
My question is : should the function BFS get a pointer or a reference and why ?
also , id love to hear some more comments on the implementation itslef.
Thanks a lot !