私は構造体内にリストを持っています。私は通常、このリストに要素を挿入したいだけです。構造体をC++で一覧表示する
私の構造体は次のとおりです。
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
int weight;
std::list<int> adjacents;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
int pos;
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
私はaceessしようとすると:
graph->array[position].head->adjacents->push_back(number);
それはちょうど私にこれを要求します:終了コード139で終了し
プロセス(信号11によって中断:SIGSEGV)
申し訳ありませんが、私はこのエラーについて手掛かりはありません。リンクリストを:
ヒント:すべてのコードをポスト。グラフを定義するもののように、等々。おそらく、データ構造の初期化に間違いがありました。 "references"の連鎖の中の何かがNULLです。そのコードがなければ、私たちはあなたの問題の原因を教えてくれません! – GhostCat
'graph.array-> head-> adjacents.push_back(number);' – Shravan40
デバッガが問題を指摘できます。たぶん 'position'は負であるか、' V'より大きいか等しいでしょう。 – Franck