代码如下(附解析):

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAX_VERTEX_NUM 20//最大顶点个数

typedef int Status;
typedef int InfoType;
typedef char VertexType;

bool visited[MAX_VERTEX_NUM];

typedef struct ArcNode {
int adjvex;//该弧所指向的顶点位置
struct ArcNode *nextarc;//指向下一条弧的指针
InfoType info;//该弧相关信息的指针
}ArcNode;


typedef struct VNode {
VertexType data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct {
AdjList vertices;
int vexnum, arcnum;//图的当前顶点数和弧数
int kind;//图的种类标志
}ALGraph;

int LocateVex(ALGraph G, char v)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
if (G.vertices[i].data == v)
return i;
}
return -1;
}
/*
采用邻接表存储表示,构造无向图G
*/
Status CreateUDG(ALGraph &G)
{
int i, j, k, IncInfo;
ArcNode *pi, *pj;
char v1, v2;
cout << "输入顶点数G.vexnum:";
cin >> G.vexnum;
cout << "输入边数G.arcnum:";
cin >> G.arcnum;
getchar();
for (i = 0; i < G.vexnum; ++i)
{
cout << "输入顶点G.vertices[" << i << "].data:";
cin >> G.vertices[i].data;
getchar();
//初始化链表头指针为空
G.vertices[i].firstarc = NULL;
}
/*
输入各边并构造邻接表
*/
for (k = 0; k < G.arcnum; ++k)
{
cout << "请输入第" << k + 1 << "条边的两个顶点:"<<endl;
//输入一条边的起点和终点
cin >> v1;
cin >> v2;
getchar();
//确定V1和V2在G中的位置
i = LocateVex(G, v1);
j = LocateVex(G, v2);
if (!(pi = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
pi->adjvex = j;
pi->info = 0;
//插入链表G.vertices[i]
pi->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = pi;

if (!(pj = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
//插入链表G.vertices[j]
pj->adjvex = i;
pj->info = 0;
pj->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = pj;
}//for
return OK;
}//CreateUDG

//有向图
Status CreateDG(ALGraph &G)
{
int i, j, k, IncInfo;
ArcNode *pi, *pj;
char v1, v2;
cout << "输入顶点数G.vexnum:";
cin >> G.vexnum;
cout << "输入边数G.arcnum:";
cin >> G.arcnum;
getchar();
for (i = 0; i < G.vexnum; ++i)
{
cout << "输入顶点G.vertices[" << i << "].data:";
cin >> G.vertices[i].data;
getchar();
//初始化链表头指针为空
G.vertices[i].firstarc = NULL;
}
/*
输入各边并构造邻接表
*/
for (k = 0; k < G.arcnum; ++k)
{
cout << "请输入第" << k + 1 << "条边的两个顶点:" << endl;
//输入一条边的起点和终点
cin >> v1;
cin >> v2;
getchar();
//确定V1和V2在G中的位置
i = LocateVex(G, v1);
j = LocateVex(G, v2);
if (!(pi = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
pi->adjvex = j;
pi->info = 0;
//插入链表G.vertices[i]
pi->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = pi;
}//for
return OK;
}//CreateDG
//有向网
Status CreateDN(ALGraph &G)
{
int i, j, k, IncInfo;
ArcNode *pi, *pj;
char v1, v2;
cout << "输入顶点数G.vexnum:";
cin >> G.vexnum;
cout << "输入边数G.arcnum:";
cin >> G.arcnum;
cout << "输入是否有Info信息(0、没有,1、有)";
cin >> IncInfo;
getchar();
for (i = 0; i < G.vexnum; ++i)
{
cout << "输入顶点G.vertices[" << i << "].data:";
cin >> G.vertices[i].data;
getchar();
//初始化链表头指针为空
G.vertices[i].firstarc = NULL;
}
/*
输入各边并构造邻接表
*/
for (k = 0; k < G.arcnum; ++k)
{
cout << "请输入第" << k + 1 << "条边的两个顶点和相连边的权值:" << endl;
//输入一条边的起点和终点
cin >> v1;
cin >> v2;
cin >> IncInfo;
getchar();
//确定V1和V2在G中的位置
i = LocateVex(G, v1);
j = LocateVex(G, v2);
if (!(pi = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
pi->adjvex = j;
//插入链表G.vertices[i]
pi->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = pi;

pi->info = IncInfo;
}//for
return OK;
}//CreateDN
//无向网
Status CreateUDN(ALGraph &G)
{
int i, j, k, IncInfo;
ArcNode *pi, *pj;
char v1, v2;
cout << "输入顶点数G.vexnum:";
cin >> G.vexnum;
cout << "输入边数G.arcnum:";
cin >> G.arcnum;
cout << "输入是否有Info信息(0、没有,1、有)";
cin >> IncInfo;
getchar();
for (i = 0; i < G.vexnum; ++i)
{
cout << "输入顶点G.vertices[" << i << "].data:";
cin >> G.vertices[i].data;
getchar();
//初始化链表头指针为空
G.vertices[i].firstarc = NULL;
}
/*
输入各边并构造邻接表
*/
for (k = 0; k < G.arcnum; ++k)
{
cout << "请输入第" << k + 1 << "条边的两个顶点和相连边的权值:" << endl;
//输入一条边的起点和终点
cin >> v1;
cin >> v2;
cin >> IncInfo;
getchar();
//确定V1和V2在G中的位置
i = LocateVex(G, v1);
j = LocateVex(G, v2);
if (!(pi = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
pi->adjvex = j;
//插入链表G.vertices[i]
pi->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = pi;
pi->info = IncInfo;
if (!(pj = (ArcNode *)malloc(sizeof(ArcNode))))
exit(OVERFLOW);
//对弧结点赋邻接点"位置"信息
pj->adjvex = i;
//插入链表G.vertices[i]
pj->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = pi;
pj->info = IncInfo;
}//for
return OK;
}//CreateUDN


Status CreateGraph(ALGraph &G)
{
cout << "请输入图的种类:0表示DG,1表示DN,2表示UDG,3表示UDN" << endl;
cin >> G.kind;
switch (G.kind)
{
case 0:
return CreateDG(G);
case 1:
return CreateDN(G);
case 2:
return CreateUDG(G);
case 3:
return CreateUDN(G);
default:
return ERROR;
}
}

void list(ALGraph G)
{
int i;
ArcNode *p;
cout << "输出邻接表((0)代表无权值):" << endl;
for (i = 0; i < G.vexnum; ++i)
{
cout << i << ":" << G.vertices[i].data;
p = G.vertices[i].firstarc;
while (p)
{
cout << setw(3) << p->adjvex;
cout << "(" <<p->info<< ")";
p = p->nextarc;
}
cout << endl;
}
}


void DFS(ALGraph G, int v)
{
ArcNode *p;
int w;
visited[v] = TRUE;
cout << G.vertices[v].data << " ";

for (p = G.vertices[v].firstarc; p; p = p->nextarc)
{
w = p->adjvex;
if (!visited[w])//对v的尚未访问的邻接顶点w递归调用DFS
DFS(G, w);
}
}
//深度优先遍历
void DFSTraverse(ALGraph G)
{
int v;
for (v = 0; v < G.vexnum; ++v)
{
visited[v] = FALSE;
}
for (v = 0; v < G.vexnum; ++v)
{
if (!visited[v])
{
DFS(G, v);
cout << endl;
}
}
}

void BFSTraverse(ALGraph G)
{
int v,w;
//辅助队列
int front=0, rear = 0;
VNode Queue[MAX_VERTEX_NUM];
ArcNode *p;

//初始化标志数组
for (v = 0; v < G.vexnum; ++v)
{
visited[v] = FALSE;
}
for (v = 0; v < G.vexnum; ++v)
{
if (!visited[v])//v尚未访问
{
visited[v] =true;
cout << G.vertices[v].data<<" ";
Queue[++rear] = G.vertices[v];//v入队列
while (front != rear)//判断队列是否为空
{
VNode u = Queue[++front];//队头元素出队列,并等于u
cout << endl;
/*
判断该结点的后序表结点,存在即输出,入队列
*/
for (p = u.firstarc; p; p = p->nextarc)
{
w = p->adjvex;
if (!visited[w])
{
visited[w] = true;
cout << G.vertices[w].data << " ";
Queue[++rear] = G.vertices[w];
}//if
}//for
}//while
}//if
}//for
}//BFSTraverse

void main()
{
ALGraph G;
CreateGraph(G);
list(G);
//深度优先遍历图
cout << "深度优先遍历:";
DFSTraverse(G);
cout << endl;
//广度优先遍历
cout << "广度优先遍历:";
BFSTraverse(G);
system("pause");
}