无向无权图的邻接矩阵和邻接表的输出

1函数

1.1向邻接表中插入一个数据,并保证邻接表的有序性

void insertIntoAdjVertics(struct vNode** adjVertics, int start, int end)//向邻接表中插入一个数据,并保证邻接表的有序性
{
    struct vNode* node = (struct vNode*)malloc(sizeof(struct vNode));
    struct vNode* head = adjVertics[start];
    node->value = end;
    node->next = NULL;
    if (head == NULL) {
        adjVertics[start] = node;
        return;
    }
    if (head->next == NULL && head->value > end) {
        node->next = head;
        adjVertics[start] = node;
        return;
    }
    while (head->next != NULL && head->next->value < end) {
        head = head->next;
    }
    if (head->next == NULL) {
        head->next = node;
        return;
    }
    node->next = head->next;
    head->next = node;
}

1.2输出邻接矩阵

void displayedges(MGraph g, int n) //打印邻接矩阵
{
    int i, j;
    printf("\n");
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            printf("%d ", g.edges[i][j]);
        }
        printf("\n");
    }
}

1.3输出邻接表

void displayAdjVertice(struct vNode** adjVertics, int n) //打印邻接表
{
    int i;
    for (i = 1; i <= n; i++) {
        struct vNode* head = adjVertics[i];
        printf("%d: ", i);
        while (head != NULL) {
            printf("->%d ", head->value);
            head = head->next;
        }
        printf("\n");
    }
}

1.4主函数

int main() {
    MGraph g;

    int n, e, i, j;
    int start, end;

    printf("input vertex and edge number: ");
    scanf_s("%d%d", &n, &e);

    for (i = 1; i <= n; i++) {          //初始化邻接矩阵
        for (j = 1; j <= n; j++) {
            g.edges[i][j] = 0;
        }
    }

    struct vNode** adjVertics;
    adjVertics = (struct vNode**)malloc(sizeof(struct vNode*) * n);

    for (i = 1; i <= n; i++) {
        adjVertics[i] = NULL;
    }

    i = 0;
    //输入定点,格式为 a b
    printf("input start and end vertex, format a b \n");
    while (1) {
        scanf_s("%d", &start);
        scanf_s("%d", &end);

        g.edges[start][end] = 1;        //对称矩阵
        g.edges[end][start] = 1;

        insertIntoAdjVertics(adjVertics, start, end);
        insertIntoAdjVertics(adjVertics, end, start);

        i++;
        if (i == e) {
            break;
        }
    }


    displayedges(g, n);
    printf("-------------\n-------------\n-------------\n");
    displayAdjVertice(adjVertics, n);
    system("pause");
    return EXIT_SUCCESS;
}

2可执行代码

#define  MAXV  20
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

//图的邻接矩阵
typedef struct              //图的定义
{
    int edges[MAXV][MAXV];  //邻接矩阵
    int n, e;           //顶点数,弧数
} MGraph;               //图的邻接矩阵表示类型

//邻接表中的链表节点
struct vNode {
    int value;
    struct vNode* next;
};

void insertIntoAdjVertics(struct vNode** adjVertics, int start, int end);//向邻接表中插入一个数据,并保证邻接表的有序性
void displayedges(MGraph g, int n);//打印邻接矩阵
void displayAdjVertice(struct vNode** adjVertics, int n);//打印邻接表


int main() {
    MGraph g;

    int n, e, i, j;
    int start, end;

    printf("input vertex and edge number: ");
    scanf_s("%d%d", &n, &e);

    for (i = 1; i <= n; i++) {          //初始化邻接矩阵
        for (j = 1; j <= n; j++) {
            g.edges[i][j] = 0;
        }
    }

    struct vNode** adjVertics;
    adjVertics = (struct vNode**)malloc(sizeof(struct vNode*) * n);

    for (i = 1; i <= n; i++) {
        adjVertics[i] = NULL;
    }

    i = 0;
    //输入定点,格式为 a b
    printf("input start and end vertex, format a b \n");
    while (1) {
        scanf_s("%d", &start);
        scanf_s("%d", &end);

        g.edges[start][end] = 1;        //对称矩阵
        g.edges[end][start] = 1;

        insertIntoAdjVertics(adjVertics, start, end);
        insertIntoAdjVertics(adjVertics, end, start);

        i++;
        if (i == e) {
            break;
        }
    }


    displayedges(g, n);
    printf("-------------\n-------------\n-------------\n");
    displayAdjVertice(adjVertics, n);
    system("pause");
    return EXIT_SUCCESS;
}


void insertIntoAdjVertics(struct vNode** adjVertics, int start, int end)//向邻接表中插入一个数据,并保证邻接表的有序性
{
    struct vNode* node = (struct vNode*)malloc(sizeof(struct vNode));
    struct vNode* head = adjVertics[start];
    node->value = end;
    node->next = NULL;
    if (head == NULL) {
        adjVertics[start] = node;
        return;
    }
    if (head->next == NULL && head->value > end) {
        node->next = head;
        adjVertics[start] = node;
        return;
    }
    while (head->next != NULL && head->next->value < end) {
        head = head->next;
    }
    if (head->next == NULL) {
        head->next = node;
        return;
    }
    node->next = head->next;
    head->next = node;
}



void displayedges(MGraph g, int n) //打印邻接矩阵
{
    int i, j;
    printf("\n");
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            printf("%d ", g.edges[i][j]);
        }
        printf("\n");
    }
}



void displayAdjVertice(struct vNode** adjVertics, int n) //打印邻接表
{
    int i;
    for (i = 1; i <= n; i++) {
        struct vNode* head = adjVertics[i];
        printf("%d: ", i);
        while (head != NULL) {
            printf("->%d ", head->value);
            head = head->next;
        }
        printf("\n");
    }
}

3运行结果展示

1641255-20190528085849531-1913834139.png

转载于:https://www.cnblogs.com/illusory---/p/10933781.html

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值