Построение графа списка смежности с использованием списка ссылок / вектора

Это мой первый шаг в построении графа, и я решил использовать 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;
};

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector