Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用链接列表/向量构建邻接列表图_C++_Adjacency List - Fatal编程技术网

C++ 使用链接列表/向量构建邻接列表图

C++ 使用链接列表/向量构建邻接列表图,c++,adjacency-list,C++,Adjacency List,这是我第一次尝试构建图形,我决定使用LL连接向量(或节点或元素,不管它们叫什么),我的每个节点都有一个向量,其中包含指向节点的指针。我在graph类中创建了一个“addEdge”函数,但它似乎破坏了我的程序。我的addElement类似乎工作得很好,因为它可以完美地添加和打印它们。但当我尝试添加一条边时,它会断开。我通过了调试器,调试器在 while(curr->next!=NULL | | curr->val!=n) 你知道为什么吗 #include "element.h" #include

这是我第一次尝试构建图形,我决定使用LL连接向量(或节点或元素,不管它们叫什么),我的每个节点都有一个向量,其中包含指向节点的指针。我在graph类中创建了一个“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;
    }
#包括“element.h”
#包括“向量”
#包括“iostream”
#包括“功能性”
使用名称空间std;
类图
{
公众:
元素*头;
图形(INTV)
{
head=NULL;
向量节点;
}
无效加法器(int-val)
{
if(head==NULL)
{
element*newelement=新元素(NULL,NULL,val);
head=新元素;
返回;
}
element*newelement=新元素(NULL,NULL,val);
元素*curr=水头;
while(当前->下一步!=NULL)
{
当前=当前->下一步;
}
curr->next=新元素;
新元素->上一个=当前;
返回;
}
无效添加边(整数n,整数边)
{
元素*e=头部;
元素*curr=水头;
如果(curr=NULL)
{
coutval!=边缘)
{
coutval!=边缘)
{
库特瓦尔!=n)
{
库特瓦尔!=n)
{

cout#include“vector”应该是#include,类似于iostream和functional header.Oops。但是仍然不是修复。如果(curr=NULL),这个赋值在这里是错别字?或者它也出现在代码中?我猜你的意思是如果(curr==NULL)在这里
#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;
};