[作业]图的邻接表表示法
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
//https://blog.csdn.net/buaa_shang/article/details/10399923
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef struct ArcNode { //边表
int adjvex;
ArcNode *nextarc;
type_info *info;
};
typedef struct Vnode { //顶点表
char data;
ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList *vertices;
int vexnum; //图中当前顶点数
int arcnum; //图中当前边数
int kind; //图的种类标志
}ALGraph;
void CreatALGraph(ALGraph* G) {
int i, j, k;
int a;
ArcNode* s;
printf("请输入顶点数和边数:\n");
scanf("%d %d", &i, &j);
G->vexnum = i;
G->arcnum = j;
printf("请输入顶点编号:\n");
for (i = 1; i <= G->vexnum; i++) { //建立顶点表
scanf("%d", &a);
G->vertices[i]->data = a; //读入顶点信息
G->vertices[i]->firstarc = NULL; //边表头指针置为空
}
printf("请输入由两个定点构成的边,示例:0 1\n");
for (k = 0; k < G->arcnum; k++)
{
scanf("%d %d", &i, &j); //读入边(Vi, Vj)的顶点对应的序号
s = (ArcNode *)malloc(sizeof(ArcNode)); //生成边表节点
s->adjvex = j;
s->nextarc = G->vertices[i]->firstarc;
G->vertices[i]->firstarc = s; //将新节点*s插入顶点Vi的边表头部
}
}
int main() {
int i, j;
ALGraph* G = (ALGraph *)malloc(sizeof(ALGraph));
G->vertices = (AdjList *)malloc(sizeof(Vnode) * MAX_VERTEX_NUM);
CreatALGraph(G);
for (i = 1; i <= G->vexnum; i++)
{
while (G->vertices[i]->firstarc)
{
printf("%d -> ", G->vertices[i]->data);
printf("%d\n", G->vertices[i]->firstarc->adjvex);
G->vertices[i]->firstarc = G->vertices[i]->firstarc->nextarc;
}
}
return 0;
}
转载自原文链接, 如需删除请联系管理员。
原文链接:[作业]图的邻接表表示法,转载请注明来源!