码迷,mamicode.com
首页 > 其他好文 > 详细

用邻接矩阵和邻接表创建图

时间:2020-01-28 17:22:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:max   ini   col   null   有向图   main   输入   数组   alt   

#include <iostream>
using namespace std;

#define INFINITY 65536//无穷大
#define    MAX_VERTEX_NUM 10//最大顶点个数
typedef enum{DG,DN,UDG,UDN}GraphKind;//有向图,有向网,无向图,无向网
struct Graph
{
    char vexs[MAX_VERTEX_NUM];//储存顶点
    int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//邻接矩阵
    int vexnum, arcnum;//顶点数和边(弧)数
    GraphKind kind;//图的种类
};
//确定顶点序号
int LocateVex(Graph *G, char ch)
{
    for (int i = 0; i < G->vexnum; i++)
        if (G->vexs[i] == ch)
        {
            return i;//返回顶点序号
            break;
        }
    return -1;
}
void CreateUDN(Graph *G)//创建无向网
{
    int i, j;
    char v1, v2;//记录顶点名称
    int w;//记录权值
    cout << "请输入顶点个数:" << endl;cin >> G->vexnum;
    cout << "请输入边的个数:" << endl;cin >> G->arcnum;
    cout << "构造顶点:" << endl;
    for (int i = 0; i < G->vexnum; i++)//构造顶点
        cin >> G->vexs[i];
    for (int i = 0; i < G->vexnum; i++)//初始化邻接矩阵
        for (int j = 0; j < G->vexnum; j++)
            G->arc[i][j] = 0;
    cout << "构造邻接矩阵:" << endl;
    for (int k = 0; k < G->arcnum; k++)
    {
        cin >> v1 >> v2 >> w;
        i = LocateVex(G, v1);
        j = LocateVex(G, v2);
        G->arc[j][i] = G->arc[i][j] = w;
    }
}
void CreateGraph(Graph *G)
{
    switch (G->kind)
    {
    //case DG:return CreateDG(G);
    //case DN:return CreateDN(G);
    //case UDG:return CreateUDG(G);
    case UDN:return CreateUDN(G);
    }
}
//遍历邻接矩阵
void PrintArc(Graph *G)
{
    int i, j;
    for (i = 0; i < G->vexnum; i++)
    {
        for (j = 0; j < G->vexnum; j++)
            cout << G->arc[i][j] << " ";
        cout << endl;
    }
}

//邻接表
struct ENode//边结点
{
    int index;//边所指向的点的序号
    int info;//记录权值
    struct ENode* next;//指向下一条边
};
struct VNode//顶点结点
{
    char data;//顶点名称
    ENode* first;//指向顶点的第一个邻边
};
struct AGraph//记录图的信息
{
    int vexnum, arcnum;//顶点数和边数
    VNode p[MAX_VERTEX_NUM];//顶点数组
    GraphKind kind;//图的种类
};
int LocateVex(AGraph *G,char ch)
{
    for (int i = 0; i < G->vexnum; i++)
        if (ch == G->p[i].data)
        {
            return i;
            break;
        }
    return -1;
}
//创建邻接表
void CreateAGraph(AGraph *G)
{
    int i, j, info;
    char v1, v2;
    cout << "请输入顶点个数:" << endl; cin >> G->vexnum;
    cout << "请输入边的个数:" << endl; cin >> G->arcnum;
    cout << "构造顶点:" << endl;
    for (int i = 0; i < G->vexnum; i++)//构造顶点数组
    {
        cin >> G->p[i].data;
        G->p[i].first = NULL;
    }
    cout << "构造邻接表:" << endl;
    for (int k = 0; k < G->arcnum; k++)
    {
        cin >> v1 >> v2 >> info;
        i = LocateVex(G, v1);
        j = LocateVex(G, v2);
        ENode *E = (ENode*)malloc(sizeof(ENode));
        E->index = j;
        E->info = info;
        E->next = G->p[i].first;//头插法
        G->p[i].first = E;
    }
}
//打印邻接表
void PrintAGraph(AGraph *G)
{
    for (int i = 0; i < G->vexnum; i++)
    {
        cout << G->p[i].data;
        if (G->p[i].first != NULL)
        {
            ENode *p = G->p[i].first;
            while (p->next != NULL)
            {
                cout << "->" << p->index;
                p = p->next;
            }
            cout << "->" << p->index;
        }
        cout << endl;
    }
}
int main()
{
    Graph *G=(Graph*)malloc(sizeof(Graph));
    G->kind = UDN;
    CreateGraph(G);
    PrintArc(G);
    cout << endl;
    cout << "-------------------------------------" << endl;
    cout << "邻接表:" << endl;
    AGraph *AG=(AGraph*)malloc(sizeof(AGraph));
    CreateAGraph(AG);
    PrintAGraph(AG);
    system("pause");
    return 0;
}

运行结果:

技术图片

 

 

 技术图片

用邻接矩阵和邻接表创建图

标签:max   ini   col   null   有向图   main   输入   数组   alt   

原文地址:https://www.cnblogs.com/cs0915/p/12238183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!