实现图的邻接矩阵和邻接表的存储

//graph.h头文件
typedef int InfoType;
#define MAXV 100//最大顶点个数
//定义邻接矩阵类型
typedef struct
{
    int no;//顶点标号
    InfoType info;//顶点其他信息,这里用于存放权值
}VertexType;//顶点类型
typedef struct//图的定义
{
    int edges[MAXV][MAXV];//邻接矩阵
    int n,e;//顶点数,弧数
    VertexType vexs[MAXV];//存放顶点信息
}MGraph;//图的邻接矩阵类型
//以下定义邻接表类型
typedef struct ANode//弧的节点结构类型
{
    int adjvex;//该弧的终点位置
    struct ANode*nextarc;//指向下一条弧的指针
    InfoType info;//该弧的相关信息,这里用于存放权值
}ArcNode;
typedef int Vertex;
typedef struct Vnode//邻接表节点的类型
{
    Vertex data;//顶点信息
    ArcNode * firstarc;//指向第一条弧
}VNode;
typedef VNode AdjList[MAXV];//AdjList 是邻接表类型
typedef struct
{
    AdjList adjlist;//邻接表
    int n,e;//图的顶点数n和边数e
}ALGraph;//图的邻接表类型

//相关操作源文件.cpp
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
#define INF 32767//INF表示∞
void MatToList(MGraph g,ALGraph * &G)
//将邻接矩阵g转换成邻接表G
{
    int i,j,n=g.n;//n为顶点数
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    for(i=0;i<n;i++)//给邻接表中所有头结点的指针域置初值
        G->adjlist[i].firstarc=NULL;
    for(i=0;i<n;i++)//检查邻接矩阵中每个元素
        for(j=n-1;j>=0;j--)
            if(g.edges[i][j]!=0)//邻接矩阵的当前元素不为0
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));//创建一个结点*p
                p->adjvex=j;
                p->info=g.edges[i][j];//存放的权值
                p->nextarc=G->adjlist[i].firstarc;//将*p链接到链表后
                G->adjlist[i].firstarc=p;
            }
            G->n=n;
            G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g)
//将邻接表G转换成邻接矩阵g
{
    int i,j,n=G->n;
    ArcNode *p;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)//g.edges[i][j]赋初值0
            g.edges[i][j]=0;
        for(i=0;i<n;i++)
        {
            p=G->adjlist[i].firstarc;
            while(p!=NULL)//对所有相邻的顶点进行处理
            {
                g.edges[i][p->adjvex]=p->info;
                p=p->nextarc;
            }
        }
        g.n=n;
        g.e=G->e;
}
void DispMat(MGraph g)
//输出邻接矩阵g
{
    int i,j;
    for(i=0;i<g.n;i++)
    {
        for(j=0;j<g.n;j++)
            if(g.edges[i][j]==INF)
                printf(" % 3s","");
            else
                printf(" % 3d",g.edges[i][j]);
            printf("\n");
    }
}
void DispAdj(ALGraph *G)
{
    int i;
    ArcNode *p;
    for(i=0;i<G->n;i++)
    {
        p=G->adjlist[i].firstarc;
        if(p!=NULL)
            printf(" %3d: ",i);
        while(p!=NULL)//对所有相邻的顶点进行处理
        {
            printf(" %3d",p->adjvex);
            p=p->nextarc;
        }
        printf("\n");
    }
}


//主函数源文件.cpp
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
extern void MatToList(MGraph,ALGraph *&);//外部文件中
extern void ListToMat(ALGraph *,MGraph &);
extern void DispMat(MGraph);
extern void DispAdj(ALGraph *);
void main()
{
    int i,j;
    MGraph g,g1;
    ALGraph *G;
    int A[MAXV][6]=
    {
        {0,5,0,7,0,0},
        {0,0,4,0,0,0},
        {8,0,0,0,0,9},
        {0,0,5,0,0,6},
        {0,0,0,5,0,0},
        {3,0,0,0,1,0}
    };
    g.n=6;
    g.e=10;
    for(i=0;i<g.n;i++)//建立图8.1所示的邻接矩阵
        for(j=0;j<g.n;j++)
            g.edges[i][j]=A[i][j];
        printf("\n");
        printf(" 有向图G的邻接矩阵:\n");
        DispMat(g);
        G=(ALGraph *)malloc(sizeof(ALGraph));
        printf(" 图G的邻接矩阵转换成邻接表:\n");
        MatToList(g,G);
        DispAdj(G);
        printf(" 图G的邻接矩表转换成邻接矩阵:\n");
        ListToMat(G,g1);
        DispMat(g1);
        printf("\n");
}
原文地址:https://www.cnblogs.com/heqinghui/p/2617405.html