I have search but can't find a anything specific to my case.
I wanna implement a graph in c++ using linked list only. My structure is as below:
class Vertex
{
public:
Vertex(std::string name = "none");
private:
std::string name;
Edge *edges;
Vertex *next;
Runnable *runnables;
};
class Edge
{
public:
Edge();
private:
Vertex *connectsTo;
Edge *next;
};
class TaskGraph{
public:
TaskGraph();
private:
Vertex *head;
};
I have a method to add vertices to the graph void addVertex(TaskGraph* taskGraph, const std::string&);
This method works well as I can print out all the vertices in the graph. On the otherhand, to add directed edge, I did something like this:
void MyGraph::addEdge(TaskGraph* taskGraph, const string& src, const string& dest){
//get to the source vertex: this is ok
//get to the destination vertex: this is also ok
//add edge : the problem is here // s is source vertex while d is destination vertex.
Edge *e = new Edge;
e->connectsTo = d;
if(s->edges == 0){
s->edges = e;
}else{
s->edges->next = e;
}
}
After adding say 5 edges, only two is actually added (i.e. the first and list edges, others are being replaced). I figure out it's because of this line: "s->edges->next = e;" but can't figure out how to implement it correctly. Please help!!!
Thanks
s?Edge* edgesmember of theVertexclass as a pointer to a singleEdgeobject. That doesn't seem like it will work if you want to support a graph where vertices can be adjacent to more than one other vertex.