这是我第一次构建图形并且我决定使用LL来连接我的向量(或节点或元素,无论它们被调用),并且我的每个节点都有一个保存指针的向量节点。我在图表类中创建了一个“ addEdge ”函数,但它似乎打破了我的程序。我的addElement类似乎工作正常,因为它将完美地添加和打印出来。但是当我尝试添加边缘时,它会中断。我经历了调试器,它在
处中断 while(curr->next != NULL || curr->val != n)
任何想法为什么?
#include "element.h"
#include "vector"
#include "iostream"
#include "functional"
using namespace std;
class Graph
{
public:
element *head;
Graph(int V)
{
head = NULL;
vector <element*> nodes;
}
void addElement(int val)
{
if (head == NULL)
{
element *newelement = new element(NULL, NULL, val);
head = newelement;
return ;
}
element *newelement = new element(NULL,NULL, val);
element *curr = head;
while(curr->next != NULL)
{
curr = curr->next;
}
curr->next = newelement;
newelement->prev = curr;
return;
}
void addEdge(int n, int edge)
{
element *e = head;
element *curr = head;
if(curr = NULL)
{
cout<<"There are no elements in your graph to connect";
return;
}
while(e->next != NULL || e->val != edge)
{
cout<<"Looking";
e = e->next;
}
if(e->val != edge)
{
cout<<"Your edge node doesn't exist";
return;
}
while(curr->next != NULL || curr->val != n)
{
cout<<"Looking for main node";
curr = curr->next;
}
if(curr->val != n)
{
cout<<"Could not find the main node";
return;
}
curr->edges.push_back(e);
cout<<"Edge connected";
return;
}
节点类
#include "vector"
#include "iostream"
#include "functional"
using namespace std;
class element {
public:
element(element* n = NULL, element *p = NULL, int num = NULL)
{
val = num;
next = n;
prev = p;
}
int val;
element *prev;
element *next;
vector<element*> edges;
};