I'm trying to implement a graph using adjacency list, but in the A_List class function called ,,AddGraphEdge" , there's a problem ,,Exception thrown: read access violation.this was 0x20." It occurs in the list.cpp function ,,AddEdge" when I do if(head==nullptr). I just simply check if head is null. When i delete this statement in place when I assign head=new_edge it tells me the same problem. It works fine if I'm using only normal list, and i create an list object in main and call AddEdge function. I don't know why calling this function in another causes problems.
class Graph
{
protected:
int _Vertices;
int _Edges;
int _Density;
public:
Graph() : _Vertices(0), _Edges(0), _Density(0) {};
Graph(const int& Vertices,const int& Edges, const int& Density) : _Vertices(Vertices), _Density(Density), _Edges(Edges) {};
void set_Vertices(const int& Vertices) { _Vertices=Vertices; };
int get_Vertices() { return _Vertices; };
void set_Edges(const int& Edges) { _Edges=Edges; };
int get_Edges() { return _Edges; };
virtual void DisplayGraph() = 0;
virtual void Get_Random_Graph() = 0;
};
class Edge
{
private:
int _StartVertice;
int _EndVertice;
int _weight;
Edge* _NextEdge;
public:
Edge():_StartVertice(0), _EndVertice(0), _weight(0), _NextEdge(nullptr) {};
Edge(const int& StartVertice, const int& EndVertice, const int& weight) : _StartVertice(StartVertice), _EndVertice(EndVertice), _weight(weight), _NextEdge(nullptr) {};
const int& get_StartVertice() const { return _StartVertice; };
const int& get_EndVertice() const { return _EndVertice; };
const int& get_Weight() const { return _weight; };
Edge* get_NextEdge() { return _NextEdge; };
void set_NextEdge(Edge* NextEdge) { _NextEdge = new Edge; _NextEdge = NextEdge; };
void set_EdgeValues(const int& StartVertice, const int& EndVertice, const int& weight)
{
_StartVertice = StartVertice;
_EndVertice = EndVertice;
_weight = weight;
};
};
#include "Edge.h"
class List
{
private:
int ListSize;
Edge* head;
public:
~List() {};
List() : ListSize(0), head(nullptr) {};
const Edge* get_head() { return head; };
const int get_ListSize() { return ListSize; };
void AddEdge(const int& StartVertice, const int& EndVertice, const int& weigth);
void DisplayList();
};
#include "List.h"
#include "Graph.h"
class A_List: public Graph
{
private:
List* _Adj_List;
public:
A_List() : Graph() { _Adj_List = nullptr; };
A_List(const int& Vertices, const int& Edges, const int& Density) : Graph(Vertices, Edges, Density) { _Adj_List = new List[Vertices]; };
virtual void DisplayGraph() override;
virtual void Get_Random_Graph() override;
void AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth);
};
#include "List.h"
#include <iostream>
void List::AddEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
Edge* new_edge = new Edge;
new_edge->set_EdgeValues(StartVertice, EndVertice, weigth);
if (head == nullptr)
{
head = new_edge;
}
else
{
Edge* tmp = head;
head = new_edge;
new_edge->set_NextEdge(tmp);
}
ListSize++;
}
void List::DisplayList()
{
Edge* tmp = head;
while (head->get_NextEdge() != nullptr)
{
std::cout << tmp->get_StartVertice() << tmp->get_Weight() << tmp->get_EndVertice() << std::endl;
tmp = tmp->get_NextEdge();
}
std::cout << tmp->get_StartVertice() << tmp->get_Weight() << tmp->get_EndVertice() << std::endl;
}
#include "Adjacency_List.h"
void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
_Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
}
void A_List::DisplayGraph()
{int Vertices = A_List::get_Edges();
for (int i = 0; i < Vertices; i++)
{
_Adj_List[i].DisplayList();
}
}
void A_List::Get_Random_Graph()
{
}
#include <iostream>
#include "Adjacency_List"
int main()
{
A_List Graph_list;
Graph_list.AddGraphEdge(4,3,2);
Graph_list.DisplayGraph();
return 0;
}
The constructor sets _Adj_List to nullptr:
A_List() : Graph() { _Adj_List = nullptr; };
And now it gets dereferenced and used (it's still nullptr):
void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
_Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
}
But it might not be the only bug, as it's quite messy code