I am having a problem writing the last part of my code for this program.
There is a DFS function that I need to write that is included in the header file of the graph.h my program.
Here is the sample of code that my instructor gave me.
template<class VertexType>
void DepthFirstSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex)
// Assumes VertexType is a type for which the “==“ and "<<"
// operators are defined
{
using namespace std;
StackType<VertexType> stack;
QueType<VertexType> vertexQ;
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
stack.Push(startVertex);
do
{
stack.Pop(vertex);
if (vertex == endVertex)
{
cout << vertex);
found = true;
}
else
{
if (!graph.IsMarked(vertex))
{
graph.MarkVertex(vertex);
cout << vertex;
graph.GetToVertices(vertex, vertexQ);
while (!vertexQ.IsEmpty())
{
vertexQ.Dequeue(item);
if (!graph.IsMarked(item))
stack.Push(item);
}
}
}
} while (!stack.IsEmpty() && !found);
if (!found)
cout << "Path not found." << endl;
}
But when I try to write it I keep getting errors when it compiles.
This is his email to us the students on the DFS function.
DepthFirstSearch() tasks
- DFS requires one stack (static), one queue (static), and one queue (dynamic) to hold the path. Instead of printing out each vertex on the path, DFS queues them by name in the dynamic queue. When DFS ends, it must return the address of the dynamic queue to main() so that main() can complete its tasks. Otherwise, the DFS algorithm follows that presented in the book/notes.
This function is in the graph.cpp file.
I have attached all the files that are used in this program.
What an I doing wrong on this function?