Hi guys, can someone explain me why to use a double pointer in the class graph?
I really don't understand this line : this->array = new AdjList*[Algorithme::nb]
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
class AdjListNode {
public:
AdjListNode(char dest[6], float weight); // constructor
char dest[6];
float weight;
class AdjListNode* next;
};
class AdjList {
public:
AdjListNode* head;
};
class Graph {
public:
Graph();
int V;
AdjList** array;
};
Graph::Graph() {
this->V = Algorithme::nb;
this->array = new AdjList*[Algorithme::nb];
for (int i = 0; i < Algorithme::nb; ++i) {
this->array[i] = new AdjList();
this->array[i]->head = NULL;
}
}
The double pointer is used because that variable is meant to point to an array of pointers. The line you were having trouble with (this->array = new AdjList*[Algorithme::nb];) allocates an array of AdjList* pointers of length Algorithme::nb.
Side note, I don't see any a destructor declared in Graph. Please remember to free up that array (AND every AdjList its pointers point to).