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

modify

上级 768c8f99
#ifndef GRAPH_H_
#define GRAPH_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
//
// Define the data structure here
//
......@@ -23,24 +18,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
//
......@@ -50,11 +48,9 @@ void InitGraph(Graph* pGraph);
void DeleteGraph(Graph* pGraph);
void OutputResult(Graph* pGraph);
//
// Declare global variables here
//
extern int visited[MAX_VERTEX_NUM];
#endif /* GRAPH_H_ */
#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];
int main(int argc, char* argv[])
{
......@@ -16,8 +17,10 @@ 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;
}
//
// Depth-first search
......@@ -67,11 +70,15 @@ function:
parameter:
pGraph -- Figure pointer
*/
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.
typedef struct VertexArrayEntry
{
// 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;
const VertexArrayEntry VertexArray[] = {
const VertexArrayEntry VertexArray[] =
{
{ "V0", {2, 5, 6, 1, -1} },
{ "V1", {3, 6, 4, 2, -1} },
{ "V2", {0, 3, 6, 1, -1} },
......@@ -94,17 +101,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;
......@@ -131,9 +140,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;
......@@ -154,11 +163,11 @@ parameter:
void OutputResult(Graph* pGraph)
{
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);
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论