每天进步一点点之图的表示方法

1,邻接矩阵表示方法

#define MaxVertexNum 10
typedef char VertexType;
typedef int EdgeType;
typedef struct{
    VertexType Vex[MaxVertexNum];
    EdgeType Edge[MaxVertexNum][MaxVertexNum];
    int vexnum,arcnum;
}MGraph;
int G[MAX][MAX],Nv,Ne;
void buildGraph(){
    int i,j,v1,v2,w;
    scanf("%d",&Nv);
    for(i=0;i<Nv;i++){
        for(j=0;j<Nv;j++)
            G[i][j]=0;
    }
    scanf("%d",&Ne);
    for(i=0;i<Ne;i++){
        scanf("%d %d %d",&v1,&v2,&w);
        G[v1][v2] = w;
        G[v2][v1]= w;
    }
}

优点:简单

缺点:需要提前分配大量内存

2,临界表表示方法

#define MaxVertexNum 100
typedef struct ArcNode{
    int adjvex;
    ArcNode *next;
};
typedef struct VNode{
    VertextType data;
    ArcNode *first;
}AdjList[MaxVertexNum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;

#define MaxVertexNum 100typedef struct ArcNode{int adjvex;ArcNode *next;};typedef struct VNode{VertextType data;ArcNode *first;}AdjList[MaxVertexNum];typedef struct{AdjList vertices;int vexnum,arcnum;}ALGraph;

原文地址:https://www.cnblogs.com/lixiangfu/p/13363384.html