408_2014_图(c++全代码)

本篇文章针对节点的c++实现版本,针对本题的第二问链使结构的设计,提供一份数据结构以及一份可执行的g++文件,仅供参考

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#define random(x) (rand() % x)

using namespace std;



class LinkNode
{
public:
int ID, IP;
int Metric;
LinkNode *next;
LinkNode(int ID = 0, int IP = 0, int metric = 0)
{
this->next = NULL;
this->ID = ID;
this->IP = IP;
this->Metric = metric;
}
};

class NetNode
{
public:
int IP;
int Metric;
NetNode(int IP = 0, int metric = 0)
{
this->IP = IP;
this->Metric = metric;
}
};

class RouterHeaderNode
{
public:
LinkNode *linkNext;
NetNode *netNext;
int IP;
RouterHeaderNode *headerNext;
RouterHeaderNode(int IP = 0)
{
this->IP = IP;
this->linkNext = NULL;
this->netNext = NULL;
this->headerNext = NULL;
}
};

int main(int argc, char **argv)
{

std::ios::sync_with_stdio(false);

class RouterHeaderNode *routerHeaderNode, *headerRouter;
class LinkNode *linkNode, *tmpLinkNode;
class NetNode *netNode, *tmpNetNode;
tmpLinkNode = linkNode = NULL;
routerHeaderNode = headerRouter = NULL;

for (int routerI = 0; routerI < 5; routerI++)
{
tmpLinkNode = linkNode = NULL;
cout << "开始构建" << endl;
if (routerHeaderNode == NULL)
{
headerRouter = routerHeaderNode = new RouterHeaderNode(random(199));
}
else
{
routerHeaderNode->headerNext = new RouterHeaderNode(random(199));
routerHeaderNode = routerHeaderNode->headerNext;
}

int linkLength = 3;
linkNode = tmpLinkNode = NULL;
for (int i = 0; i < linkLength; i++)
{
if (tmpLinkNode == NULL)
{

linkNode = tmpLinkNode = new LinkNode(random(100), random(100), random(100));
}
else
{
tmpLinkNode->next = new LinkNode(random(100), random(100), random(100));
tmpLinkNode = tmpLinkNode->next;
}

cout << "插入一个link node 成功" << endl;
}
routerHeaderNode->linkNext = linkNode;
netNode = new NetNode(random(100));
routerHeaderNode->netNext = netNode;
cout << "掺入 netNode 成功" << endl;
}
routerHeaderNode = headerRouter;
while (routerHeaderNode != NULL)
{
linkNode = routerHeaderNode->linkNext;
cout << "routerIP:" << routerHeaderNode->IP << endl
<< "|->";
while (linkNode != NULL)
{
cout << "{id:" << linkNode->ID << ",ip:" << linkNode->IP << ",metric:" << linkNode->Metric << "}";
linkNode = linkNode->next;
if (linkNode != NULL)
{
cout << "->";
}
}
netNode = routerHeaderNode->netNext;
cout << "{metric:" << netNode->Metric << "}" << endl;

routerHeaderNode = routerHeaderNode->headerNext;
}

cout << "end" << endl;
}

运行结果的部分结果截取

1
2
3
4
5
6
7
8
9
10
11
routerIP:41
|->{id:67,ip:34,metric:0}->{id:69,ip:24,metric:78}->{id:58,ip:62,metric:64}{metric:0}
routerIP:86
|->{id:81,ip:27,metric:61}->{id:91,ip:95,metric:42}->{id:27,ip:36,metric:91}{metric:0}
routerIP:121
|->{id:53,ip:92,metric:82}->{id:21,ip:16,metric:18}->{id:95,ip:47,metric:26}{metric:0}
routerIP:195
|->{id:69,ip:12,metric:67}->{id:99,ip:35,metric:94}->{id:3,ip:11,metric:22}{metric:0}
routerIP:161
|->{id:64,ip:41,metric:11}->{id:53,ip:68,metric:47}->{id:44,ip:62,metric:57}{metric:0}
end