简体   繁体   English

链表C ++,

[英]Linked-List C++,

The input is in the following format 输入采用以下格式

5
1 2  9.0
1 3 12.0
2 4 18.0
2 3  6.0
2 5 20.0
3 5 15.0
0
1 5

The first number is the number of vertexes in the graph. 第一个数字是图中的顶点数。 Then next lines up to 0 are the edges of the graph. 然后直到0的下一行是图形的边缘。 With the first and second numbers being the vertexes and the third being how far the edge is between them. 第一个和第二个数字是顶点,第三个数字是它们之间的边距。 Trying to read in the data and store the edges into there locations in the List adjacency for that vertex. 尝试读取数据并将边存储到该顶点的列表邻接中的位置。 This example would make a graph with five vertexes with edges from 1 to 2&3. 本示例将制作一个具有五个顶点(边从1到2&3)的图。 2 to 4&3&1 etc. I do not know if the format for storing into the list in my readIn function is correct ( myGraph.vertexInfo[p1].adjacency -> vertex=p2; ) . 2至4&3&1等。我不知道用于存储到我的readIn函数中的列表中的格式是否正确( myGraph.vertexInfo[p1].adjacency -> vertex=p2; )。

Is myGraph.vertexInfo[p1].adjacency -> vertex=p2; myGraph.vertexInfo[p1].adjacency -> vertex=p2; the correct way to store it? 正确的存储方式? Ff not how I would store the info into the adjacency lists for each vertex? 如果不是我将如何将信息存储到每个顶点的邻接表中? Also I am getting a Segmentation fault after entering four numbers. 另外,输入四个数字后,我遇到了细分错误。

#include <cstdio>
using namespace std;

struct ListCell
{
   ListCell* next;
   int vertex;
   double weight;

   ListCell(int v, double w, ListCell* nxt)
   {
      vertex = v;
      weight = w;
      next = nxt;
   }
};

typedef ListCell* List;

struct Vertex
{
   bool signaled;
   long distance;
   List adjacency;    
};

struct Graph
{
   int     numVertices;
   Vertex* vertexInfo;

   Graph(int n)
   {
      numVertices = n;
      vertexInfo  = new Vertex[n+1];
      for(int i = 1; i <= n; i++)
      {
         vertexInfo[i].signaled = false;
      }
   }
};

//==============================================================
//                   readIn
//==============================================================
// 
//==============================================================

void readIn()
{
   int g;
   int p1;
   int p2;
   float edge;
   scanf("%i ", &g);

   Graph myGraph(g);
   scanf("%i", &p1);
   while(p1 != 0)
   {
      scanf("%i", &p2);
      scanf("%f", &edge);         
      myGraph.vertexInfo[p1].adjacency -> vertex=p2; 
      myGraph.vertexInfo[p2].adjacency -> vertex=p1;
      myGraph.vertexInfo[p1].adjacency -> weight=edge; 
      myGraph.vertexInfo[p2].adjacency -> weight=edge;
      scanf("%i", &p1);
   }
}
//==============================================================
//                   main
//==============================================================

int main(int argc, char** argv)
{
   readIn();
   return 0;
}

This is the problem: 这就是问题:

void readIn()
{
   int g;

   ...

   Graph(g);

The compiler doesn't like it that you first said " g is an int " and then said " g is a graph". 编译器不喜欢您先说“ g是一个int ”,然后说“ g是图”。

I think you're trying to initialize a graph with g as an argument to the constructor but you have something confused - 我认为您正在尝试使用g作为构造函数的参数来初始化图形,但是您有些困惑-

Graph(g);

should be 应该

Graph myGraph(g);

You should place the readIn function inside your Graph struct so it can acces its members. 您应该将readIn函数放在 Graph结构中,以便可以访问其成员。 And also you can use 而且你也可以使用

Graph graph(g);

instead of 代替

Graph(g);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM