由于要查找和查找Null Pointer的所有问题,因此很难找到我想要的东西。我有一个用于Graph结构的类(邻接列表模型),其中有一个顶点的链接列表,而没有相邻顶点的链接列表。但是,当我尝试传递邻接列表(名为adjList,在此-> adjList-> add中传递)时,即使它应该是指向类的指针,也将其读取为空指针。有什么想法吗?
Graph::Vertex::Vertex(char lab) {
this->next = 0;
this->prev = 0;
this->prior = 0;
this->label = lab;
this->dist = 0;
this->index = 0;
this->adjList = 0;
}
Graph::List::List() {
this->head = 0;
}
Graph::List::Node::Node(Vertex *to, int dist) {
this->dist = dist;
this->next = 0;
this->prev = 0;
this->datum = 0;
}
void Graph::List::add(Vertex *to, int dist) {
if (this == 0) {
this->head = new Node(to, dist);
return;
}
Node *temp = this->head;
while (temp->next != 0) {
temp = temp->next;
}
temp->next = new Node(to, dist);
temp->next->prev = temp;
}
void Graph::addV(char vert) {
if (this->vertices == 0) {
this->vertices = new Vertex(vert);
this->order++;
return;
}
else {
Vertex *temp = this->vertices;
while (temp->next != 0) {
temp = temp->next;
}
temp->next = new Vertex(vert);
temp->next->prev = temp;
this->order++;
}
void Graph::addE(char Fvert, char Tvert, int dist) { //From Vert, To Vert, and Distance
Vertex *tempT = this->vertices;
Vertex *tempF = this->vertices;
while (tempF != 0 && tempF->label != Fvert) {
tempF = tempF->next;
}
while (tempT != 0 && tempT->label != Tvert) {
tempT = tempT->next;
}
//Insertion start
if (tempF->label == Fvert && tempT->label == Tvert) {
tempF->adjList->add(tempT, dist);
}
//Insertion finished
else if (tempF->label != Fvert)
cout << "No Vertex to start from." << endl;
else if (tempT->label != Tvert)
cout << "No Vertex to go to." << endl;
else
cout << "Neither of these Vertices exist." << endl;
}
头文件
class Graph {
class List;
class Vertex { //Vertex is a list of Verticies that have a list of Edge pointer that point to other Vertecies
public:
char label;
int dist, index;
List *adjList; //EdgeList //For Vertex Remove, a list of all Verticies that are connected to the Vertex
Vertex *next, *prev, *prior;
Vertex(char);
~Vertex();
};
class List { //List is a list of Verticies with a Distance that shows the distance between verticies
public:
class Node {
public:
Vertex * datum;
Node *next, *prev;
int dist;
Node(Vertex*, int); //Input Vertex to compete edge for that list, and provide a distance
};
Node* head;
void destr_helper(Node*);
List();
~List();
void add(Vertex*, int);
void rem(char);
bool contains(char);
};
Vertex* vertices;
int order;
void destr_helper(Vertex*);
public:
Graph(); //bool can be added for directed and undirected
//~Graph();
void addV(char);
void addE(char, char, int);
void remV(char);
void remE(char, char);
};