邻接矩阵和邻接表相互转换C++

邻接矩阵及邻接表数据结构:

const int MaxVexNum =10;//顶点的最大个数
typedef char VerTexData;//顶点数据类型
typedef int EdgeData;//边权值的类型
typedef struct
{
        VerTexData VexList1[MaxVexNum];//顶点表
        EdgeData edge[MaxVexNum][MaxVexNum];//邻接矩阵
        int n,e;//图中当前的顶点数和边数
}MTGraph;
typedef struct node//边节点
{
    int dest;//目标节点位置
    EdgeData cost;//边的权值
    struct node *link;//下一边链接指针
}EdgeNode;
typedef struct//顶点结点
{
    VerTexData data;//顶点数据域
    EdgeNode *adj;//边链表头指针
}VerTexNode;
typedef struct//图的邻接表
{
    VerTexNode VexList[MaxVexNum];//邻接表
    int n,e;//图中当前的顶点个数与边数
}AdjGraph;

函数原型:

void CreatGraph1(MTGraph *G);//创建有向图的邻接矩阵

void MatrixToList(MTGraph *g, AdjGraph &G);//邻接矩阵转为邻接表

void ShowGraph(AdjGraph G);//输出邻接表

void CreatGraph(AdjGraph &G);//创建邻接表

void ListToMatrix(AdjGraph &G,MTGraph *g);//邻接表转为邻接矩阵

void ShowGraph1(MTGraph G);//输出有向图的邻接矩阵

代码如下:

#include <iostream>
using namespace std;
#define INFINITY 65535
const int MaxVexNum =10;//顶点的最大个数
typedef char VerTexData;//顶点数据类型
typedef int EdgeData;//边权值的类型
typedef struct
{
        VerTexData VexList1[MaxVexNum];//顶点表
        EdgeData edge[MaxVexNum][MaxVexNum];//邻接矩阵
        int n,e;//图中当前的顶点数和边数
}MTGraph;
typedef struct node//边节点
{
    int dest;//目标节点位置
    EdgeData cost;//边的权值
    struct node *link;//下一边链接指针
}EdgeNode;
typedef struct//顶点结点
{
    VerTexData data;//顶点数据域
    EdgeNode *adj;//边链表头指针
}VerTexNode;
typedef struct//图的邻接表
{
    VerTexNode VexList[MaxVexNum];//邻接表
    int n,e;//图中当前的顶点个数与边数
}AdjGraph;

void CreatGraph1(MTGraph *G)//创建有向图的邻接矩阵
{
        cout<<"创建邻接矩阵\n顶点个数和边个数分别为:\t";
        cin>>G->n>>G->e;
        cout<<"请输入节点信息:\n";
        for(int i=0;i<G->n;i++)
        {

                 cin>>&(G->VexList1[i]);
        }
        for(int i=0;i<G->n;i++)//初始化邻接矩阵
            for(int j=0;j<G->n;j++)
               {
                   if(i==j)G->edge[i][j]=0;
                   else G->edge[i][j]=INFINITY;
               }
        int i,j,w;
        for(int k=0;k<G->e;k++)
        {
             cout<<"输入边(vi,vj)的下标i,下标j和权重w:\t";
             cin>>i>>j>>w;
             G->edge[i][j]=w;
        }
}
void MatrixToList(MTGraph *g, AdjGraph &G)//邻接矩阵转为邻接表
{
    CreatGraph1(g);G.n=g->n;G.e=g->e;
    for(int i=0;i<g->n;i++)
    {
        G.VexList[i].data=g->VexList1[i];
        G.VexList[i].adj=NULL;
    }
    for(int i=0;i<g->n;i++)
    {  
        for(int j=0;j<g->n;j++)
        {
            if((g->edge[i][j]!=0)&&(g->edge[i][j]!=INFINITY))
            {
                EdgeNode *p=new EdgeNode;
                p->dest=j;p->cost=g->edge[i][j];
                p->link=G.VexList[i].adj;
                G.VexList[i].adj=p;
            }
        }
    }
}
void ShowGraph(AdjGraph G)//输出邻接表
{
    cout<<"邻接矩阵转为邻接表运行结果为:\n";
    for(int i=0;i<G.n;i++)
    {
        cout<<G.VexList[i].data;
        EdgeNode *p=G.VexList[i].adj;
        while(p!=NULL)
        {
           cout<<" -> ("<<p->dest<<","<<p->cost<<")";
           p=p->link;
        }
    cout<<"\n";
    }
}
void CreatGraph(AdjGraph &G)//创建邻接表
{
    int tail,head,weight;
    cout<<"创建邻接表\n输入图的顶点数和边数:\t"<<endl;
    cin>>G.n>>G.e;
    cout<<"输入顶点:\n";
    for(int i=0;i<G.n;i++)//输入顶点信息
    {
      cin>>G.VexList[i].data;
       G.VexList[i].adj=NULL;
    }
    cout<<"逐条边输入,分别输入尾,头,权重:\n";
    for(int i=0;i<G.e;i++)
    {
        cin>>tail>>head>>weight;//逐条边输入
        EdgeNode *p=new EdgeNode;
        p->dest=head;p->cost=weight;
        p->link=G.VexList[tail].adj;
        G.VexList[tail].adj=p;
    }
}
void ListToMatrix(AdjGraph &G,MTGraph *g)
{
    CreatGraph(G);G.n=g->n;G.e=g->e;
    for(int i=0;i<g->n;i++)
    {
        g->VexList1[i]=G.VexList[i].data;
        for(int j=0;j<g->e;j++)
        {
            if(i==j) g->edge[i][j]=0;
            else
                 g->edge[i][j]=INFINITY;
        }
    }
    for(int i=0;i<G.n;i++)
    {
        EdgeNode *p=G.VexList[i].adj;
        while(p!=NULL)
        {
            int j=p->dest;
            g->edge[i][j]=p->cost;
            p=p->link;
        }
    }
}
void ShowGraph1(MTGraph G)//输出有向图的邻接矩阵
{
       cout<<"输出图的邻接矩阵为:\n";
        for(int i=0;i<G.n;i++)
        {
                for(int j=0;j<G.n;j++)
                {
                        cout<<G.edge[i][j]<<"\t";
                        if(j==G.n-1) cout<<"\n";
                }
        }
}
int main()
{
    MTGraph g;AdjGraph G;
    MatrixToList(&g,G);
    ShowGraph(G);
    ListToMatrix(G,&g);
    ShowGraph1(g);
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值