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

modify

上级 bc876638
#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
// //
#include "Queue.h" #include "Queue.h"
// //
// Define the data structure here // Define the data structure here
// //
...@@ -23,24 +20,27 @@ ...@@ -23,24 +20,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
// //
...@@ -49,12 +49,10 @@ void BreadthFirstSearch(Graph* pGraph); ...@@ -49,12 +49,10 @@ void BreadthFirstSearch(Graph* pGraph);
void InitGraph(Graph* pGraph); void InitGraph(Graph* pGraph);
void DeleteGraph(Graph* pGraph); void DeleteGraph(Graph* pGraph);
// //
// Declare global variables here // Declare global variables here
// //
extern int visited[MAX_VERTEX_NUM]; extern int visited[MAX_VERTEX_NUM];
extern Queue queue; extern Queue queue;
#endif /* GRAPH_H_ */ #endif /* GRAPH_H_ */
...@@ -31,8 +31,10 @@ int EnQueue(Queue* pQ, int Elem) ...@@ -31,8 +31,10 @@ int EnQueue(Queue* pQ, int Elem)
// //
// Queue full, fail to join. // Queue full, fail to join.
// //
if(MAX_QUEUE_LENGTH == pQ->length) if (MAX_QUEUE_LENGTH == pQ->length)
{
return -1; return -1;
}
pQ->buffer[pQ->end] = Elem; pQ->buffer[pQ->end] = Elem;
pQ->end = (pQ->end + 1) % MAX_QUEUE_LENGTH; pQ->end = (pQ->end + 1) % MAX_QUEUE_LENGTH;
...@@ -59,8 +61,10 @@ int DeQueue(Queue* pQ) ...@@ -59,8 +61,10 @@ int DeQueue(Queue* pQ)
// //
// Queue empty, dequeue failed // Queue empty, dequeue failed
// //
if(QueueEmpty(pQ)) if (QueueEmpty(pQ))
{
return -1; return -1;
}
Elem = pQ->buffer[pQ->begin]; Elem = pQ->buffer[pQ->begin];
pQ->begin = (pQ->begin + 1) % MAX_QUEUE_LENGTH; pQ->begin = (pQ->begin + 1) % MAX_QUEUE_LENGTH;
......
#ifndef QUEUE_H_ #ifndef QUEUE_H_
#define QUEUE_H_ #define QUEUE_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
// //
// Other header files are included here // Other header files are included here
// //
// //
// Define the data structure here // Define the data structure here
// //
...@@ -21,14 +16,14 @@ ...@@ -21,14 +16,14 @@
#define MAX_QUEUE_LENGTH 64 // Maximum queue length #define MAX_QUEUE_LENGTH 64 // Maximum queue length
// circle queue // circle queue
typedef struct Queue{ typedef struct Queue
{
int buffer[MAX_QUEUE_LENGTH]; // queue buffer int buffer[MAX_QUEUE_LENGTH]; // queue buffer
int begin; // starting position int begin; // starting position
int end; // end position int end; // end position
int length; // queue size int length; // queue size
}Queue; }Queue;
// //
// Declare the function here // Declare the function here
// //
...@@ -38,12 +33,8 @@ int EnQueue(Queue* pQ, int Elem); ...@@ -38,12 +33,8 @@ int EnQueue(Queue* pQ, int Elem);
int DeQueue(Queue* pQ); int DeQueue(Queue* pQ);
int QueueEmpty(Queue* pQ); int QueueEmpty(Queue* pQ);
// //
// Declare global variables here // Declare global variables here
// //
#endif /* QUEUE_H_ */ #endif /* QUEUE_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];
Queue queue; // The queue Queue queue; // The queue
int main(int argc, char* argv[]) int main(int argc, char* argv[])
...@@ -22,19 +23,21 @@ int main(int argc, char* argv[]) ...@@ -22,19 +23,21 @@ 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;
}
// //
// Breadth-first search // Breadth-first search
// //
BreadthFirstSearch(&graph); 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); printf("%s ", graph.vexlist[j].name);
} }
...@@ -78,11 +81,14 @@ function: ...@@ -78,11 +81,14 @@ 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. 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; }VertexArrayEntry;
const VertexArrayEntry VertexArray[] = { const VertexArrayEntry VertexArray[] =
{
{ "V0", {2, 5, 1, -1} }, { "V0", {2, 5, 1, -1} },
{ "V1", {3, 6, 2, -1} }, { "V1", {3, 6, 2, -1} },
{ "V2", {0, 3, 6, 1, -1} }, { "V2", {0, 3, 6, 1, -1} },
...@@ -105,17 +111,19 @@ void InitGraph(Graph* pGraph) ...@@ -105,17 +111,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;
...@@ -142,9 +150,9 @@ void DeleteGraph(Graph* pGraph) ...@@ -142,9 +150,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;
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论