简体   繁体   English

读入链接列表无法正常工作C ++

[英]Reading in to Linked-List not Working correctly C++

I have been stuck on this problem for hours will someone please help. 我一直在这个问题上停留了几个小时,请有人帮忙。

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 also stores opposites EX. 2至4&3&1等也存储对面EX。 2 1 9.0. 2 1 9.0。 It is not storing the data correctly. 它没有正确存储数据。 When reading in each new data for each vertex overwrites the previous data. 在读取每个顶点的每个新数据时,将覆盖先前的数据。 It is storing the data in multiple listCells because when printed out i get 它将数据存储在多个listCells中,因为当打印出来时,我得到

1 3 12.000
1 3 12.000
2 5 20.000
2 5 20.000
2 5 20.000
2 5 20.000
3 5 15.000
3 5 15.000
3 5 15.000

So it is the write number of cells just wrong information. 因此,正是单元格的写数只是错误的信息。

#include <cstdio>
using namespace std;
int tracing= 1;

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;
      }
   }
};

//==============================================================
//                   tail
//==============================================================
// 
//==============================================================

   List tail(List L)
   {
      return L->next;
   }
//==============================================================
//                   isEmpty
//==============================================================
// 
//==============================================================

   bool isEmpty(List L)
   {
      return L == NULL;
   }
//==============================================================
//                   readIn
//==============================================================
// 
//==============================================================

Graph 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);

      if(tracing >0)
      {
         printf("Edge from %i to %i is %5.3f\n", p1, p2, edge);
      }

      myGraph.vertexInfo[p1].adjacency = new ListCell 
      (p2,edge,myGraph.vertexInfo[p1].adjacency);


      myGraph.vertexInfo[p2].adjacency = new ListCell
      (p1, edge, myGraph.vertexInfo[p2].adjacency);

      scanf("%i", &p1);
   }
   return myGraph;
}

//==============================================================
//                   printOut
//==============================================================
// 
//==============================================================

void printOut(Graph myGraph)
{  
   int n;
   int length = myGraph.numVertices;
   float d;
   List p;

   printf("There are %i vertices.\n", length);
   printf("The edges are as follows. \n\n");
   for(int i=1; i<=length; i++)
   {
      p= myGraph.vertexInfo[i].adjacency;
      for(p=p; !isEmpty(p); p=tail(p))
      {
         n = myGraph.vertexInfo[i].adjacency -> vertex;
         d = myGraph.vertexInfo[i].adjacency -> weight; 
         if(i<n)
         {
            printf("%i %i %7.3f \n",i,n,d);
         }
      }
   }
}
//==============================================================
//                   main
//==============================================================

int main(int argc, char** argv)
{
   Graph myGraph = readIn();
   printOut(myGraph);
   return 0;
}

There is a lot wrong with your code. 您的代码有很多错误。 The first thing and probably the most glaring is this: 首先,也许最明显的是:

myGraph.vertexInfo[p1].adjacency = new ListCell 
   (p2,edge,myGraph.vertexInfo[p1].adjacency);

you call this in the input loop. 您可以在输入循环中调用它。 Look at your first vertex you input (1). 查看输入的第一个顶点(1)。 You input this twice, one time for "1 2 9.0", and again for "1 3 12.0". 您输入两次,一次输入“ 1 2 9.0”,再次输入“ 1 3 12.0”。 The variable p1 doesn't change (it remains at 1), but you're overwriting the first input with the second input. 变量p1不变(保持为1),但是您用第二个输入覆盖了第一个输入。

So you not only have an error in the input, you have a memory leak since you're creating a ListCell dynamically, and overwriting the previous dynamically allocated ListCell. 因此,不仅在输入中存在错误,而且由于动态创建ListCell并覆盖先前动态分配的ListCell,因此内存泄漏。

Maybe an array is not what you should use as a vertexInfo. 也许数组不是您应该用作vertexInfo的数组。 Maybe it should be a map of vertex to vertices/distances: 也许应该是顶点到顶点/距离的映射:

std::map<int, std::vector<std::pair<int,double>>>

where the key is the vertex, and the inner vector is a vector of all the adjacent vertices and distances (each one represented as a pair). 其中关键点是顶点,内部向量是所有相邻顶点和距离(每个表示为一对)的向量。

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

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