C,C++实现图的邻接矩阵存储

萌新小白,请大家多多指教。

代码分别使用了书上的方法和我对书上总结的方法。

书上的方法(方法一):对四种图分别编写函数,一个函数做一件事,功能简洁一点

我的方法(方法二):对四个函数进行总和,总代码短

#include<stdio.h>
#include<stdlib.h>
#define MAX_VERTEX 20
typedef int* Info;//边上的信息,如果有的话
typedef enum{DG,DN,UDG,UDN}GraphType;
struct Vertex//图中的点
{
	int data;
};
typedef struct Edge//图中的边
{
	int w;//可做图中有无边的标志或网中边的权重
	Info information;
}Edge,AdjMatrix[MAX_VERTEX][MAX_VERTEX];//此处相当于定义了AdjMatrix是一个二维数组类型
struct Graph
{
	Vertex* vlist;//存放顶点的数组地址
	AdjMatrix am;//邻接矩阵
	int vertexnum;//顶点数
	int edgenum;//边数
	GraphType type;//图的种类
};

//寻找图的顶点存放在顶点数组中的位置,返回数组下标
int Located(Graph &g, int x)
{
	for (int i = 0; i < g.vertexnum; i++)
		if (g.vlist[i].data == x)
			return i;
	return g.vertexnum;
}

//方法一:书上的方法,将四种图分为四个函数
/*
void CreateDirectedGraph(Graph& g)
{
	printf("Input vertex number and edge number\n");
	scanf("%d%d", &g.vertexnum, &g.edgenum);
	g.vlist = (Vertex*)malloc(g.vertexnum * sizeof(Vertex));
	if (!g.vlist)
	{
		printf("malloc error\n");
		return;
	}
	for (int i = 0; i < g.vertexnum; i++)
	{
		printf("Input vertex\n");
		scanf("%d", &g.vlist[i].data);
	}
	for (int i = 0; i < g.vertexnum; i++)
		for (int j = 0; j < g.vertexnum; j++)
			g.am[i][j] = { 0,NULL };
	for (int t = 0; t < g.edgenum; t++)
	{
		int a, b;
		printf("Input vertexs of the edge\n");
		scanf("%d%d", &a, &b);
		int i = Located(g, a), j = Located(g, b);
		if (i != g.vertexnum && j != g.vertexnum)
			g.am[i][j] = { 1,NULL };
	}
}

void CreateDirectedNet(Graph& g)
{
	int w;
	printf("Input vertex number , edge number\n");
	scanf("%d%d", &g.vertexnum, &g.edgenum);
	g.vlist = (Vertex*)malloc(g.vertexnum * sizeof(Vertex));
	if (!g.vlist)
	{
		printf("malloc error\n");
		return;
	}
	for (int i = 0; i < g.vertexnum; i++)
	{
		printf("Input vertex\n");
		scanf("%d", &g.vlist[i].data);
	}
	for (int i = 0; i < g.vertexnum; i++)
		for (int j = 0; j < g.vertexnum; j++)
			g.am[i][j] = { 0,NULL };
	for (int t = 0; t < g.edgenum; t++)
	{
		int a, b;
		printf("Input vertexs and weight of the edge\n");
		scanf("%d%d%d", &a, &b, &w);
		int i = Located(g, a), j = Located(g, b);
		if (i != g.vertexnum && j != g.vertexnum)
			g.am[i][j] = { w,NULL };
	}
}

void CreateUndirectedGraph(Graph& g)
{
	printf("Input vertex number and edge number\n");
	scanf("%d%d", &g.vertexnum, &g.edgenum);
	g.vlist = (Vertex*)malloc(g.vertexnum * sizeof(Vertex));
	if (!g.vlist)
	{
		printf("malloc error\n");
		return;
	}
	for (int i = 0; i < g.vertexnum; i++)
	{
		printf("Input vertex\n");
		scanf("%d", &g.vlist[i].data);
	}
	for (int i = 0; i < g.vertexnum; i++)
		for (int j = 0; j < g.vertexnum; j++)
			g.am[i][j] = { 0,NULL };
	for (int t = 0; t < g.edgenum; t++)
	{
		int a, b;
		printf("Input vertexs of the edge\n");
		scanf("%d%d", &a, &b);
		int i = Located(g, a), j = Located(g, b);
		if(i!=g.vertexnum&& j!= g.vertexnum)
			g.am[i][j]= g.am[j][i] = { 1,NULL };
	}
}

void CreateUndirectedNet(Graph& g)
{
	int w;
	printf("Input vertex number , edge number\n");
	scanf("%d%d", &g.vertexnum, &g.edgenum);
	g.vlist = (Vertex*)malloc(g.vertexnum * sizeof(Vertex));
	if (!g.vlist)
	{
		printf("malloc error\n");
		return;
	}
	for (int i = 0; i < g.vertexnum; i++)
	{
		printf("Input vertex\n");
		scanf("%d", &g.vlist[i].data);
	}
	for (int i = 0; i < g.vertexnum; i++)
		for (int j = 0; j < g.vertexnum; j++)
			g.am[i][j] = { 0,NULL };
	for (int t = 0; t < g.edgenum; t++)
	{
		int a, b;
		printf("Input vertexs and weight of the edge\n");
		scanf("%d%d%d", &a, &b, &w);
		int i = Located(g, a), j = Located(g, b);
		if (i != g.vertexnum && j != g.vertexnum)
			g.am[i][j] = g.am[j][i] = { w,NULL };
	}
}

void CreateGraph(Graph& g)
{
	printf("Input graphtype\n");
	scanf("%d", &g.type);
	switch (g.type)
	{
	case 0:CreateDirectedGraph(g); break;
	case 1:CreateDirectedNet(g); break;
	case 2:CreateUndirectedGraph(g); break;
	case 3:CreateUndirectedNet(g); break;
	default:
		break;
	}
}*/

//方法二:我把四个函数合成了,看起来简洁点
void CreateGraph(Graph& g)
{
	int w;//权重
	printf("Input graphtype , vertex number , edge number\n");
	scanf("%d%d%d",&g.type, &g.vertexnum, &g.edgenum);
	g.vlist = (Vertex*)malloc(g.vertexnum * sizeof(Vertex));
	if (!g.vlist)
	{
		printf("malloc error\n");
		return;
	}
	for (int i = 0; i < g.vertexnum; i++)//输入顶点
	{
		printf("Input vertex\n");
		scanf("%d", &g.vlist[i].data);
	}
	for (int i = 0; i < g.vertexnum; i++)//初始化邻接矩阵
		for (int j = 0; j < g.vertexnum; j++)
			g.am[i][j] = { 0,NULL };
	for (int t = 0; t < g.edgenum; t++)//输入边
	{
		int a, b;
		printf("Input vertexs of the edge\n");
		scanf("%d%d", &a, &b);
		int i = Located(g, a), j = Located(g, b);//在顶点数组找该边的两个顶点的位置
		if (i != g.vertexnum && j != g.vertexnum)
			g.am[i][j] = { 1,NULL };
		if (g.type == 1 || g.type == 3)//若为网,给予权重
		{
			printf("Input weight of the edge\n");
			scanf("%d",&w);
			g.am[i][j] = { w,NULL };
		}
		if (g.type == 2 || g.type == 3)//若为无向,令矩阵对称
		{
			g.am[j][i] = g.am[i][j] ;
		}
	}
}

//打印邻接矩阵,检查程序是否正确
void Print(Graph& g)
{
	for (int i = 0; i < g.vertexnum; i++)
	{
		for (int j = 0; j < g.vertexnum; j++)
			printf("%d ", g.am[i][j].w);
		printf("\n");
	}
}

int main()
{
	Graph g;
	CreateGraph(g);
	Print(g);
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值