邻接矩阵转为邻接表

23 篇文章 0 订阅

 Problem Description

设有向图G,现给出其邻接矩阵,要求将其转化为邻接表(采用头插法存边)进行存储。

 Input

输入数据有多组

每组数据第一行为一个正整数n(0<n<=50),代表G的顶点数目

接下来有n行,为有向图G的邻接矩阵

 Output

对于每组数据,输出有若干行,为该有向图中所有顶点的出边信息(空表不输出任何信息),每行最后均无空格,每两组数据之间有一空行,具体格式见样例。

 Sample Input

3
0 0 1
0 0 0
1 0 0

4
0 1 0 1
1 0 1 0
1 0 0 1
0 0 0 0

 Sample Output

0->2
2->0

0->3->1
1->2->0
2->3->0
#include <iostream>
#include <stdlib.h>
#define maxvex 20
#include<stdio.h>
using namespace std;

typedef struct {
    int adjvex;

} ArcCell[maxvex][maxvex];

typedef struct {
    ArcCell arc;
    int vertexnum,arcnum;
    char vex[maxvex];
} MatrixGraph;

struct ArcNode {
    int adjvex;
    ArcNode* next;
};
typedef struct Edge {
    char vertex;
    ArcNode *firstedge;
} EdgeList[maxvex];

typedef struct {
    EdgeList adjlist;
    int vertexnum,arcnum;
} GraphList;

void CreatList(GraphList &G,int n,int e) {
    G.arcnum=e;
    G.vertexnum=n;
    for(int i=0; i<G.vertexnum; i++) {
        cin>>G.adjlist[i].vertex;
        G.adjlist[i].firstedge=NULL;
    }
    for(int i=0; i<G.arcnum; i++) {
        int a,b;
        cin>>a>>b;
        ArcNode* p=new ArcNode;
        p->adjvex=b;
        p->next=G.adjlist[a].firstedge;
        G.adjlist[a].firstedge=p;
    }

}

int Locate(GraphList G,char v) {
    int inedx=-1;
    for(int i=0; i<G.vertexnum; i++) {
        if(G.adjlist[i].vertex==v) {
            inedx=i;
            break;
        }
    }
    return inedx;

}


void CreatMat(MatrixGraph& G,int n) {


    int e=0;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin>>G.arc[i][j].adjvex;
            if(G.arc[i][j].adjvex) e++;
        }
    }

    G.arcnum=e;
    G.vertexnum=n;



}


void MatToList(GraphList& g2,MatrixGraph &g1) {
    g2.arcnum=g1.arcnum;
    g2.vertexnum=g1.vertexnum;



    for(int i=0; i<g1.vertexnum; i++) {
        g2.adjlist[i].firstedge=NULL;
        for(int j=0; j<g1.vertexnum; j++) {

            if(g1.arc[i][j].adjvex) {
                ArcNode* p=new ArcNode;
                p->adjvex=j;
                p->next= g2.adjlist[i].firstedge;
                g2.adjlist[i].firstedge=p;
            }
        }
    }

}



void print(GraphList g) {
    for(int i=0; i<g.vertexnum; i++) {
        if(g.adjlist[i].firstedge)
            cout<<i;
        for(ArcNode* p=g.adjlist[i].firstedge; p; p=p->next) {
            cout<<"->"<<p->adjvex;
        }
        if(g.adjlist[i].firstedge)
            cout<<endl;
    }


}



int main() {
   
    int n,e;
    int count=0;
    while(cin>>n) {
        if(count)cout<<endl;
        MatrixGraph g;
        CreatMat(g,n);
        GraphList g2;
        MatToList(g2,g);
        print(g2);
        count++;
    }
    return 0;
}





 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haibianyoushark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值