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

modify

上级 bc876638
#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_ */
......@@ -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;
......
#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_ */
#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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论