提交 ecae838e 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 768c8f99
#ifndef GRAPH_H_ #ifndef GRAPH_H_
#define GRAPH_H_ #define GRAPH_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
#include <stdio.h> #include <stdio.h>
// //
// Other header files are included here // Other header files are included here
// //
// //
// Define the data structure here // Define the data structure here
// //
...@@ -23,24 +18,27 @@ ...@@ -23,24 +18,27 @@
#define MAX_VERTEX_NUM 50 // Maximum number of vertices #define MAX_VERTEX_NUM 50 // Maximum number of vertices
// Edge (arc) // Edge (arc)
typedef struct ArcNode { typedef struct ArcNode
{
int vertex; // Vertex field. That is, the index of the vertex in the vertex array. 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). struct ArcNode* next; // Chain domain. Point to the next Edge (arc).
}ArcNode; }ArcNode;
// vertex // vertex
typedef struct VexNode { 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. {
// 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. struct ArcNode* firstarc; // Pointing to the first side arc.
}VexNode; }VexNode;
// Graph. Use adjacency list storage // Graph. Use adjacency list storage
typedef struct Graph { typedef struct Graph
{
VexNode vexlist[MAX_VERTEX_NUM]; // Vertex table VexNode vexlist[MAX_VERTEX_NUM]; // Vertex table
int length; // Vertex number int length; // Vertex number
}Graph; }Graph;
// //
// Declare the function here // Declare the function here
// //
...@@ -50,11 +48,9 @@ void InitGraph(Graph* pGraph); ...@@ -50,11 +48,9 @@ void InitGraph(Graph* pGraph);
void DeleteGraph(Graph* pGraph); void DeleteGraph(Graph* pGraph);
void OutputResult(Graph* pGraph); void OutputResult(Graph* pGraph);
// //
// Declare global variables here // Declare global variables here
// //
extern int visited[MAX_VERTEX_NUM]; extern int visited[MAX_VERTEX_NUM];
#endif /* GRAPH_H_ */ #endif /* GRAPH_H_ */
#include "Graph.h" #include "Graph.h"
#include <stdlib.h>
// Array of vertex access flags.0 means no access; Greater than 0 indicates the order of access.
int visited[MAX_VERTEX_NUM]; // Array of vertex access flags.0 means no access; Greater than 0 indicates the order of access. int visited[MAX_VERTEX_NUM];
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -16,8 +17,10 @@ int main(int argc, char* argv[]) ...@@ -16,8 +17,10 @@ int main(int argc, char* argv[])
// //
// Initializes an array of access flags // Initializes an array of access flags
// //
for(i=0; i<graph.length; i++) for (i=0; i<graph.length; i++)
{
visited[i] = 0; visited[i] = 0;
}
// //
// Depth-first search // Depth-first search
...@@ -67,11 +70,15 @@ function: ...@@ -67,11 +70,15 @@ function:
parameter: parameter:
pGraph -- Figure pointer 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. // Name of the vertices.NULL indicates the end of the vertex sequence.
const char* name;
// The sequence of vertices adjacent to this vertex.-1 indicates the end of the sequence.
int VexIndex[MAX_VERTEX_NUM];
}VertexArrayEntry; }VertexArrayEntry;
const VertexArrayEntry VertexArray[] = { const VertexArrayEntry VertexArray[] =
{
{ "V0", {2, 5, 6, 1, -1} }, { "V0", {2, 5, 6, 1, -1} },
{ "V1", {3, 6, 4, 2, -1} }, { "V1", {3, 6, 4, 2, -1} },
{ "V2", {0, 3, 6, 1, -1} }, { "V2", {0, 3, 6, 1, -1} },
...@@ -94,17 +101,19 @@ void InitGraph(Graph* pGraph) ...@@ -94,17 +101,19 @@ void InitGraph(Graph* pGraph)
// //
// Initializes the graph's adjacency list with the given data // 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; break;
}
pGraph->vexlist[i].name = VertexArray[i].name; pGraph->vexlist[i].name = VertexArray[i].name;
pArcNodePtr = &pGraph->vexlist[i].firstarc; 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; *pArcNodePtr = NULL;
break; break;
...@@ -131,9 +140,9 @@ void DeleteGraph(Graph* pGraph) ...@@ -131,9 +140,9 @@ void DeleteGraph(Graph* pGraph)
int i; int i;
ArcNode* pArcNode; 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; pArcNode = pGraph->vexlist[i].firstarc;
pGraph->vexlist[i].firstarc = pArcNode->next; pGraph->vexlist[i].firstarc = pArcNode->next;
...@@ -154,11 +163,11 @@ parameter: ...@@ -154,11 +163,11 @@ parameter:
void OutputResult(Graph* pGraph) void OutputResult(Graph* pGraph)
{ {
int i, j; int i, j;
for(i=0; i< pGraph->length; i++) for (i=0; i< pGraph->length; i++)
{ {
for(j=0; j< pGraph->length; j++) for (j=0; j< pGraph->length; j++)
{ {
if(visited[j] == i+1) if (visited[j] == i+1)
{ {
printf("%s ", pGraph->vexlist[j].name); printf("%s ", pGraph->vexlist[j].name);
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论