我正在尝试在C++中实现一个图。 我使用结构来表示图中的节点,该结构包含两个变量 -
a)一个整数,用于包含有关节点的一些信息。
b)一个列表,用于包含与其相连的其他顶点的索引。
以下是代码。
// Graphs using adjacency list
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
// structure to represent a vertex(node) in a graph
typedef struct vertex{
int info;
list<int> adj; // adjacency list of edges contains the indexes to vertex
} *vPtr;
int main(){
vPtr node = (vPtr)malloc(sizeof(struct vertex));
node->info = 34; // some arbitrary value
(node->adj).push_back(2); // trying to insert a value in the list
return 0;
}
代码编译正常,但在将元素推入列表时出现运行时错误。我的结构有问题吗?我正在使用Code Blocks和GNU GCC、C++ 98编译器来编译我的代码。