当我在邻接列表上添加一个关系时,我如何解决这个分割错误



这是头文件的一部分

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 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;
}
}

我不知道为什么我还需要添加细节,否则我不能发布这个,所以我只是写这个短语,希望它能起作用;

如果没有更多的代码,我会想到一件事:

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(。

相关内容

  • 没有找到相关文章

最新更新