Пример псевдокода:
Код: Выделить всё
void BreadthFirstSearch(node* n, void (*func)(node*)){
// do the search stuff
// run the function on each node
func(current_node);
}
void SetColor(node* n, color c){ // set color... }
void SetNumber(node* n, int i){ //set number... }
int main(){
// make the graph...
// set the color...
BreadthFirstSearch(n, SetColor, WHERE DO I PUT THE COLOR PARAMETER?);
Вот урезанная версия моего кода, чтобы вы могли видеть что я пытаюсь сделать:
Код: Выделить всё
void BFS(node* n, void (*func)(node*)){
// make a visited set
std::set visited = std::set();
// make a frontier queue
std::queue frontier = std::queue();
frontier.push(n);
while(!frontier.empty()){
// get current node from frontier
node* current = frontier.front();
frontier.pop();
// add current node to visited set
visited.insert(current);
// get neighbors from current node and add to frontier...
// run the function on the current node
func(current);
}
}
void set_color(node* n){
// hard code the color because I don't know what I'm doing
n->color = (255, 0, 0);
}
int main(){
// do stuff
// run the search with set_color function
BFS(start_node, set_color);
// do more stuff
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... nother-fun
Мобильная версия