[英]Reading in to Linked-List not Working correctly C++
我一直在這個問題上停留了幾個小時,請有人幫忙。
輸入采用以下格式
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
第一個數字是圖中的頂點數。 然后直到0的下一行是圖形的邊緣。 第一個和第二個數字是頂點,第三個數字是它們之間的邊距。 嘗試讀取數據並將邊存儲到該頂點的列表鄰接中的位置。 本示例將制作一個具有五個頂點(邊從1到2&3)的圖。 2至4&3&1等也存儲對面EX。 2 1 9.0。 它沒有正確存儲數據。 在讀取每個頂點的每個新數據時,將覆蓋先前的數據。 它將數據存儲在多個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
因此,正是單元格的寫數只是錯誤的信息。
#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;
}
您的代碼有很多錯誤。 首先,也許最明顯的是:
myGraph.vertexInfo[p1].adjacency = new ListCell
(p2,edge,myGraph.vertexInfo[p1].adjacency);
您可以在輸入循環中調用它。 查看輸入的第一個頂點(1)。 您輸入兩次,一次輸入“ 1 2 9.0”,再次輸入“ 1 3 12.0”。 變量p1不變(保持為1),但是您用第二個輸入覆蓋了第一個輸入。
因此,不僅在輸入中存在錯誤,而且由於動態創建ListCell並覆蓋先前動態分配的ListCell,因此內存泄漏。
也許數組不是您應該用作vertexInfo的數組。 也許應該是頂點到頂點/距離的映射:
std::map<int, std::vector<std::pair<int,double>>>
其中關鍵點是頂點,內部向量是所有相鄰頂點和距離(每個表示為一對)的向量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.