Some of the code has been omitted. I am trying to write a DFS function to traverse a maze. It will eventualy add the nodes to a tree that I will traverse once I get the DFS working. Currently it goes into an infinite loop and I cannot figure out why. I based it off of this puedo-code
dfs(graph G)
{
list L = empty
tree T = empty
choose a starting vertex x
search(x)
while(L is not empty)
{
remove edge (v, w) from beginning of L
if w not yet visited
{
add (v, w) to T
search(w)
}
}
}
search(vertex v)
{
visit v
for each edge (v, w)
add edge (v, w) to the beginning of L
}
Any help would be so much appreciated I cannot figure this out for the life of me.
vector<Node*> nodeList;//list of verticies
stack<Edge> L;
void search(Node n) {
n.makeVisited();
vector<Edge> edges = n.getAdjNodeList();
for(int i = 0; i < edges.size(); i++) {
L.push(edges[i]);
edges.erase(edges.begin()+i);
}
}
void DFS(Node start) {
search(start);
while(!L.empty()) {
cerr << "L ISNT EMPTY ";
Edge ed = Edge(L.top().getNode1(), L.top().getNode2());
L.pop();
if((ed.getNode2()->isVisited()) == false) {
//tree.insertAfter(*ed.getNode1(), *ed.getNode2());
search(*ed.getNode2());
} else
return;
}
}
Thanks