数据结构---图(数组表示法)

// 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++)
        {
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值