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

modify

上级 cc40ac6c
...@@ -30,8 +30,10 @@ int Push(Stack* pS, int Elem) ...@@ -30,8 +30,10 @@ int Push(Stack* pS, int Elem)
// //
// Stack full, push failed. // Stack full, push failed.
// //
if(MAX_STACK_LENGTH-1 < pS->top) if (MAX_STACK_LENGTH-1 < pS->top)
{
return -1; return -1;
}
pS->top++; pS->top++;
pS->buffer[pS->top] = Elem; // Insert the element at the top of the stack pS->buffer[pS->top] = Elem; // Insert the element at the top of the stack
...@@ -56,8 +58,10 @@ int Pop(Stack* pS) ...@@ -56,8 +58,10 @@ int Pop(Stack* pS)
// //
// The stack is empty and the pop fails // The stack is empty and the pop fails
// //
if(StackEmpty(pS)) if (StackEmpty(pS))
{
return -1; return -1;
}
Elem = pS->buffer[pS->top]; Elem = pS->buffer[pS->top];
pS->top--; pS->top--;
......
#ifndef STACK_H_ #ifndef STACK_H_
#define STACK_H_ #define STACK_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,12 +16,13 @@ ...@@ -21,12 +16,13 @@
#define MAX_STACK_LENGTH 64 // Maximum length of stack #define MAX_STACK_LENGTH 64 // Maximum length of stack
// Stack // Stack
typedef struct Stack{ typedef struct Stack
{
int buffer[MAX_STACK_LENGTH]; // Stack buffer int buffer[MAX_STACK_LENGTH]; // Stack buffer
int top; // Indicates the position at the top of the stack, not the number of elements in the stack // Indicates the position at the top of the stack, not the number of elements in the stack
int top;
}Stack; }Stack;
// //
// Declare the function here // Declare the function here
// //
...@@ -36,12 +32,8 @@ int Push(Stack* pS, int Elem); ...@@ -36,12 +32,8 @@ int Push(Stack* pS, int Elem);
int Pop(Stack* pS); int Pop(Stack* pS);
int StackEmpty(Stack* pS); int StackEmpty(Stack* pS);
// //
// Declare global variables here // Declare global variables here
// //
#endif /* STACK_H_ */ #endif /* STACK_H_ */
#ifndef TOPOLOGICALSORT_H_ #ifndef TOPOLOGICALSORT_H_
#define TOPOLOGICALSORT_H_ #define TOPOLOGICALSORT_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 "Stack.h" #include "Stack.h"
// //
// Define the data structure here // Define the data structure here
// //
...@@ -24,25 +20,28 @@ ...@@ -24,25 +20,28 @@
#define MAX_VERTEX_NUM 50 // Maximum number of vertices #define MAX_VERTEX_NUM 50 // Maximum number of vertices
// Edge (arc) // Edge (arc)
typedef struct EdgeNode { typedef struct EdgeNode
{
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 EdgeNode* nextedge; // Chain domain. Point to the next Edge (arc). struct EdgeNode* nextedge; // Chain domain. Point to the next Edge (arc).
}EdgeNode, *PEdgeList; }EdgeNode, *PEdgeList;
// 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;
PEdgeList firstarc; // Pointing to the first side arc. PEdgeList firstarc; // Pointing to the first side arc.
int Indegree; // indegree of vertex. int Indegree; // indegree of vertex.
}VexNode; }VexNode;
// Graph. Use adjacency list storage // Graph. Use adjacency list storage
typedef struct GraphList { typedef struct GraphList
{
VexNode vexlist[MAX_VERTEX_NUM]; // Vertex table VexNode vexlist[MAX_VERTEX_NUM]; // Vertex table
int length; // Vertex number int length; // Vertex number
}GraphList; }GraphList;
// //
// Declare the function here // Declare the function here
// //
......
#include "TopologicalSort.h" #include "TopologicalSort.h"
#include <stdlib.h>
Stack stack; Stack stack;
...@@ -82,11 +83,14 @@ function: ...@@ -82,11 +83,14 @@ function:
parameter: parameter:
pGraphList -- Figure pointer pGraphList -- 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[] =
{
{ "C1", {2, 1, 3, 11, -1}}, { "C1", {2, 1, 3, 11, -1}},
{ "C2", {2, -1} }, { "C2", {2, -1} },
{ "C3", {7, 6, 4, -1} }, { "C3", {7, 6, 4, -1} },
...@@ -96,15 +100,14 @@ const VertexArrayEntry VertexArray[] = { ...@@ -96,15 +100,14 @@ const VertexArrayEntry VertexArray[] = {
{ "C7", {-1} }, { "C7", {-1} },
{ "C8", {-1} }, { "C8", {-1} },
{ "C9", {10, 11, 9, -1} }, { "C9", {10, 11, 9, -1} },
{ "C10",{11, -1} }, { "C10", {11, -1} },
{ "C11",{5, -1} }, { "C11", {5, -1} },
{ "C12",{-1} }, { "C12", {-1} },
{ NULL } // End mark { NULL } // End mark
}; };
void InitGraph(GraphList* pGraphList) void InitGraph(GraphList* pGraphList)
{ {
int i, j; // The cursor
EdgeNode** pEdgeNodePtr; // Pointer to a pointer to an edge (arc) node pointer EdgeNode** pEdgeNodePtr; // Pointer to a pointer to an edge (arc) node pointer
// //
...@@ -115,17 +118,21 @@ void InitGraph(GraphList* pGraphList) ...@@ -115,17 +118,21 @@ void InitGraph(GraphList* pGraphList)
// //
// 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 (int i=0; i<MAX_VERTEX_NUM ;i++)
{
// Name of the vertices.NULL indicates the end of the vertex sequence.
if (NULL == VertexArray[i].name)
{ {
if(NULL == VertexArray[i].name) // Name of the vertices.NULL indicates the end of the vertex sequence.
break; break;
}
pGraphList->vexlist[i].name = VertexArray[i].name; pGraphList->vexlist[i].name = VertexArray[i].name;
pEdgeNodePtr = &pGraphList->vexlist[i].firstarc; // Pointer to vertex array (where pointer is initialized) // Pointer to vertex array (where pointer is initialized)
for(j=0; j<MAX_VERTEX_NUM; j++) pEdgeNodePtr = &pGraphList->vexlist[i].firstarc;
for (int j=0; j<MAX_VERTEX_NUM; j++)
{ {
if(-1 == VertexArray[i].VexIndex[j]) if (-1 == VertexArray[i].VexIndex[j])
{ {
*pEdgeNodePtr = NULL; *pEdgeNodePtr = NULL;
break; break;
...@@ -152,9 +159,9 @@ void DeleteGraph(GraphList* pGraphList) ...@@ -152,9 +159,9 @@ void DeleteGraph(GraphList* pGraphList)
int i; int i;
EdgeNode* pEdgeNode; EdgeNode* pEdgeNode;
for(i=0; i<pGraphList->length; i++) for (i=0; i<pGraphList->length; i++)
{ {
while(pGraphList->vexlist[i].firstarc != NULL) while (pGraphList->vexlist[i].firstarc != NULL)
{ {
pEdgeNode = pGraphList->vexlist[i].firstarc; pEdgeNode = pGraphList->vexlist[i].firstarc;
pGraphList->vexlist[i].firstarc = pEdgeNode->nextedge; pGraphList->vexlist[i].firstarc = pEdgeNode->nextedge;
...@@ -168,7 +175,7 @@ void DeleteGraph(GraphList* pGraphList) ...@@ -168,7 +175,7 @@ void DeleteGraph(GraphList* pGraphList)
void OutputSortResult(GraphList *pGraphList) void OutputSortResult(GraphList *pGraphList)
{ {
int i; int i;
for(i = 0; i < ResultLength; i++) for (i = 0; i < ResultLength; i++)
{ {
printf("%d ", TopoSortResult[i]); printf("%d ", TopoSortResult[i]);
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论