我编写了一个简单的代码来打印给定边的图形,并且我正在使用结构将数据存储在图形中,当节点数少于5个但大于5个会带来malloc错误时,它可以很好地工作。不知道为什么会这样,有人可以指出我做错了什么。
#include <stdio.h>
#include <stdlib.h>
// Define maximum number of vertices in the graph
#define N 5
//#define N 4 //use this when the nodes are less than 5
// Data structure to store graph
struct Graph {
// An array of pointers to Node to represent adjacency list
struct Node *head[N];
};
// A data structure to store adjacency list nodes of the graph
struct Node {
int dest;
struct Node *next;
};
// data structure to …Run Code Online (Sandbox Code Playgroud)