Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab016
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
DS Lab Group
实验项目模板
Lab016
提交
0c55b491
提交
0c55b491
8月 15, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
bc876638
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
39 行增加
和
38 行删除
+39
-38
Graph.h
Graph.h
+8
-10
Queue.c
Queue.c
+6
-2
Queue.h
Queue.h
+2
-11
main.c
main.c
+23
-15
没有找到文件。
Graph.h
浏览文件 @
0c55b491
#ifndef GRAPH_H_
#define GRAPH_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
#include "Queue.h"
//
// Define the data structure here
//
...
...
@@ -23,24 +20,27 @@
#define MAX_VERTEX_NUM 50 // Maximum number of vertices
// Edge (arc)
typedef
struct
ArcNode
{
typedef
struct
ArcNode
{
int
vertex
;
// Vertex field. That is, the index of the vertex in the vertex array.
struct
ArcNode
*
next
;
// Chain domain. Point to the next Edge (arc).
}
ArcNode
;
// vertex
typedef
struct
VexNode
{
const
char
*
name
;
// Use a string as the vertex's stored value. You can also think of it as the vertex's name.
typedef
struct
VexNode
{
// Use a string as the vertex's stored value. You can also think of it as the vertex's name.
const
char
*
name
;
struct
ArcNode
*
firstarc
;
// Pointing to the first side (arc).
}
VexNode
;
// Graph. Use adjacency list storage
typedef
struct
Graph
{
typedef
struct
Graph
{
VexNode
vexlist
[
MAX_VERTEX_NUM
];
// Vertex table
int
length
;
// Vertex number
}
Graph
;
//
// Declare the function here
//
...
...
@@ -49,12 +49,10 @@ void BreadthFirstSearch(Graph* pGraph);
void
InitGraph
(
Graph
*
pGraph
);
void
DeleteGraph
(
Graph
*
pGraph
);
//
// Declare global variables here
//
extern
int
visited
[
MAX_VERTEX_NUM
];
extern
Queue
queue
;
#endif
/* GRAPH_H_ */
Queue.c
浏览文件 @
0c55b491
...
...
@@ -31,8 +31,10 @@ int EnQueue(Queue* pQ, int Elem)
//
// Queue full, fail to join.
//
if
(
MAX_QUEUE_LENGTH
==
pQ
->
length
)
if
(
MAX_QUEUE_LENGTH
==
pQ
->
length
)
{
return
-
1
;
}
pQ
->
buffer
[
pQ
->
end
]
=
Elem
;
pQ
->
end
=
(
pQ
->
end
+
1
)
%
MAX_QUEUE_LENGTH
;
...
...
@@ -59,8 +61,10 @@ int DeQueue(Queue* pQ)
//
// Queue empty, dequeue failed
//
if
(
QueueEmpty
(
pQ
))
if
(
QueueEmpty
(
pQ
))
{
return
-
1
;
}
Elem
=
pQ
->
buffer
[
pQ
->
begin
];
pQ
->
begin
=
(
pQ
->
begin
+
1
)
%
MAX_QUEUE_LENGTH
;
...
...
Queue.h
浏览文件 @
0c55b491
#ifndef QUEUE_H_
#define QUEUE_H_
//
// Include the C standard library header file here
//
//
// Other header files are included here
//
//
// Define the data structure here
//
...
...
@@ -21,14 +16,14 @@
#define MAX_QUEUE_LENGTH 64 // Maximum queue length
// circle queue
typedef
struct
Queue
{
typedef
struct
Queue
{
int
buffer
[
MAX_QUEUE_LENGTH
];
// queue buffer
int
begin
;
// starting position
int
end
;
// end position
int
length
;
// queue size
}
Queue
;
//
// Declare the function here
//
...
...
@@ -38,12 +33,8 @@ int EnQueue(Queue* pQ, int Elem);
int
DeQueue
(
Queue
*
pQ
);
int
QueueEmpty
(
Queue
*
pQ
);
//
// Declare global variables here
//
#endif
/* QUEUE_H_ */
main.c
浏览文件 @
0c55b491
#include "Graph.h"
#include <stdlib.h>
int
visited
[
MAX_VERTEX_NUM
];
// Array of vertex access flags.0 means no access; Greater than 0 indicates the order of access.
// Array of vertex access flags.0 means no access; Greater than 0 indicates the order of access.
int
visited
[
MAX_VERTEX_NUM
];
Queue
queue
;
// The queue
int
main
(
int
argc
,
char
*
argv
[])
...
...
@@ -22,19 +23,21 @@ int main(int argc, char* argv[])
//
// Initializes an array of access flags
//
for
(
i
=
0
;
i
<
graph
.
length
;
i
++
)
for
(
i
=
0
;
i
<
graph
.
length
;
i
++
)
{
visited
[
i
]
=
0
;
}
//
// Breadth-first search
//
BreadthFirstSearch
(
&
graph
);
for
(
i
=
0
;
i
<
graph
.
length
;
i
++
)
for
(
i
=
0
;
i
<
graph
.
length
;
i
++
)
{
for
(
j
=
0
;
j
<
graph
.
length
;
j
++
)
for
(
j
=
0
;
j
<
graph
.
length
;
j
++
)
{
if
(
visited
[
j
]
==
i
+
1
)
if
(
visited
[
j
]
==
i
+
1
)
{
printf
(
"%s "
,
graph
.
vexlist
[
j
].
name
);
}
...
...
@@ -78,11 +81,14 @@ function:
parameter:
pGraph -- Figure pointer
*/
typedef
struct
VertexArrayEntry
{
typedef
struct
VertexArrayEntry
{
const
char
*
name
;
// Name of the vertices.NULL indicates the end of the vertex sequence.
int
VexIndex
[
MAX_VERTEX_NUM
];
// The sequence of vertices adjacent to this vertex.-1 indicates the end of the sequence.
// The sequence of vertices adjacent to this vertex.-1 indicates the end of the sequence.
int
VexIndex
[
MAX_VERTEX_NUM
];
}
VertexArrayEntry
;
const
VertexArrayEntry
VertexArray
[]
=
{
const
VertexArrayEntry
VertexArray
[]
=
{
{
"V0"
,
{
2
,
5
,
1
,
-
1
}
},
{
"V1"
,
{
3
,
6
,
2
,
-
1
}
},
{
"V2"
,
{
0
,
3
,
6
,
1
,
-
1
}
},
...
...
@@ -105,17 +111,19 @@ void InitGraph(Graph* pGraph)
//
// Initializes the graph's adjacency list with the given data
//
for
(
i
=
0
;
i
<
MAX_VERTEX_NUM
;
i
++
)
for
(
i
=
0
;
i
<
MAX_VERTEX_NUM
;
i
++
)
{
if
(
NULL
==
VertexArray
[
i
].
name
)
{
if
(
NULL
==
VertexArray
[
i
].
name
)
break
;
}
pGraph
->
vexlist
[
i
].
name
=
VertexArray
[
i
].
name
;
pArcNodePtr
=
&
pGraph
->
vexlist
[
i
].
firstarc
;
for
(
j
=
0
;
j
<
MAX_VERTEX_NUM
;
j
++
)
for
(
j
=
0
;
j
<
MAX_VERTEX_NUM
;
j
++
)
{
if
(
-
1
==
VertexArray
[
i
].
VexIndex
[
j
])
if
(
-
1
==
VertexArray
[
i
].
VexIndex
[
j
])
{
*
pArcNodePtr
=
NULL
;
break
;
...
...
@@ -142,9 +150,9 @@ void DeleteGraph(Graph* pGraph)
int
i
;
ArcNode
*
pArcNode
;
for
(
i
=
0
;
i
<
pGraph
->
length
;
i
++
)
for
(
i
=
0
;
i
<
pGraph
->
length
;
i
++
)
{
while
(
pGraph
->
vexlist
[
i
].
firstarc
!=
NULL
)
while
(
pGraph
->
vexlist
[
i
].
firstarc
!=
NULL
)
{
pArcNode
=
pGraph
->
vexlist
[
i
].
firstarc
;
pGraph
->
vexlist
[
i
].
firstarc
=
pArcNode
->
next
;
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论