栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

图的存贮结构----邻接矩阵表示法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

图的存贮结构----邻接矩阵表示法

#include
#include
typedef struct graph {
	int NoEdge;
	int Vertices;
	int** A;
}Graph;
void CreateGraph(Graph *g, int n , int noedge) {
	int i, j;
	g->NoEdge = noedge;
	g->Vertices = n;
	g->A = (int**)malloc(n*sizeof(int*));
	for (i = 0; i < n;i++) {
		g->A[i] = (int*)malloc(n*sizeof(int));
		for (j = 0; j < n;j++) {
			g->A[i][j] = noedge;
		}
		g->A[i][i] = 0;
	}
}
void Add(Graph*g, int u,int v, int w) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] != g->NoEdge) {
		printf("BadInputn");
	}
	else {
		g->A[u][v] = w;
	}
}
void Delete(Graph *g, int u, int v) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] == g->NoEdge) {
		printf("BadInputn");
	}
	else {
		g->A[u][v] = g->NoEdge;
		printf("Delete Successfullyn");
	}
}
void Exist(Graph* g, int u, int v) {
	if (u < 0 || v < 0 || u > g->Vertices - 1 || v>g->Vertices - 1 || u == v || g->A[u][v] == g->NoEdge) {
		printf("No Existancen");
	}
	else {
		printf("Element Exists!n");
	}
}
void PrintMatrix(Graph* g) {
	int i,j;
	for (i = 0; i < g->Vertices;i++) {
		for (j = 0; j < g->Vertices;j++) {
			printf("%-4d",g->A[i][j]);
		}
		printf("n");
	}
	printf("***************n");
}
void main() {
	Graph* g;
	g = (Graph*)malloc(sizeof(Graph));
	CreateGraph(g, 4, -1); //在这里网的noedge统一为-1,不再为无穷值,可以看出这是创建了一个网
	PrintMatrix(g);
	Add(g, 1, 0, 4);
	Add(g, 1, 2, 5);
	Add(g, 2, 3, 3);
	Add(g, 3, 0, 1);
	Add(g, 3, 1, 1);
	Delete(g, 1, 0);
	PrintMatrix(g);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692086.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号