图的建立及遍历输出

来源:百度知道 编辑:UC知道 时间:2024/06/22 17:01:36
任务:建立图的存储结构
实现图的深度优先或广度优先遍历算法
要求:能够输入图的顶点和边的信息 并存储到相应存储结构中,输出
图的邻接矩阵及遍历结果

#include <iostream.h>
#include <stdlib.h>

#define INFINITY 0

#define MAX_VERTEX_NUM 10 //最大顶点数

#define MAX_EDGE_NUM 40 //最大边数

typedef enum {DG,DN,UDG,UDN}Graphkind;

typedef char VertexType; //顶点数据类型

typedef struct ArcCell
{
int adj; //无权图,1或0表示相邻否;带权图则是权值。
//int *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
VertexType vexs[MAX_VERTEX_NUM]; //顶点向量
AdjMatrix arcs; //邻接矩阵
int vexnum,arcnum; //图的当前顶点数和弧数。
Graphkind kind;
}MGraph;

int LocateVex(MGraph G,VertexType v1)
{
int i;
for(i=0;i<G.vexnum;i++)
if(G.vexs[i]==v1)
return i;
return -1;
}

int CreatUDN(MGraph &G)
// 采用数组表示法,构造无向网 G
{
VertexType v1,v2;
int w,j;
cout<<"输入图的顶点数"<<endl;
cin>>G.vexnum;
cout<<&q