0

这是我第一次构建图表,我决定使用 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;
};
4

0 回答 0