数据结构(9) -- 图的邻接矩阵存储

////////////////////////////////////////////////////////
//图的邻接矩阵存储
////////////////////////////////////////////////////////
#include <iostream>
#include <stdlib.h>
#define MaxVertexNum 100 //最大顶点数
#define INFINITY 0  //无穷大设为无符号整数的最大值
typedef char VertexType;  //顶点类型设为字符类型
typedef int EdgeType; ///边的权值设为整形
enum GraphType{DG, UG, DN, UN}; //有向图,无向图,有向网图,无向网图
using namespace std;

typedef struct
{
    VertexType Vertices[MaxVertexNum]; //顶点表
    EdgeType Edges[MaxVertexNum][MaxVertexNum]; //邻接矩阵,即边表
    int n, e; //顶点数n和边数e
    enum GraphType GType; 
}MGraph;

void CreateMGraph(MGraph *G)
{
    int  i, j, k, w;
    G->GType = UN;    /* Undirected Network  无向网图  */
    cout << "请输入顶点数和边数(输入格式为:顶点数, 边数):" << endl;
    cin >> G->n >> G->e; /* 输入顶点数和边数 */
    cout << "请输入顶点信息(输入格式为:顶点号<CR>):" << endl;
    for (i = 0; i < G->n; i++)
        cin >> &(G->Vertices[i]); /*  输入顶点信息,建立顶点表  */
    for (i = 0; i < G->n; i++)
        for (j = 0; j < G->n; j++)
            G->Edges[i][j] = INFINITY; /* 初始化邻接矩阵 */
    cout << "请输入每条边对应的两个顶点的序号和权值,输入格式为:i, j, w:" << endl;
    for (k = 0; k < G->e; k++) {
        cin >> i >> j >> w; /* 输入e条边上的权,建立邻接矩阵 */
        G->Edges[i][j] = w;
        G->Edges[j][i] = w; /* 因为无向网图的邻接矩阵是对称的 */
    }
}

void Print(MGraph *G)
{
    cout << "   ";
    for (int i = 0; i < G->n; i++)
        cout << G->Vertices[i] << "  ";
    cout << endl;
    for (int i = 0; i < G->n; i++)
    {
        cout << G->Vertices[i] << "  ";
        for (int j = 0; j < G->n; j++)
        {
            cout << G->Edges[i][j] << "  ";
        }
        cout << endl;
    }
}

int main()
{
    MGraph *G = new MGraph;
    CreateMGraph(G);
    Print(G);
    int i = 0;
    //scanf("%d", &i);
    system("pause");
    return 0;
}

 

posted @ 2015-04-09 20:51  禅意  阅读(312)  评论(0编辑  收藏  举报