数据结构(图)

图的数组存储
在这里插入图片描述
源代码:

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//图的数组表示
#define INFINITY 0  //两个顶点之间无边或弧时的数值
#define MAX_VERTEX_NUM 20  //最大顶点个数
#define MAX_INFO 10  //弧相关信息的字符串的最大长度
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1

typedef int Status;
typedef int VRType;//顶点关系类型

typedef char InfoType;
typedef char VertexType[MAX_VERTEX_NUM];//顶点类型(字符串类型)

typedef enum GraphKind
{
	DG, DN, UDG, UDN
} GraphKind;//有向图,有向网,无向图,无向网

typedef struct
{
	VRType adj;//VRType是顶点关系类型,对于无权图,用1或0

	InfoType * info; //该弧的相关指针
}ArcCell, AdjMaxtix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
	VertexType vexs[MAX_VERTEX_NUM];//顶点向量(20个string_Node_name,每一个结点名最长20个字符)
	AdjMaxtix arcs;
	int vexnum, arcnum;
	GraphKind kind;
}MGraph;

int LocateVex(MGraph *G, VertexType u)
{

	for (int i = 0; i < G->vexnum; ++i)

		if (strcmp(u, G->vexs[i]) == 0)
			return i;

return -1;
}

Status CreateDG(MGraph *G)//有向图
{

	int i, j, k, l, Include_Info;
	char a[MAX_INFO];
	VertexType va, vb;
	printf("*************Directed Graph***********\n");
	printf("Please input the number of side,vertex in this directed graph,\nand then the side include other information (YES:1,NO:0)\n");
	printf("Please input the number of vertex:\t");
	fflush(stdout);
	scanf("%d", &G->vexnum);

	printf("Please input the number of side:\t");
	fflush(stdout);
	scanf("%d", &G->arcnum);

	printf("Please input other information included in this side(YES:1,NO:0):\t");
	fflush(stdout);
	scanf("%d", &Include_Info);

	printf("*Start creating Directed Graph*\nPlease input the vertex-directed one by one\n");
	fflush(stdout);
	for (i = 0; i < G->vexnum; ++i)
	{
		printf("Please input the %d-nd vertex-directed:\t", i + 1);
		fflush(stdout);
		fflush(stdin);
		scanf("%s", G->vexs[i]);
	}

	for (i = 0; i < G->vexnum; ++i)        //初始化邻接矩阵
		for (j = 0; j < G->vexnum; ++j)
		{
			G->arcs[i][j].adj = INFINITY;  //有向图
			G->arcs[i][j].info = NULL;
		}
	printf("Please input  %d-nd arc head and arc tail\n", G->arcnum);

	for (k = 0; k < G->arcnum; ++k)
	{
		printf("Please input %d-nd arc tail:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", va);

		printf("Please input %d-nd arc head:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", vb);
      /*计算该弧的两端点在矩阵中的位置*/
		i = LocateVex(G, va);

		j = LocateVex(G, vb);

		G->arcs[i][j].adj = 1; //有向图的权值设置
/*弧的描述,info所指的字符串*/
		if (Include_Info)
		{
			printf("Please input information of this side:(max:%d char):\t", MAX_INFO);
			fflush(stdout);
			fflush(stdin);
			gets(a);
			l = strlen(a);
			if (l)
			{
				G->arcs[i][j].info = (char *)malloc((l + 1) * sizeof(char)); //有向图(i和j可以唯一标识这条弧即这条弧的信息)
				strcpy(G->arcs[i][j].info, a);//复制到info所指的字符串中去
			}
		}
	}
	G->kind = DG;//这个矩阵的种类为有向图矩阵
	return OK;

}

Status CreateDN(MGraph* G)//有向网,采用数组(邻接矩阵),构造有向网

{

	int i, j, k, w, Include_Info;
	char a[MAX_INFO];
	VertexType va, vb;
	printf("*************Directed Net***********\n");
	printf("Please input the number of side,vertex in this directed net,\nand then the side include other information (YES:1,NO:0)\n");
	printf("Please input the number of vertex:\t");
	fflush(stdout);
	scanf("%d", &G->vexnum);

	printf("Please input the number of side:\t");
	fflush(stdout);
	scanf("%d", &G->arcnum);

	printf("Please input other information included in this side(YES:1,NO:0):\t");
	fflush(stdout);
	scanf("%d", &Include_Info);

	printf("*Start creating Directed Net*\nPlease input the vertex-directed one by one\n");
	fflush(stdout);
	for (i = 0; i < G->vexnum; ++i)
	{
		printf("Please input the %d-nd vertex-directed:\t", i + 1);
		fflush(stdout);
		fflush(stdin);
		scanf("%s", G->vexs[i]);
        //printf("%s",&G->vexs[i]);//test
	}

	for (i = 0; i < G->vexnum; ++i)        //初始化邻接矩阵

		for (j = 0; j < G->vexnum; ++j)

		{
			G->arcs[i][j].adj = INFINITY;  //有向网

			G->arcs[i][j].info = NULL;
		}
	printf("Please input  %d-nd arc head and arc tail\n", G->arcnum);
		for (k = 0; k < G->arcnum; ++k)
	{
		printf("Please input %d-nd arc tail:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", va);//弧的尾结点(字符串)

		printf("Please input %d-nd arc head:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", vb);//弧的头结点

		printf("Please input the weight of this side:\t");
		fflush(stdout);

		scanf("%d", &w);

		i = LocateVex(G, va);
		//printf("%d",i);//test

		j = LocateVex(G, vb);
		//printf("%d",j);//test
		G->arcs[i][j].adj = w;//有向网的权值

		if (Include_Info)

		{
			printf("Please input information of this side:(max:%d char):\t", MAX_INFO);
			fflush(stdout);
            fflush(stdin);
			gets(a);
			w = strlen(a);
			if (w)
			{
				G->arcs[i][j].info = (char *)malloc((w + 1) * sizeof(char)); //有向网
				strcpy(G->arcs[i][j].info, a);
			}
		}
	}
	G->kind = DN;
	return OK;

}



Status CreateUDG(MGraph *G)  //用数组(邻接矩阵)表示法,构造无向图

{


	int i, j, k, l, Include_Info;
	char a[MAX_INFO];
	VertexType va, vb;
	printf("*************UnDirected Graph***********\n");
	printf("Please input the number of side,vertex in this directed net,\nand then the side include other information (YES:1,NO:0)\n");
	printf("Please input the number of vertex:\t");
	fflush(stdout);
	scanf("%d", &G->vexnum);

	printf("Please input the number of side:\t");
	fflush(stdout);
	scanf("%d", &G->arcnum);

	printf("Please input other information included in this side(YES:1,NO:0):\t");
	fflush(stdout);
	scanf("%d", &Include_Info);

	printf("*Start creating UnDirected Graph*\nPlease input the vertex-directed one by one\n");
	fflush(stdout);
	for (i = 0; i < G->vexnum; ++i)
	{
		printf("Please input the %d-nd vertex-directed:\t", i + 1);
		fflush(stdout);
		fflush(stdin);
		scanf("%s", G->vexs[i]);
	}

	for (i = 0; i < G->vexnum; ++i)        //初始化邻接矩阵

		for (j = 0; j < G->vexnum; ++j)

		{
			G->arcs[i][j].adj = INFINITY;  //无向图

			G->arcs[i][j].info = NULL;
		}
	printf("Please input  %d-nd arc head and arc tail\n", G->arcnum);

	for (k = 0; k < G->arcnum; ++k)
	{
		printf("Please input %d-nd arc tail:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", va);

		printf("Please input %d-nd arc head:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", vb);


		i = LocateVex(G, va);

		j = LocateVex(G, vb);

		G->arcs[i][j].adj = 1;
		G->arcs[j][i].adj = 1;  //无向图

		if (Include_Info)

		{
			printf("Please input information of this side:(max:%d char):\t", MAX_INFO);
			fflush(stdout);
            fflush(stdin);
			gets(a);
			l = strlen(a);
			if (l)
			{
				G->arcs[i][j].info = (char *)malloc((l + 1) * sizeof(char));
				strcpy(G->arcs[i][j].info, a);
			}
		}
	}
	G->kind = UDG;
	return OK;

}

Status CreateUDN(MGraph *G) //采用数组(邻接矩阵)表示法,构造无向网

{
	int i, j, k, w, Include_Info;
	char a[MAX_INFO];
	VertexType va, vb;
	printf("*************UnDirected Net***********\n");
	printf("Please input the number of side,vertex in this directed net,\nand then the side include other information (YES:1,NO:0)\n");
	printf("Please input the number of vertex:\t");
	fflush(stdout);
	scanf("%d", &G->vexnum);

	printf("Please input the number of side:\t");
	fflush(stdout);
	scanf("%d", &G->arcnum);

	printf("Please input other information included in this side(YES:1,NO:0):\t");
	fflush(stdout);
	scanf("%d", &Include_Info);

	printf("*Start creating UnDirected Net*\nPlease input the vertex-directed one by one\n");
	fflush(stdout);

	for (i = 0; i < G->vexnum; ++i)
	{
		printf("Please input the %d-nd vertex-directed:\t", i + 1);
		fflush(stdout);
		fflush(stdin);
		scanf("%s", G->vexs[i]);

		//printf("%s",&G->vexs[i]);//test
       // fflush(stdout);//test
	}

	for (i = 0; i < G->vexnum; ++i)//初始化邻接矩阵

		for (j = 0; j < G->vexnum; ++j)

		{
			G->arcs[i][j].adj = INFINITY; //无向网

			G->arcs[i][j].info = NULL;
		}
	printf("Please input  %d-nd arc head and arc tail\n", G->arcnum);

	for (k = 0; k < G->arcnum; ++k)
	{
		printf("Please input %d-nd arc tail:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", va);

		printf("Please input %d-nd arc head:\t", k + 1);

		fflush(stdout);
		fflush(stdin);
		scanf("%s", vb);

		printf("Please input the weight of this side:\t");
		fflush(stdout);

		scanf("%d", &w);
		i = LocateVex(G, va);

		j = LocateVex(G, vb);

		G->arcs[i][j].adj =w;
		G->arcs[i][j].adj = w;//无向网

		if (Include_Info)
		{
			printf("Please input information of this side:(max:%d char)\t", MAX_INFO);
			fflush(stdout);
			fflush(stdin);
			gets(a);
			w = strlen(a);
			if (w)
			{
				G->arcs[i][j].info = (char *)malloc((w + 1) * sizeof(char)); //无向网
				strcpy(G->arcs[i][j].info, a);
			}
		}
	}
	G->kind = UDN;
	return OK;
}

Status CreateGraph(MGraph * G)

{
	int i = 0;
	printf("Please input the kind of this Graph:\nDirected Graph:%4d\nDirected Net:%6d\n",i,i+1);
	fflush(stdout);
	printf("UnDirected Graph:%2d\nUnDirected Net:%4d\n",i+2,i+3);
	fflush(stdout);
	fflush(stdin);
	scanf("%d", &G->kind);

	switch (G->kind)

	{
	case DG:  CreateDG(G);       //有向图
		break;
	case DN:  CreateDN(G);       //有向网
		break;
	case UDG: CreateUDG(G);      //无向图
		break;
	case UDN: CreateUDN(G);      //无向网
		break;
	}

	return OK;

}

Status DestroyGraph(MGraph *G) //若图存在,则销毁图G

{

	int i, j, k = 0;
	if (G->kind % 2) // 网
		k = INFINITY; // K为两顶点之间无边或无弧时邻接矩阵元素的值
	for (i = 0; i < G->vexnum; ++i)
		if (G->kind < 2) //有向
		{
			for (j = 0; j < G->vexnum; ++j)
				if (G->arcs[i][j].adj != k) //两顶点之间有弧
					if (G->arcs[i][j].info)
					{
				    	free(G->arcs[i][j].info);  //释放指针info所指的单元
					    G->arcs[i][j].info = NULL; //拯救野指针
					}
		}
		else//无向
		{
			for (j = i + 1; j < G->vexnum; ++j)
			{
				if (G->arcs[i][j].adj != k)
					if (G->arcs[i][j].info)
					{
						free(G->arcs[i][j].info);
						G->arcs[i][j].info = G->arcs[j][i].info = NULL;//限制两个指针
					}
			}
		}
	G->vexnum = 0;
	G->vexnum = 0;
	printf("Destroy Graph/Net success!");
	return OK;
}

void Display(MGraph G)
{
	for (int i = 0; i < G.vexnum; ++i)
		for (int j = 0; j < G.vexnum; ++j)
		{
			printf("%4d ", G.arcs[i][j].adj);
			if ((j + 1) % G.vexnum == 0)

				printf("\n");
		}
}

int main()
{

	MGraph G;
	CreateGraph(&G);
	Display(G);
	DestroyGraph(&G);
	return 0;
}

运行结果:

Please input the kind of this Graph:
Directed Graph:   0
Directed Net:     1
UnDirected Graph: 2
UnDirected Net:   3
3
*************UnDirected Net***********
Please input the number of side,vertex in this directed net,
and then the side include other information (YES:1,NO:0)
Please input the number of vertex:	3
Please input the number of side:	2
Please input other information included in this side(YES:1,NO:0):	1
*Start creating UnDirected Net*
Please input the vertex-directed one by one
Please input the 1-nd vertex-directed:	a
Please input the 2-nd vertex-directed:	b
Please input the 3-nd vertex-directed:	c
Please input  2-nd arc head and arc tail
Please input 1-nd arc tail:	b
Please input 1-nd arc head:	a
Please input the weight of this side:	5
Please input information of this side:(max:10 char)	asdf
Please input 2-nd arc tail:	c
Please input 2-nd arc head:	a
Please input the weight of this side:	15
Please input information of this side:(max:10 char)	asdf
   0    0    0 
   5    0    0 
  15    0    0 
Destroy Graph/Net success!

参考资料:
[1].数据结构:C语言版/严蔚敏
[2].https://blog.csdn.net/u010721079/article/details/26101885?utm_source=apphttps://blog.csdn.net/u010721079/article/details/26101885?utm_source=app

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值