2016-09-11 8 views
0

私は構造体内にリストを持っています。私は通常、このリストに要素を挿入したいだけです。構造体を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)

申し訳ありませんが、私はこのエラーについて手掛かりはありません。リンクリストを:

+5

ヒント:すべてのコードをポスト。グラフを定義するもののように、等々。おそらく、データ構造の初期化に間違いがありました。 "references"の連鎖の中の何かがNULLです。そのコードがなければ、私たちはあなたの問題の原因を教えてくれません! – GhostCat

+1

'graph.array-> head-> adjacents.push_back(number);' – Shravan40

+0

デバッガが問題を指摘できます。たぶん 'position'は負であるか、' V'より大きいか等しいでしょう。 – Franck

答えて

1

セグメンテーションフォールトは、私はあなたが接続される可能性が高い二つのリストを持っているので、あなたのコード内で暗黙の構造体の不変があるとし

graph->array[position].head = NULL; 

graph->array[position].head->adjacents.push_back(number); 

から来ていますAdjList::headで始まり、AdjNode::nextとリストAdjNode::adjacentを繰り返しています。

接続を維持するには、両方のリストに要素を追加する(Cスタイル)関数を追加できます。 (特に標準テンプレートライブラリのコンテナと)C++スタイルでCスタイル(のmalloc /無料)を混合することが悪い考えであることを

void 
addAdjacent(AdjList& list, int adjacent) { 
    // struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 
    struct AdjListNode* newNode = new AdjListNode; 
    newNode->next = list.head; 
    list.head = newNode; 
    newNode->dest = 0; 
    newNode->weight = 0; 
    newNode->adjacents = std::list<int>(); // undefined behavior with malloc 
    newNode->adjacents.push_back(adjacent); 
} 

注意。私のコードのコメント部分はstd::listので、そのフィールドには、次のmain機能は、それが多くのメモリリークを持っている場合でも動作します終わりに0

で満たされていないセグメンテーションフォールトを作成

int main(int argc, char** argv) { 
    struct Graph* graph = createGraph(2); 
    addAdjacent(graph->array[0], 1); 
    addAdjacent(graph->array[1], 2); 
    free(graph); 
    return 0; 
} 
(valgrindのツールを参照してください)

A C++ - (任意のメモリリークなし)98解決策は次のようになります。

// A structure to represent an adjacency list node 
struct AdjListNode 
{ 
    int dest; 
    int weight; 
    std::list<int> adjacents; 
    struct AdjListNode* next; 

    AdjListNode() : dest(0), weight(0), next(NULL) {} 
}; 

// A structure to represent an adjacency list 
struct AdjList 
{ 
    int pos; 
    struct AdjListNode *head; // pointer to head node of list 

    // Initialize each adjacency list as empty by making head as NULL 
    AdjList() : pos(0), head(NULL) {} 
    ~AdjList() 
     { while (head) { 
      struct AdjListNode* temp = head; 
      head = head->next; 
      delete temp; 
     } 
     } 

    void addAdjacent(int adjacent) 
     { struct AdjListNode* newNode = new AdjListNode; 
     newNode->next = head; 
     head = newNode; 
     newNode->adjacents.push_back(adjacent); 
     } 
}; 

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

    // Create an array of adjacency lists. Size of array will be V 
    Graph(int v) : V(v), array(NULL) 
     { if (v >= 0 && v <= 1000) 
      array = new struct AdjList[v]; 
     else 
      throw std::bad_alloc(); 
     } 
    ~Graph() 
     { delete [] array; } 
}; 

int main() { 
    struct Graph* graph = new Graph(2); 
    graph->array[0].addAdjacent(1); 
    graph->array[1].addAdjacent(1); 
    delete graph; 
    return 0; 
} 
+0

私はその解決策を今チェックしています。答えてくれてありがとう! – wagnerdelima

+0

ああ、ありがとう!それは本当に今私の問題を解決しました。私はC++の専門家ではありません。 xD – wagnerdelima

+0

多数の頂点を追加した後、私は問題があることに気付きました。グラフは無向でなければならないので、頂点0は1を指し、1は0を指すのが普通です。だから、好きなときに: graph-> array [0] .addAdjacent(1); graph-> array [1]です。addAdjacent(0); graph-> array [1] .addAdjacent(2); graph-> array [2] .addAdjacent(1); 出力は次のようになります。 頂点:1 頂点:0に隣接している2 頂点:1に隣接している2に隣接している:1 明らかに正しくない何か。任意のアイデア@フランク? – wagnerdelima

関連する問題