// MGraph_array.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui------
-------2015-2-9--------------------*/
#include "stdafx.h"
#include <iostream>
#include <queue>
using namespace std;
#define INFINITY INT_MAX
#define NAX_VERTEX_NUM 20
#define VRtype int
//#define InfoType char*
#define VertexType char*
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
bool visited[NAX_VERTEX_NUM];
void(*VisitFunc)(VertexType);
typedef enum
{
DG,DN,UDG,UDN//有向图,有向网,无向图,无向网
}GraphKind;
typedef struct ArcCell
{
VRtype adj;//无权图,用0或1;带权图,即权值
//InfoType *info;
}ArcCell, AdjMatrix[NAX_VERTEX_NUM][NAX_VERTEX_NUM];
typedef struct //定义图的结构
{
VertexType vexs[NAX_VERTEX_NUM];//顶点向量
AdjMatrix arcs; //邻接矩阵
int vexnum,arcnum; //定点数,边(弧)数
GraphKind kind; //图的种类标记
}MGraph;
int CreateDG(MGraph &G);//构造有向图
int CreateDN(MGraph &G);//构造有向网
int CreateUDG(MGraph &G);//构造无向图
int CreateUDN(MGraph &G);//构造无向网
int CreateGraph(MGraph &G)//采用数组(邻接矩阵)表示法,构造图G
{
int n;
cout<<"创建有向图(0),有向网(1),无向图(2),无向网(3)"<<endl;
cin>>n;
switch(n)
{
case DG:
G.kind = DG;
return CreateDG(G);//有向图
case DN:
G.kind = DN;
return CreateDN(G);//有向网
case UDG:
G.kind = UDG;
return CreateUDG(G);//无向图
case UDN:
G.kind = UDN;
return CreateUDN(G);//无向网
default:
return ERROR;
}
}
int LocateVex(MGraph G, VertexType V)//顶点V在图中的位置
{
int i;
for(i=0; i<G.vexnum; i++)
{
if(strcmp(G.vexs[i],V) == 0)
return i;
}
return -1;//不存在,返回-1
}
int CreateUDG(MGraph &G)//构造无向图
{
cout<<"输入顶点数,边数:"<<endl;
cin>>G.vexnum>>G.arcnum;
int i,j,k;
for(i=0; i<G.vexnum; i++)//构造顶点向量
{
cout<<"输入第"<<i+1<<"个顶点的名称:";
G.vexs[i] = (VertexType)malloc( sizeof(char) );
cin>>G.vexs[i];
}
for(i=0; i<G.vexnum; i++)//初始化领接矩阵
{
for(j=0; j<G.vexnum; j++)
{
数据结构---图(数组表示法)
最新推荐文章于 2024-07-02 16:37:54 发布

最低0.47元/天 解锁文章
6566

被折叠的 条评论
为什么被折叠?



