当我在邻接表上添加一个关系时,如何解决这个分段错误?
How I solve this segmentation fault while I add one relation on the adjacency list?
这是头文件的一部分
using namespace std;
namespace graph {
typedef string Name;
typedef bool Male;
typedef string Relation;
typedef int Age;
struct Node;
struct Vertex;
typedef Node* Grafo;
typedef Vertex* AdjacentList;
const Grafo emptyGraph = NULL;
const AdjacentList emptyAdjacentList = NULL;
}
这是我用来实现数据结构链接邻接表的struct;
struct graph::Node {
Name nome;
Male maschio;
Age anni;
Node* next;
Vertex* adjList;
};
struct graph::Vertex {
Node* vertPtr;
Relation rel;
Vertex* next;
};
这是给我错误的函数
它应该添加与人的关系,一个调用示例可以是:
addRelation(Mark,Lucas,Dad,Son,g)
bool graph::addRelation(Name n, Name m, Relation rel1, Relation rel2, Grafo & g) {
if (n == m) //I make sure the two persons I wanna bond aren't the same one
return false;
Node * first = g; //I initialize a ptr and I iter util it point to the desired person
while (first != emptyGraph && first -> nome != n)
first = first -> next;
if (first == emptyGraph)
return false;
Node * second = g; //same with second person
while (second != emptyGraph && second -> nome != m)
second = second -> next;
if (second == emptyGraph) //if one of them is missing we exit
return false;
if (first -> adjList != emptyAdjacentList) {
Vertex * auxFirst = first -> adjList;
while (auxFirst -> next != emptyAdjacentList && auxFirst -> vertPtr -> nome != m)
auxFirst = auxFirst -> next;
if (auxFirst -> vertPtr -> nome == m)
return false;
Vertex * secondHalfEdgeToAdd = new Vertex;
auxFirst -> next = secondHalfEdgeToAdd;
auxFirst -> rel = rel1;
auxFirst -> vertPtr = first;
auxFirst -> next = emptyAdjacentList;
}
}
我不知道为什么我仍然需要添加详细信息,否则我不能 post 所以我只是写下这个短语希望它能起作用;
没有更多代码,我突然想到的一件事是:
auxFirst -> next = secondHalfEdgeToAdd;
auxFirst -> rel = rel1;
auxFirst -> vertPtr = first;
auxFirst -> next = emptyAdjacentList;
auxFirst -> next = secondHalfEdgeToAdd;
比你有 auxFirst -> next = emptyAdjacentList;
。您正在覆盖 auxFIrst->next
指针。
IDK 如果你打算设置 auxFirst->next->next = NULL
。
这是头文件的一部分
using namespace std;
namespace graph {
typedef string Name;
typedef bool Male;
typedef string Relation;
typedef int Age;
struct Node;
struct Vertex;
typedef Node* Grafo;
typedef Vertex* AdjacentList;
const Grafo emptyGraph = NULL;
const AdjacentList emptyAdjacentList = NULL;
}
这是我用来实现数据结构链接邻接表的struct;
struct graph::Node {
Name nome;
Male maschio;
Age anni;
Node* next;
Vertex* adjList;
};
struct graph::Vertex {
Node* vertPtr;
Relation rel;
Vertex* next;
};
这是给我错误的函数
它应该添加与人的关系,一个调用示例可以是:
addRelation(Mark,Lucas,Dad,Son,g)
bool graph::addRelation(Name n, Name m, Relation rel1, Relation rel2, Grafo & g) {
if (n == m) //I make sure the two persons I wanna bond aren't the same one
return false;
Node * first = g; //I initialize a ptr and I iter util it point to the desired person
while (first != emptyGraph && first -> nome != n)
first = first -> next;
if (first == emptyGraph)
return false;
Node * second = g; //same with second person
while (second != emptyGraph && second -> nome != m)
second = second -> next;
if (second == emptyGraph) //if one of them is missing we exit
return false;
if (first -> adjList != emptyAdjacentList) {
Vertex * auxFirst = first -> adjList;
while (auxFirst -> next != emptyAdjacentList && auxFirst -> vertPtr -> nome != m)
auxFirst = auxFirst -> next;
if (auxFirst -> vertPtr -> nome == m)
return false;
Vertex * secondHalfEdgeToAdd = new Vertex;
auxFirst -> next = secondHalfEdgeToAdd;
auxFirst -> rel = rel1;
auxFirst -> vertPtr = first;
auxFirst -> next = emptyAdjacentList;
}
}
我不知道为什么我仍然需要添加详细信息,否则我不能 post 所以我只是写下这个短语希望它能起作用;
没有更多代码,我突然想到的一件事是:
auxFirst -> next = secondHalfEdgeToAdd;
auxFirst -> rel = rel1;
auxFirst -> vertPtr = first;
auxFirst -> next = emptyAdjacentList;
auxFirst -> next = secondHalfEdgeToAdd;
比你有 auxFirst -> next = emptyAdjacentList;
。您正在覆盖 auxFIrst->next
指针。
IDK 如果你打算设置 auxFirst->next->next = NULL
。