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

modify

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