The following code is part of a bigger project in which I simulate a non-directed graph using adjacency lists. The code has no compilation errors. When I use in my main function (which is not provided below) the InserEdge function for the first time (to insert e.g the edge <1,3>) then there is no problem. But when I try to insert a second edge with the same initial node (e.g <1,6>) then the program fails. More particulartly, when I am debugging the code, the while loop in InsertEdge function, seems never to end, a fact which is not logical and I can't figure out why, as I initialize all my adj pointers to NULL. I know I could use STL library but I am not very familiar with.
I would appreciate any help, thanks in advance!
I would appreciate any help, thanks in advance!
Code:
class Edge;
class Node
{
public:
int id;
bool visited;
Edge* adj;
int distance;
Node* previous;
Node(int);
~Node();
};
class Edge
{
public:
Node* initial; //first node
Node* next; //secong node of the edge
Edge* adj; //used to point the next edge in the adjacency list
int w;
Edge(Node*, Node*, int);
~Edge();
bool Connects(Node* node1, Node* node2)
{
return((node1==this->initial&&node2==this->next)||(node1==this->next&&node2==this->initial));
}
};
Node::Node(int a)
{
id=a;
visited=0;
adj=NULL; // used to point the first edge inserted in the adjacency list with
// the first node to be node a
previous=NULL;
distance=INT_MAX;
}
Node::~Node()
{
}
Edge::Edge(Node* a, Node* b, int weight)
{
initial=a;
next=b;
adj=NULL;
w=weight;
}
Edge::~Edge()
{
}
class MyGraph
{
public:
int Maxnodes;
int count;
int m;
Edge* Akmes[]; // stores all the edges inserted in the graph
Node* List[]; // stores all the nodes of the graph. Every node i has a pointer (List[i]->adj)
// pointing to a queue of edges. The common of all these edges is that they
// have as initial node the same node i.
Node* remaining[];
MyGraph(int);
~MyGraph();
Node* findnode(int);
void InsertEdge(int,int,int);
};
MyGraph::MyGraph(int size)
{
Maxnodes=size;
for (int i=1;i<=Maxnodes;i++)
{
List[i-1]=new Node(i);
}
m=0;
count=0;
}
MyGraph::~MyGraph()
{
}
Node* MyGraph::findnode(int a)
{
Node* x;
for (int i=0;i<Maxnodes;i++)
{
if(List[i]->id==a)
x=List[i];
}
return x;
}
void MyGraph::InsertEdge(int x, int y, int w)
{
Node* a=findnode(x);
Node* b=findnode(y);
Edge* akmi=new Edge(a,b,w); // I insert edge <a,b> in the graph
Akmes[count]=akmi;
count++;
Node* p;
Node* q;
Edge* l;
p=List[x-1];
l=p->adj;
if (l==NULL) // if there is no edge inserted starting with node x
p->adj=akmi; // then insert this edge, and define the pointer of node x to point
// to this first edge
else
{
while (l!=NULL) // else insert the new edge after the last edge that has been inserted in
l=l->adj; // the graph with the same initial node x. Define the pointer of the last
// edge to point to the new last edge.
l=akmi;
}
Edge* akmi2=new Edge(b,a,w); // I also insert edge <b,a> as my graph is undirected
q=List[y-1];
l=q->adj;
if (l==NULL)
q->adj=akmi2;
else
{
while (l!=NULL)
l=l->adj;
l=akmi2;
}
}
Comment