图的创建方法

1、邻接矩阵创建

#include "stdio.h"    
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXVEX 100 /* 最大顶点数,应由用户定义 */
#define GRAPH_INFINITY 65535 /* 用65535来代表∞ */

typedef int Status;	/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef char VertexType; /* 顶点类型应由用户定义  */
typedef int EdgeType; /* 边上的权值类型应由用户定义 */
typedef struct
{
    
    
	VertexType vexs[MAXVEX]; /* 顶点表 */
	EdgeType arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
	int numNodes, numEdges; /* 图中当前的顶点数和边数  */
}MGraph;

/* 建立无向网图的邻接矩阵表示 */
void CreateMGraph(MGraph* G)
{
    
    
	int i, j, k, w;
	VertexType vtx;//顶点数据
	printf("输入顶点数和边数:\n");
	scanf("%d,%d", &G->numNodes, &G->numEdges); /* 输入顶点数和边数 */
	
	for (i = 0; i < G->numNodes; i++) /* 读入顶点信息,建立顶点表 */
	{
    
    
		printf("请输入顶点数据:\n");
		scanf("%c", &vtx);
		getchar();//清除缓冲区的换行符
		G->vexs[i] = vtx;
	}
		
	for (i = 0; i < G->numNodes; i++)
		for (j = 0; j < G->numNodes; j++)
			G->arc[i][j] = GRAPH_INFINITY;	/* 邻接矩阵初始化 */
	for (k = 0; k < G->numEdges; k++) /* 读入numEdges条边,建立邻接矩阵 */
	{
    
    
		printf("输入边(vi,vj)上的下标i,下标j和权w:\n");
		scanf("%d,%d,%d", &i, &j, &w); /* 输入边(vi,vj)上的权w */
		G->arc[i][j] = w;
		G->arc[j][i] = G->arc[i][j]; /* 因为是无向图,矩阵对称 */
	}
}

int main(void)
{
    
    
	MGraph G;
	CreateMGraph(&G);

	return 0;
}

2、邻接表创建

#include "stdio.h"    
#include "stdlib.h"
#include "math.h"  
#include "time.h"


typedef char VertexType;//顶点数据类型
typedef int EdgeType;//边数据类型
#define MAXVEX 100//最大顶点个数

typedef struct EdgeNode//边节点数据结构
{
    
    
	int adjvex;//该节点的下标
	int weight;//边的权值
	struct EdgeNode* next;//指向下一个临界点
}EdgeNode;


typedef struct VertexNode//顶点数据结构
{
    
    
	VertexType data;//顶点数据
	EdgeNode* firstEdge;//第一条邻边
}VertexNode,VexList[MAXVEX];

typedef struct MapByAdjList//图数据结构
{
    
    
	int vexNum, edgeNum;//顶点个数 边个数
	VexList vexlist;//顶点列表
}MapByAdjList;

/*无向图的创建方法*/
void CreateMapByAdjList(MapByAdjList* M)
{
    
    
	int i, j, k, w;
	printf("请输入图的顶点个数和边数:\n");
	scanf("%d,%d", &M->vexNum, &M->edgeNum);

	for (i = 0; i < M->vexNum; i++)/*顶点信息输入*/
	{
    
    
		printf("请输入第%d个顶点的数据:\n", i + 1);
		scanf("%c", &M->vexlist[i].data);
		getchar();
		M->vexlist[i].firstEdge = NULL;
	}

	for (k = 0; k < M->edgeNum; k++)/*边表建立*/
	{
    
    
		printf("请输入边(vi,vj)的下标以及边的权值w:\n");
		scanf("%d,%d,%d", &i, &j,&w);
		EdgeNode* newEdge=(EdgeNode*) malloc(sizeof(EdgeNode));
		newEdge->next = M->vexlist[i].firstEdge;
		newEdge->weight = w;
		newEdge->adjvex = j;
		M->vexlist[i].firstEdge = newEdge;//通过头插法将新边插入到边表中

		newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
		newEdge->next = M->vexlist[j].firstEdge;
		newEdge->weight = w;
		newEdge->adjvex = i;
		M->vexlist[j].firstEdge = newEdge;
	}
}

int main()
{
    
    
	MapByAdjList M;
	CreateMapByAdjList(&M);
	return 0;
}

3、十字链表法创建

#include "stdio.h"    
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

typedef char VexType;/*定义顶点数据类型*/
#define MAXVEX 100/*顶点最大个数*/

typedef struct EdgeNode/*定义边表结构*/
{
    
    
	int tailvex;/*弧的起点在顶点表中的下标*/
	int headvex;/*弧的终点在顶点表中的下标*/
	struct EdgeNode* headlink;/*指向同一终点的下一条弧*/
	struct EdgeNode *taillink;/*同一起点的下一条弧*/

}EdgeNode;

typedef struct VertexNode
{
    
    
	VexType data;/*顶点信息*/
	EdgeNode* firstin;/*第一条入边*/
	EdgeNode* firstout;/*第一条出边*/
}VertexNode,VertexList[MAXVEX];

typedef struct CrossLinkMap
{
    
    
	int vexNum, edgeNum;/*顶点数量和边数量*/
	VertexList vertexList;
}CrossLinkMap;

void CreateCrossLinkMap(CrossLinkMap* M)
{
    
    
	int i=0, j=0, k=0;
	printf("请输入顶点数量和边数量:\n");
	scanf("%d,%d", &M->vexNum, &M->edgeNum);

	for (i = 0; i < M->vexNum; i++)
	{
    
    
		printf("请输入第%d个顶点信息:\n",i+1);
		scanf("%c", &M->vertexList[i].data);
		getchar();/*清除缓冲区的换行符*/
		M->vertexList[i].firstin = NULL;
		M->vertexList[i].firstout = NULL;
	}

	for (k= 0; k < M->edgeNum; k++)
	{
    
    
		printf("请输入边<vi,vj>的下标i,j:\n");
		scanf("%d,%d", &i, &j);
		EdgeNode* newEdge = (EdgeNode*)malloc(sizeof(EdgeNode));
		newEdge->tailvex = i;
		newEdge->headvex = j;
		newEdge->headlink = M->vertexList[j].firstin;/*头插法插入新加入的入边*/
		newEdge->taillink = M->vertexList[i].firstout;/*头插法插入新加入的出边*/
		M->vertexList[i].firstout = newEdge;
		M->vertexList[j].firstin = newEdge;
	}
}

int main()
{
    
    
	CrossLinkMap M;
	CreateCrossLinkMap(&M);
	printf("A firstout:%d %d\n", M.vertexList[0].firstout->tailvex, M.vertexList[0].firstout->headvex);
	printf("A firstin:%d %d\n", M.vertexList[0].firstin->tailvex, M.vertexList[0].firstin->headvex);
	printf("A firstin.headlink:%d %d\n", M.vertexList[0].firstin->headlink->tailvex, M.vertexList[0].firstin->headlink->headvex);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_60406853/article/details/129430350