邻接矩阵与邻接表的构建与输出

邻接矩阵与邻接表的构建与输出


#include <cstdio>
#include <cstring>
#include <cstdlib>

#define MAXV 6
#define INF 0x3f3f3f3f
using namespace std;

typedef int InfoType;

//邻接矩阵/

typedef struct {
    int no; //结点编号
    InfoType info; //结点信息,此处保存结点的出度数
} VertexType; //顶点结点

typedef struct {
    int edges[MAXV][MAXV]; //保存弧, i = 弧头编号, j = 弧尾编号, edges[i][j] = 弧长
    int n, e; //n = 结点数, e = 弧数
    VertexType vexs[MAXV]; //储存图的全部结点信息
} MatrixGraph; //邻接矩阵结构

void MatrixGraphInit(MatrixGraph &M, int map[MAXV][MAXV]) {
    memset(&M, 0, sizeof(M));
    for (int i = 0; i < MAXV; i++) {
        M.vexs[i].no = i;
        M.n++;
        for (int j = 0; j < MAXV; j++) {
            M.edges[i][j] = map[i][j];
            if (map[i][j] != 0 && map[i][j] != INF) {
                M.e++;
                M.vexs[i].info++;
            }
        }
    }
}

void MatrixGraphOutput(MatrixGraph &M) {
    printf("邻接矩阵信息如下: \n");
    printf("\t该邻接矩阵共有%d个结点与%d条弧\n", M.n, M.e);
    printf("\t结点信息如下: \n");
    for (int i = 0; i < MAXV; i++)
        printf("\t\t结点编号%d: 共有%d个出度\n", M.vexs[i].no, M.vexs[i].info);
    printf("\t弧的具体信息如下: (行 = 弧头, 列 = 弧尾, value = 边长, 行列从0开始计数)\n");
    for (int i = 0; i < MAXV; i++) {
        printf("\t\t");
        for (int j = 0; j < MAXV; j++)
            if (M.edges[i][j] == INF)
                printf("%s ", "∞");
            else
                printf("%d ", M.edges[i][j]);
        putchar(10);
    }
    putchar(10);

}

//邻接表///

typedef struct ArcNode {
    int adjvex, value; //adjvex = 该边的邻接点编号, value = 边的信息, 此处存储的是边长
    ArcNode *next; //该边的头结点指向的下一条边的指针
} ArcNode; //边结点

typedef struct {
    InfoType info; //头结点的信息, 此处存储的是该点共有多少个出度
    ArcNode *first; //该点指向的第一条边
} VNode; //头结点

typedef struct {
    VNode adjacencyList[MAXV]; //头结点数组
    int n, e; //n = 节点数, e = 边数
} ListGraph; //邻接表结构

void ListGraphInit(ListGraph &L, int map[MAXV][MAXV]) {
    memset(&L, 0, sizeof(L));
    for (int i = 0; i < MAXV; i++) {
        L.n++;
        int mark = 0;
        for (int j = 0; j < MAXV; j++) {
            if (map[i][j] != 0 && map[i][j] != INF) {
                L.e++;
                L.adjacencyList[i].info++;
                if (!mark) {
                    mark++;
                    L.adjacencyList[i].first = (ArcNode *) malloc(sizeof(ArcNode));
                    L.adjacencyList[i].first->adjvex = j;
                    L.adjacencyList[i].first->value = map[i][j];
                    L.adjacencyList[i].first->next = nullptr;
                } else {
                    ArcNode *p = L.adjacencyList[i].first, *q;
                    while (p)
                        q = p, p = p->next;
                    p = (ArcNode *) malloc(sizeof(ArcNode));
                    q->next = p;
                    p->value = map[i][j];
                    p->adjvex = j;
                    p->next = nullptr;
                }
            }
        }
    }
}

void ListGraphOutput(ListGraph &L) {
    printf("邻接表信息如下: \n");
    printf("\t该邻接表共有%d个结点与%d条边\n", L.n, L.e);
    printf("\t结点信息如下: \n");
    for (int i = 0; i < MAXV; i++)
        printf("\t\t结点编号%d: 共有%d个出度\n", i, L.adjacencyList[i].info);
    printf("\t弧的具体信息如下: \n");
    for (int i = 0; i < MAXV; i++) {
        printf("\t\t%d  -%d->  %d", i, L.adjacencyList[i].first->value, L.adjacencyList[i].first->adjvex);
        ArcNode *p = L.adjacencyList[i].first->next;
        while (p) {
            printf("  -%d->  %d", p->value, p->adjvex);
            p = p->next;
        }
        putchar(10);
    }
    putchar(10);
}

void ListGraphDestory(ListGraph &L) {
    for (int i = 0; i < MAXV; i++) {
        ArcNode *p = L.adjacencyList[i].first, *q;
        while (p) {
            q = p->next;
            free(p);
            p = q;
        }
    }
    printf("邻接表已被释放! \n");
}

///

int main() {
    int m = INF;
    MatrixGraph M;
    int map[MAXV][MAXV] = {
            0, 5, m, 7, m, m,
            m, 0, 4, m, m, m,
            8, m, 0, m, m, 9,
            m, m, 5, 0, m, 6,
            m, m, m, 5, 0, m,
            3, m, m, m, 1, 0
    };
    MatrixGraphInit(M, map);
    MatrixGraphOutput(M);
    ListGraph L;
    ListGraphInit(L, map);
    ListGraphOutput(L);
    ListGraphDestory(L);
    return 0;
}


运行结果

E:\Learning\Programme\Jet_Brains\CLionProjects\GraphTest\cmake-build-debug\GraphTest.exe
邻接矩阵信息如下:
        该邻接矩阵共有6个结点与10条弧
        结点信息如下:
                结点编号0: 共有2个出度
                结点编号1: 共有1个出度
                结点编号2: 共有2个出度
                结点编号3: 共有2个出度
                结点编号4: 共有1个出度
                结点编号5: 共有2个出度
        弧的具体信息如下: (= 弧头,= 弧尾, value = 边长, 行列从0开始计数)
                0 57 ∞ ∞
                ∞ 0 4 ∞ ∞ ∞
                80 ∞ ∞ 9
                ∞ ∞ 5 06
                ∞ ∞ ∞ 5 03 ∞ ∞ ∞ 1 0

邻接表信息如下:
        该邻接表共有6个结点与10条边
        结点信息如下:
                结点编号0: 共有2个出度
                结点编号1: 共有1个出度
                结点编号2: 共有2个出度
                结点编号3: 共有2个出度
                结点编号4: 共有1个出度
                结点编号5: 共有2个出度
        弧的具体信息如下:
                0  -5->  1  -7->  3
                1  -4->  2
                2  -8->  0  -9->  5
                3  -5->  2  -6->  5
                4  -5->  3
                5  -3->  0  -1->  4

邻接表已被释放!

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值