小编Nis*_*ant的帖子

在c ++中使用Adjacency List的图表

我试图用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;
}
Run Code Online (Sandbox Code Playgroud)

代码正在编译正常,但是当我推回列表中的元素时,我遇到了运行时错误.我的结构有什么问题吗?
我正在使用代码块和GNU GCC,C++ 98编译器来编译我的代码.

c++ structure graph list adjacency-list

7
推荐指数
1
解决办法
5840
查看次数

使用变量初始化数组

我试图将二维数组传递给函数.我没有把它传递给函数的麻烦.但我很难理解这背后的逻辑.功能和主要定义如下:

// Function to print the two-dimensional array
void print(int x, int y, int a[x][y]){
    printf("\n");
    int i, j;
    for(i = 0; i < x; i++){
        for(j = 0; j < y; j++)
            printf("%d     ", a[i][j]);
        printf("\n");
    }
}

// Function to initialize the two-dimensional array
void init_2d(int *a, int x, int y){
    int i, j;
    for(i = 0; i < x; i++){
        for(j = 0; j < y; j++){
            a[i*y + j] = i + j;
        } …
Run Code Online (Sandbox Code Playgroud)

c multidimensional-array

3
推荐指数
1
解决办法
4706
查看次数