有向图的存储(邻接矩阵)

有向图的存储(邻接矩阵)

数据结构

//有向图的邻接矩阵
#define MaxVertexNum 10//最大顶点数目
typedef char V;//顶点的数据类型
typedef int E;//边的权值的数据类型
typedef struct {
	V vex[MaxVertexNum];//顶点表
	E edge[MaxVertexNum][MaxVertexNum];//邻接矩阵
	int vexnum, edgenum;//图的顶点数和弧数
}MGraph;

操作

//判断图G是否存在边<x, y>
bool Adjacent(MGraph G, int x, int y) {
	if (G.edge[x][y]==1) {
		return true;
	}
	return false;
}
//列出图G中与结点x邻接的边
void Neighbors(MGraph G, V x) {
	int item = 0;
	while (G.vex[item] != x) {
		item++;
		if (item >= MaxVertexNum) {
			printf("没有与%c相邻的边\n", x);
			return;
		}
	}
	int k = 0;
	printf("与%c相邻的边有:", x);
	while (k < MaxVertexNum) {
		if (G.edge[item][k] == 1) {
			printf("(%d, %d)", item, k);
		}
		k++;
	}
	printf("\n");
}

//在图G中插入顶点
void InsertVertex(MGraph& G, V x) {
	int length = G.vexnum;
	if (length < MaxVertexNum) {//插入顶点
		G.vex[length] = x;
		G.vexnum++;
	}
	else {
		printf("顶点数以及达到最大!\n");
	}
}
//在图G中删除结点
void DeleteVertex(MGraph&G, V x) {
	int item = 0;
	while (G.vex[item] != x) {
		item++;
		if (item >= MaxVertexNum) {
			printf("没有%c结点\n", x);
			return;
		}
	}
	G.vex[item] = NULL;
	for (int i = item+1; i < MaxVertexNum; i++) {
		G.vex[i - 1] = G.vex[i];
	}
	for (int i = 0; i < MaxVertexNum; i++) {
		if (G.edge[item][i] > 0) {
			G.edge[item][i] = 0;
			G.edgenum--;
		}
		if (G.edge[i][item] >  0) {
			G.edge[i][item] = 0;
			G.edgenum--;
		}
	}
	G.vexnum--;
}

//若边<x, y>存在,则向图G中添加该边
void AddEdge(MGraph& G, int x, int y, V value){
	if (Adjacent(G, x, y)) {
		printf("图中已经存在该边\n");
	}
	else {
		if (G.vex[x] < 0 || G.vex[y] < 0) {
			printf("图中未存在该顶点\n");
		}
		else {
			G.edge[x][y] = value;
			G.edgenum++;
		}		
	}
}

//删除边
void RemoveEdge(MGraph& G, int x, int y) {
	if (Adjacent(G, x, y)) {
		G.edge[x][y] = 0;
		G.edgenum--;
	}
	else {
		printf("图中未存在该边\n");
	}
}
//设置边的值
void Set_edge_value(MGraph& G, int x, int y, V value) {
	if (Adjacent(G, x, y)) {
		G.edge[x][y] = value;
	}
	else {
		printf("该边不存在\n");
	}
}
//获取边的值
E Get_edge_value(MGraph G, int x, int y) {
	if (Adjacent(G, x, y)) {
		return G.edge[x][y];
	}
	else {
		printf("该边不存在\n");
		return NULL;
	}
}

//打印图
void PrintGraph(MGraph G) {
	//打印顶点
	int i = 0;
	printf("顶点数据:");
	while (i < MaxVertexNum && G.vex[i] >= 0) {
		printf("%c ", G.vex[i]);
		i++;
	}
	printf("\n");
	//打印邻接矩阵
	printf("邻接矩阵数据:\n");
	for (i = 0; i < MaxVertexNum; i++) {
		for (int j = 0; j < MaxVertexNum; j++) {
			if (G.edge[i][j] >= 0) {
				printf("%d ", G.edge[i][j]);
			}
			else {
				printf("0 ");
			}			
		}
		printf("\n");
	}
	printf("边数目:%d\n", G.edgenum);
	printf("顶点数目:%d\n", G.vexnum);
}

int main() {
	MGraph* G = (MGraph*)malloc(sizeof(MGraph));
	G->edgenum = 0;
	G->vexnum = 0;
	//G->edge = (E**)malloc(sizeof(E));
	//G->vex = (V*)malloc(sizeof(V));
	InsertVertex(*G, 'a');
	InsertVertex(*G, 'b');
	InsertVertex(*G, 'c');
	InsertVertex(*G, 'd');
	InsertVertex(*G, 'e');
	AddEdge(*G, 0, 1, 1);
	AddEdge(*G, 1, 0, 1);

	AddEdge(*G, 0, 3, 2);
	AddEdge(*G, 3, 0, 2);

	AddEdge(*G, 1, 2, 3);
	AddEdge(*G, 2, 1, 3);

	AddEdge(*G, 1, 4, 4);
	AddEdge(*G, 4, 1, 4);

	AddEdge(*G, 2, 4, 5);
	AddEdge(*G, 4, 2, 5);

	AddEdge(*G, 2, 3, 6);
	AddEdge(*G, 3, 2, 6);
	PrintGraph(*G);

	RemoveEdge(*G, 0, 1);
	PrintGraph(*G);

	DeleteVertex(*G, 'a');
	PrintGraph(*G);

	return 0;
}

测试结构

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值