图基础之图的表示方法--邻接表和邻接矩阵

创建无向图的邻接表和邻接矩阵

ALGraph.h

#pragma once

#define MAXVEX 100
typedef char VertexType;        //顶点类型
typedef int EdgeType;           //边上的权值类型

struct EdgeNode                 //边表结点
{
    int adjvex;                 //邻接点域,存储该节点对应的下标

    EdgeType weight;            //存储权值,非网图不需要
    EdgeNode* next;             //下一个邻接点
};

typedef struct VertexNode
{
    VertexType data;            //顶点

    EdgeNode* firstEdge;        //边表头指针
}AdjList[MAXVEX];               //顶点数组

struct GraphAdjList
{
    AdjList adjList;            //顶点数组
    int numVertexes, numEdges;  //实际图中顶点数和边数
};

//建立无向图的邻接表结构
void CreateALGraph(GraphAdjList& G);

//根据顶点名称查找其下标
int locateVertex(GraphAdjList& G, VertexType v);

void print_ad(GraphAdjList& G);

ALGraph.cpp

#include "ALGraph.h"

#include <iostream>
using namespace std;

//建立无向图的邻接表结构
void CreateALGraph(GraphAdjList& G)
{
    cout << "请输入顶点个数和边个数:" << endl;
    cin >> G.numVertexes >> G.numEdges;

    cout << "请输入" << G.numVertexes << "个顶点:" << endl;
    for (int i = 0; i < G.numVertexes; i++)
    {
        cin >> G.adjList[i].data;
        G.adjList[i].firstEdge = NULL;
    }

    cout << "请输入" << G.numEdges << "条边:" << endl;

    VertexType vertex1, vertex2;
    int m, n;
    int edge;
    for (int i = 0; i < G.numEdges; i++)
    {
        cin >> vertex1 >> vertex2 >> edge;      //输入边及其依附的两个顶点

        m = locateVertex(G, vertex1);
        n = locateVertex(G, vertex2);

        if (m >= 0 && n >= 0)
        {
            //创建一个新的边结点
            EdgeNode* n1 = new EdgeNode;
            n1->weight = edge;
            n1->adjvex = n;
            n1->next = G.adjList[m].firstEdge;  //头插法插入顶点的firstedge
            G.adjList[m].firstEdge = n1;

            //由于是无向图,还需要插入到第n个顶点的边表中
            EdgeNode* n2 = new EdgeNode;
            n2->weight = edge;
            n2->adjvex = m;
            n2->next = G.adjList[n].firstEdge;
            G.adjList[n].firstEdge = n2;

        }
    }
    cout << "邻接表创建完成" << endl;
}

//根据顶点名称查找其下标
int locateVertex(GraphAdjList &G, VertexType v)
{
    for (int i = 0; i < G.numVertexes; i++)
    {
        if (v == G.adjList[i].data)
        {
            return i;
        }
    }
    return -1;
}

void print_ad(GraphAdjList &G)
{
    for (int i=0;i<G.numVertexes;i++)
    {
        EdgeNode *edgeNode = G.adjList[i].firstEdge;
        printf("%c:%c",G.adjList[i].data,G.adjList[edgeNode->adjvex].data);
        while (edgeNode->next)
        {
            printf(",%c",G.adjList[edgeNode->next->adjvex].data);
            edgeNode = edgeNode->next;
        }
        printf("\n");
    }
}

MGraph.h

#pragma once

typedef char VertexType;        //顶点类型
typedef int EdgeType;           //边类型
#define MAXVEX  100             //最大顶点数目
#define MAXEDGE 65536           //用65536代表无穷

typedef struct
{
    VertexType vexs[MAXVEX];    //顶点数组
    EdgeType arc[MAXVEX][MAXVEX];//边数组

    int numVertexes, numEdges;  //图中实际的顶点数和边数
} MGraph;

//创建无向网图
void CreateMGraph(MGraph& G);

//查询顶点索引
int LocateVex(MGraph G, VertexType v);

void print_mg(MGraph &G);

MGraph.cpp

#include "MGraph.h"
#include <iostream>

using namespace std;

//创建无向网图的邻接矩阵
void CreateMGraph(MGraph& G)
{
    cout << "请输入顶点个数和边个数:" << endl;
    cin >> G.numVertexes >> G.numEdges;

    cout << "请输入" << G.numVertexes << "个顶点:" << endl;
    for (int i = 0; i < G.numVertexes; i++)
    {
        cin >> G.vexs[i];                       //输入顶点
    }

    for (int i = 0; i < G.numVertexes; i++)
    {
        for (int j = 0; j < G.numVertexes; j++)
        {
            if (i == j)
            {
                G.arc[i][j] = 0;                
            }
            else
            {
                G.arc[i][j] = MAXEDGE;              //边初始化为无穷

            }
        }
    }

    cout << "请输入" << G.numEdges << "条边:" << endl;
    VertexType vertex1, vertex2;
    EdgeType edge;
    for (int i = 0; i < G.numEdges; i++)
    {
        cin >> vertex1 >> vertex2 >> edge;
        int m = LocateVex(G, vertex1);
        int n = LocateVex(G, vertex2);

        if (m >= 0 && n >= 0)
        {
            G.arc[m][n] = edge;
            G.arc[n][m] = edge;
        }
    }
    cout << "邻接矩阵创建完成" << endl;
}

//查询顶点索引
int LocateVex(MGraph G, VertexType v)
{
    for (int i = 0; i < G.numVertexes; i++)
    {
        if (v == G.vexs[i])
        {
            return i;
        }
    }
    return -1;
}

void print_mg(MGraph &G)
{
    printf("   ");
    for(int i=0;i<G.numVertexes;i++)
    {
        printf("  %c",G.vexs[i]);
    }
    printf("\n");

    for(int i=0;i<G.numVertexes;i++)
    {
        printf("  %c",G.vexs[i]);
        for (int j=0;j<G.numVertexes;j++)
        {
            printf("%3d",G.arc[i][j]);
        }
        printf("\n");
    }   
}

main.cpp

#include <iostream>
#include "MGraph.h"
#include "ALGraph.h"

using namespace std;

int main(int argc, char** argv)
{
    //1.邻接矩阵
    MGraph MG;
    CreateMGraph(MG);
    print_mg(MG);

    //2.邻接表
    GraphAdjList ALG;
    CreateALGraph(ALG);
    print_ad(ALG);

    return 0;
}

 

posted @ 2024-01-01 23:36  zhjh256  阅读(11)  评论(0编辑  收藏  举报