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

modify

上级 7d46d513
#ifndef CRITICALPATH_H_
#define CRITICALPATH_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
//
......@@ -27,7 +23,8 @@ typedef long BOOL;
#define FALSE 0
// Edge (arc)
typedef struct EdgeNode {
typedef struct EdgeNode
{
int vertex; // Vertex field. That is, the index of the vertex in the vertex array.
int weight; // Edge weights
struct EdgeNode* nextedge; // Chain domain. Point to the next Edge (arc).
......@@ -36,31 +33,31 @@ typedef struct EdgeNode {
}EdgeNode;
// 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;
EdgeNode* 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
//
void InitGraph(GraphList* pGraphList);
void DeleteGraph(GraphList* pGraphList);
void FindInDegree(GraphList* pGraphList);
int CriticalPath(GraphList* pGraphList);
void OutputResult(GraphList* pGraphList);
//
// Declare global variables here
//
......
......@@ -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,11 +32,8 @@ int Push(Stack* pS, int Elem);
int Pop(Stack* pS);
int StackEmpty(Stack* pS);
//
// Declare global variables here
//
#endif /* STACK_H_ */
......@@ -101,8 +101,10 @@ int CriticalPath(GraphList* pGraphList)
//
// Initializes an array of the latest occurrence times of events
//
for(i = 0; i < pGraphList->length; i++)
for (i = 0; i < pGraphList->length; i++)
{
Vl[i] = Ve[pGraphList->length - 1];
}
//
// TODO: Add the code here
......@@ -118,12 +120,14 @@ function:
parameter:
pGraphList -- Figure pointer
*/
typedef struct Arc{
typedef struct Arc
{
int Weight; // The weight.
int VexIndex; // The sequence of vertices adjacent to this vertex.
const char* Name; // Side (arc) activity name
}Arc;
typedef struct VertexArrayEntry {
typedef struct VertexArrayEntry
{
const char* Name; // Name of the vertices.NULL indicates the end of the vertex sequence.
Arc ArcArray[MAX_VERTEX_NUM]; // Edge array.
}VertexArrayEntry;
......@@ -161,17 +165,22 @@ void InitGraph(GraphList* pGraphList)
//
// 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) // 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; // Makes the pointer point to the first edge of the vertex array (where the pointer was initialized)
for(j=0; j<MAX_VERTEX_NUM; j++)
// Makes the pointer point to the first edge of
// the vertex array (where the pointer was initialized)
pEdgeNodePtr = &pGraphList->vexlist[i].firstarc;
for (j=0; j<MAX_VERTEX_NUM; j++)
{
if(NULL == VertexArray[i].ArcArray[j].Name)
if (NULL == VertexArray[i].ArcArray[j].Name)
{
*pEdgeNodePtr = NULL;
break;
......@@ -201,9 +210,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;
......@@ -228,21 +237,21 @@ void OutputResult(GraphList* pGraphList)
int ee, el; // Temporary variable
printf("VertexSeq:\t\t\t\t\t\t\t\t\t");
for(i = 0; i < pGraphList->length; i++)
for (i = 0; i < pGraphList->length; i++)
{
printf("\t%s", pGraphList->vexlist[i].name);
}
printf("\n");
printf("An array of the earliest events to occur Ve:");
for(i = 0; i < pGraphList->length; i++)
for (i = 0; i < pGraphList->length; i++)
{
printf("\t%d ", Ve[i]);
}
printf("\n");
printf("Event latest occurrentce time array Vl:\t\t");
for(i = 0; i < pGraphList->length; i++)
for (i = 0; i < pGraphList->length; i++)
{
printf("\t%d ", Vl[i]);
}
......@@ -250,10 +259,11 @@ void OutputResult(GraphList* pGraphList)
printf("AdjacencyList:\n");
printf("Vertex (VertexSubscript ActivityName Weight)...\n");
for(i = 0; i < pGraphList->length; i++)
for (i = 0; i < pGraphList->length; i++)
{
printf("%s",pGraphList->vexlist[i].name);
for(pEdgeNode = pGraphList->vexlist[i].firstarc; pEdgeNode!=NULL; pEdgeNode = pEdgeNode->nextedge)
printf("%s", pGraphList->vexlist[i].name);
for (pEdgeNode = pGraphList->vexlist[i].firstarc; pEdgeNode!=NULL;
pEdgeNode = pEdgeNode->nextedge)
{
//
// Save the vertex index that the edge (arc) node pointer points to in k
......@@ -262,13 +272,14 @@ void OutputResult(GraphList* pGraphList)
k = pEdgeNode->vertex;
//
// ee saves the value of Ve[i],le saves the value of Vl[k] and subtracts the weight corresponding to the edge node pointer
// ee saves the value of Ve[i],le saves the value of Vl[k]
// and subtracts the weight corresponding to the edge node pointer
//
ee = Ve[i];
el = Vl[k] - pEdgeNode->weight;
if(ee == el)
if (ee == el)
{
printf(" -> (%2d [%s=%d])", pEdgeNode->vertex, pEdgeNode->name, pEdgeNode->weight);
pEdgeNode->isCritical = TRUE;
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论