【发布时间】:2017-01-06 15:51:02
【问题描述】:
我正在阅读一本关于 Algorithms 的书,其中包含以下代码。我很难理解这里的一些台词。
我在以下代码中将my doubt lines 显示为DOUBT LINE 1 和DOUBT LINE 2。
我还评论了一行REFERENCE,我是having difficulty,以便理解。
请详细说明DOUBT LINE 1和DOUBT LINE 2。
#define MAXV 100 /* maximum number of vertices */
#define NULL 0 /* null pointer */
/* DFS edge types */
#define TREE 0 /* tree edge */
#define BACK 1 /* back edge */
#define CROSS 2 /* cross edge */
#define FORWARD 3 /* forward edge */
typedef struct edgenode {
int y; /* adjancency info */
int weight; /* edge weight, if any */
struct edgenode *next; /* next edge in list */
} edgenode;
typedef struct {
edgenode *edges[MAXV+1]; /* adjacency info */ //REFERENCE
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
int directed; /* is the graph directed? */
} graph;
它是 graph.h 头文件,它也是读取和插入函数。
read_graph(graph *g, bool directed)
{
int i; /* counter */
int m; /* number of edges */
int x, y; /* vertices in edge (x,y) */
initialize_graph(g, directed);
scanf("%d %d",&(g->nvertices),&m);
for (i=1; i<=m; i++) {
scanf("%d %d",&x,&y);
insert_edge(g,x,y,directed);
}
}
insert_edge(graph *g, int x, int y, bool directed)
{
edgenode *p; /* temporary pointer */
p = malloc(sizeof(edgenode)); /* allocate storage for edgenode */
p->weight = NULL;
p->y = y;
p->next = g->edges[x]; //DOUBT LINE1
g->edges[x] = p; /* insert at head of list */ //DOUBT LINE 2
g->degree[x] ++;
if (directed == FALSE)
insert_edge(g,y,x,TRUE);
else
g->nedges ++;
}
【问题讨论】:
-
头文件中有函数吗?最好不要,对吧?
标签: c pointers struct graph singly-linked-list