数据结构学习笔记----【图】邻接矩阵和邻接表转换代码
转载参考:https://blog.youkuaiyun.com/qwm8777411/article/details/8990718
邻接矩阵–>邻接表
要求
带权 有向图 邻接矩阵表示 转换为 邻接表
INPUT
1.图的节点数、图的边数 m n
2. n*n 矩阵(数字间以空格空开,非零表示权)
OUTPUT
m个结点 则输出m行
对应结点连接情况
SAMPLE
6 10
0 5 0 7 0 0
0 0 4 0 0 0
8 0 0 0 0 0
0 0 5 0 0 6
0 0 5 0 0 0
3 0 0 0 1 0
0:1 3
1:2
2:0
3:2 5
4:2
5:0 4
代码实现
`# include<stdio.h>
# include <malloc.h>
#include <iostream>
using namespace std;
typedef int InfoType;
#define MAXV 100 //最大顶点个数
#define INF 32767 //INF 正无穷
//定义邻接矩阵类型
//顶点
typedef struct{
int no;//定点编号
InfoType info;//顶点其他信息
}VertexType;//顶点类型
//图
typedef struct{
int edges[MAXV][MAXV];//邻接矩阵
int vertexNum,arcNum;//顶点数 边数
VertexType vexs[MAXV];//存放顶点信息
}MGraph;//图的邻接矩阵类型
//邻接表类型
//表节点类型
typedef struct ANode{
int adjvex; //这条弧的终点 <i,j>
struct ANode *nextarc; //指向下一条边的指针
InfoType infoType;//相关信息,用于存放权值
}ArcNode;
//头节点类型
typedef struct VNode{
typedef int Vertex;
Vertex data;//顶点信息
ArcNode *firstarc;//指向第一条边
//有趣啊,顺序&结构体
}VNode;
typedef VNode AdjList[MAXV]; //AdjList是邻接表类型
typedef struct{
AdjList adjList;
int vertexNum,e;//顶点数和边数
}ALGraph;
//==============================================