[英]Linked-List 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等。我不知道用於存儲到我的readIn
函數中的列表中的格式是否正確( myGraph.vertexInfo[p1].adjacency -> vertex=p2;
)。
是myGraph.vertexInfo[p1].adjacency -> vertex=p2;
正確的存儲方式? 如果不是我將如何將信息存儲到每個頂點的鄰接表中? 另外,輸入四個數字后,我遇到了細分錯誤。
#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;
}
這就是問題:
void readIn()
{
int g;
...
Graph(g);
編譯器不喜歡您先說“ g
是一個int
”,然后說“ g
是圖”。
我認為您正在嘗試使用g
作為構造函數的參數來初始化圖形,但是您有些困惑-
Graph(g);
應該
Graph myGraph(g);
您應該將readIn函數放在 Graph結構中,以便可以訪問其成員。 而且你也可以使用
Graph graph(g);
代替
Graph(g);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.