C语言邻接表建立图详解

时间:2022-12-05 18:30:19

有向图

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std;
#define maxn 200
int v, e;
//表结点
typedef struct _Enode
{
    int ivex; //该边所指向的节点位置
    int value;//如果边有权值的话,就对其赋值
    struct _Enode* next_edge; //指向下一条边
}ENode,*PENode;
//头结点
typedef struct _VNode
{
    int data;
    ENode* fidt_edge;
}VNode;
 
//邻接表
typedef struct _LGraph
{
    int vex_num; //点的数量
    int edg_num; //边的数量
    VNode vexs[maxn]; //一维数组存表头节点
}LGraph;
 
LGraph* create()
{
    LGraph* pG;
    pG = (LGraph*)malloc(sizeof(LGraph));
    memset(pG, 0, sizeof(LGraph));
    pG->vex_num = v;  //顶点数
    pG->edg_num = e; //边数
    for (int i = 0; i < v; ++i) //初始化定点表的指针域为空
        pG->vexs[i].fidt_edge = NULL;
    //建立链表
    for (int i = 0; i < e; ++i)
    {
        int v1, v2;
        scanf_s("%d%d", &v1, &v2);
        ENode* p1 = (ENode*)malloc(sizeof(ENode));  //为新建的边申请空间
        p1->ivex = v2;//该边指向的节点
        // 头插法建立
        p1->next_edge = pG->vexs[v1].fidt_edge;
        pG->vexs[v1].fidt_edge = p1;
    }
    return pG;
}
int main()
{
    while (~scanf_s("%d%d", &v, &e))
    {
        if (v == 0 && e == 0)
            break;
        LGraph* pG;
        pG = create();
    }
    return 0;
}

无向图

在代码的建立链表的地方变成

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//建立链表
    for (int i = 0; i < e; ++i)
    {
        int v1, v2;
        scanf_s("%d%d", &v1, &v2);
        ENode* p1 = (ENode*)malloc(sizeof(ENode));  //为新建的边申请空间
        p1->ivex = v2;//该边指向的节点
        // 头插法建立
        p1->next_edge = pG->vexs[v1].fidt_edge;
        pG->vexs[v1].fidt_edge = p1;
        //另一条边
        ENode* p2 = (ENode*)malloc(sizeof(ENode));  //为新建的边申请空间
        p2->ivex = v1;//该边指向的节点
        // 头插法建立
        p2->next_edge = pG->vexs[v2].fidt_edge;
        pG->vexs[v2].fidt_edge = p2;
    }

邻接表存图进行拓扑排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
using namespace std;
#define maxn 200
int v, e;
//表结点
typedef struct _Enode
{
    int ivex; //该边所指向的节点位置
    struct _Enode* next_edge; //指向下一条边
}ENode,*PENode;
//头结点
typedef struct _VNode
{
    int data;
    int indegree;//记录定点的入度
    ENode* fidt_edge;
}VNode;
 
//邻接表
typedef struct _LGraph
{
    int vex_num; //点的数量
    int edg_num; //边的数量
    VNode vexs[maxn]; //一维数组存表头节点
}LGraph;
 
LGraph* create()
{
    LGraph* pG;
    pG = (LGraph*)malloc(sizeof(LGraph));
    memset(pG, 0, sizeof(LGraph));
    pG->vex_num = v;  //顶点数
    pG->edg_num = e; //边数
    for (int i = 0; i < v; ++i) //初始化定点表的指针域为空
        pG->vexs[i].fidt_edge = NULL;
    for (int i = 0; i < e; ++i)
    {
        int v1, v2;
        scanf_s("%d%d", &v1, &v2);
        ENode* p1 = (ENode*)malloc(sizeof(ENode));  //为新建的边申请空间
        p1->ivex = v2;//该边指向的节点
        // 头插法建立
        p1->next_edge = pG->vexs[v1].fidt_edge;
        pG->vexs[v1].fidt_edge = p1;
    }
    return pG;
}
void TopSort(LGraph* pG)
{
    stack<int>s;
    int count, k, i;
    ENode* p;
    for (int i = 0; i < v; ++i) //记录各个顶点的入度
    {
        //遍历整个邻接表,如果表结点的值为 i,则i对应的头结点的入度加1
        p = pG->vexs[i].fidt_edge; //获得其指向的第一条边
        while (p)
        {
            pG->vexs[p->ivex].indegree++; //该边表存的位置对应的头结点的入度数量加1
            p = p->next_edge;
        }
    }
    //将入度为0的压入栈中
    for (int i = 0; i < v; ++i)
        if (pG->vexs[i].indegree == 0)s.push(i);
    count = 0;//对输出的顶点计数
    while (!s.empty())
    {
        int k = s.top(); //取出
        s.pop();
        ++count;
        //与k节点相邻的节点的入度减1
        for (p = pG->vexs[k].fidt_edge; p; p = p->next_edge)
        {
            int to;
            to = p->ivex;
            pG->vexs[to].indegree--;
            //减为0的话就压入栈中
            if (pG->vexs[to].indegree == 0)
                s.push(to);
        }
    }
    if (count < pG->vex_num)
        printf("NO\n");
    else
        printf("YES\n");
}
int main()
{
    while (~scanf_s("%d%d", &v, &e))
    {
        if (v == 0 && e == 0)
            break;
        LGraph* pG;
        pG = create();
        TopSort(pG);
    }
    return 0;
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/qq_39838607/article/details/119895892