图的存储 数据结构 陈越

图的邻接矩阵表示-无向网图的初始化程序

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define INF 65535

typedef int Vertex; //顶点下标表示顶点
typedef int WeightType;//边的权值
typedef char DataType;//顶点存储的数据类型设为字符

/*图结点的定义*/
typedef struct GNode *PtrToGNode;

struct GNode {

	int Nv;//顶点数
	int Ne;//边数

	WeightType G[MaxVertexNum][MaxVertexNum];//邻接矩阵
	DataType Data[MaxVertexNum];//存顶点的数据
};

typedef PtrToGNode MGraph;/*以邻接矩阵存储的图的类型*/

typedef struct ENode *PtrToENode;

struct ENode {
	Vertex V1, V2; /*有向边 <v1,v2>*/
	WeightType Weight;
};

typedef PtrToENode Edge;



MGraph CreatGraph(int VertexNum) {
	/*初始化一个有vertexnum个顶点 但没有边的图*/

	MGraph Graph;

	Graph = (MGraph)malloc(sizeof (struct GNode));
	Graph->Nv = VertexNum;
	Graph->Ne = 0; //还没建边

	for (int i = 0; i < Graph->Nv; i++)
		for (int j = 0; j < Graph->Nv; j++) {
			Graph->G[i][j] = INF;
		}

	return Graph;
}


void InsertEdge(MGraph Graph, Edge E) {
	/*插入边 <v1,v2>*/
	Graph->[E->V1][E->V2] = E->Weight;
	/*若是无向图,还要插入边<v2,v1>*/
	Graph->G[E->V2][E->V1] = E->Weight;
}

MGraph BuildGraph() {
	MGraph Graph;
	Edge E;
	Vertex V;
	int Nv, i;

	scanf("%d", &Nv); //读入顶点的个数

	Graph = CreatGraph(Nv); /*初始化有nv个顶点 但是没有边的图 */

	scanf("%d", &(Graph->Ne)); /*读入边数*/

	if (Graph->Ne != 0) { //如果有边

		E = (Edge) malloc (sizeof (struct ENode)) ; //建立边结点
		/*读入边,格式为“起点 终点 权重”*/
		for ( i = 0; i < Graph->Ne; i++) {
			scanf("%d %d %d", &E->V1, &E->V2, &E->Weight );
	
			/*权重如果不是整形,输入的格式要改*/
			InsertEdge(Graph, E);
		}
	}

	//如果顶点有数据的话,读入数据
	for(V=0 ; V<Graph->Nv;V++)
		scanf("%c" , &(G->Data[V]));

  return  Graph;
}







图的邻接表表示-无向图的初始化程序

#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define INF 65535


/*----------邻接表表示法的结构和类型声明-----------------------------------------*/

typedef int Vertex; //顶点下标表示顶点
typedef int WeightType;//边的权值
typedef char DataType;//顶点存储的数据类型设为字符

/*边的定义*/
typedef struct ENode *PtrToENode;
struct ENode{
	Vertex V1,V2;// 有向边<v1,v2>
	WeightType weight;//权值
	
};

typedef PtrToENode Edge;

/*邻接点的定义*/
typedef struct AdjVNode *PtrToAdjVNode;

struct AdjVNode{
	Vertex AdjV; //邻接点的下标
	WeightType weight;//边权
	PtrToAdjVNode Next;//指向下一个邻接点的指针
		
};

/*顶点表头结点的定义*/

typedef struct Vnode{
	PtrToAdjVNode FirstEdge;/*边表头指针*/
	DataType Data; /*存顶点的数据*/
	/*注意很多情况下顶点无数据,此时data可以不用出现*/

}AdjList[MaxVertexNum];


/*图结点的定义*/
typedef struct GNode *PtrToGNode;

struct GNode{
	int Nv;//顶点
	int Ne;//边数
	AdjList G;/*邻接表*/
};

typedef PtrToGNode LGraph;

/*------------------------------------------------------------------------------*/

LGraph CreatGraph(int VertexNum)
{
	/*初始化一个有vertexnum个顶点但没有边的图*/
	Vertex V;
	LGraph Graph;
	
	Graph=(LGraph)malloc(sizeof (struct GNode));
	Graph->Nv=VertexNum;
	Graph->Ne=0;
	
	/*初始化邻接表头指针*/
	for(V=0; V<Graph->Nv ;V++)
	{
		Graph->G[V].FirstEdge=NULL;
	}
	return Graph;
}

void InsertEdge( LGraph Graph ,Edge E)
{
	PtrToAdjVNode NewNode;
	/*插入边<v1,v2>*/
	/*为V2建立新的邻接点*/
	NewNode =(PtrToAdjVNode)malloc(sizeof (struct AdjVNode));
	NewNode->AdjV=E->V2;
	NewNode->weight=E->weight;
	/*将V2插入V1的表头*/
	NewNode->Next=Graph->G[E->V1].FirstEdge;
	Graph->G[E->V1].FirstEdge=NewNode;
	
	/*若为无向图则还要插入边<v2,v1>*/
	
	NewNode=(PtrToAdjVNode)malloc(sizeof (struct AdjVNode));
	NewNode->AdjV=E->V2;
	NewNode->weight=E->weight;
	NewNode->Next=Graph->G[E->V2].FirstEdge;
	Graph->G[E->V2].FirstEdge=NewNode;
	
}


LGraph BuildGraph()
{
	LGraph Graph;
	Edge E;
	Vertex V;
	int Nv, i;
	
	scanf("%d",&Nv);/*读入顶点个数*/
	Graph=CreatGraph(Nv);/*初始化有Nv个顶点但没有边的图*/
	
	scanf("%d",&(Graph->Ne));/*读入边数*/
	if(Graph->Ne!=0)
	{
		E=(Edge)malloc(sizeof (struct ENode));
		
		for(int i=0;i<Graph->Ne;i++){
		scanf("%d %d %d",&E->V1,&E->V2,&E->weight);
		InsertEdge(Graph,E);
		}
	}
	
	/*如果顶点有数据,读入数据*/
	for(V=0;V<Graph->Nv; V++)
	{
		scanf("%c",&(Graph->G[V].Data));
	}
	
	return Graph;
	
}

posted on 2024-11-23 15:24  swj2529411658  阅读(0)  评论(0编辑  收藏  举报

导航