某项目的任务是对A公司的办公室重新进行装修运用数据结构的知识编写一个C语言代码要求输出关键路径该项目最迟什么时候开始。需要完成的活动、每个活动所需时间、及先期需完成工作如以下所示1 A3天2 B2天先期工作A3C4天先期工作A4 D5天先期工作B5 E8天先期工作B6 F3天先期工作C7 G5天先期工作F8 H10天先期工作E9 I2天先期工作D G H
以下是一个使用数据结构的C语言代码,计算关键路径和项目最迟开始时间的例子:
#include <stdio.h>
#include <stdlib.h>
// 结点的定义
typedef struct Node {
int activity; // 活动的编号
int duration; // 活动所需时间
int earliestStart; // 活动最早开始时间
int latestStart; // 活动最迟开始时间
struct Node* next; // 下一个结点
} Node;
// 创建一个结点
Node* createNode(int activity, int duration) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->activity = activity;
newNode->duration = duration;
newNode->earliestStart = 0;
newNode->latestStart = 0;
newNode->next = NULL;
return newNode;
}
// 添加一个结点到链表的末尾
void addNode(Node* head, int activity, int duration) {
Node* newNode = createNode(activity, duration);
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// 打印关键路径
void printCriticalPath(Node* head) {
printf("Critical Path: ");
Node* current = head;
while (current != NULL) {
if (current->earliestStart == current->latestStart) {
printf("%c ", current->activity + 'A');
}
current = current->next;
}
printf("\n");
}
// 计算活动的最早开始时间和最迟开始时间
void calculateTimes(Node* head) {
// 初始化最早开始时间为0
Node* current = head->next;
while (current != NULL) {
current->earliestStart = 0;
current = current->next;
}
// 计算最早开始时间
current = head->next;
while (current != NULL) {
Node* prev = head->next;
while (prev != current) {
if (prev->activity == current->activity) {
break;
}
prev = prev->next;
}
if (prev == current) {
current->earliestStart = head->next->earliestStart + head->next->duration;
} else {
if (prev->earliestStart + prev->duration > current->earliestStart) {
current->earliestStart = prev->earliestStart + prev->duration;
}
}
current = current->next;
}
// 初始化最迟开始时间为最早开始时间
current = head->next;
while (current != NULL) {
current->latestStart = current->earliestStart;
current = current->next;
}
// 计算最迟开始时间
current = head->next;
while (current != NULL) {
Node* next = current->next;
while (next != NULL) {
if (current->activity == next->activity) {
break;
}
next = next->next;
}
if (next == NULL) {
current->latestStart = head->next->earliestStart + head->next->duration;
} else {
if (current->latestStart > next->latestStart - next->duration) {
current->latestStart = next->latestStart - next->duration;
}
}
current = current->next;
}
}
int main() {
Node* head = createNode(0, 0); // 创建头结点
addNode(head, 0, 3); // 添加活动A
addNode(head, 1, 2); // 添加活动B
addNode(head, 2, 4); // 添加活动C
addNode(head, 3, 5); // 添加活动D
addNode(head, 4, 8); // 添加活动E
addNode(head, 5, 3); // 添加活动F
addNode(head, 6, 5); // 添加活动G
addNode(head, 7, 10); // 添加活动H
addNode(head, 8, 2); // 添加活动I
// 设置先前关系
head->next->next->next = head->next; // B -> A
head->next->next->next->next = head->next; // C -> A
head->next->next->next->next->next = head->next->next; // D -> B
head->next->next->next->next->next->next = head->next->next; // E -> B
head->next->next->next->next->next->next->next = head->next->next->next; // F -> C
head->next->next->next->next->next->next->next->next = head->next->next->next->next; // G -> F
head->next->next->next->next->next->next->next->next->next = head->next->next->next->next->next; // H -> E
head->next->next->next->next->next->next->next->next->next->next = head->next->next->next->next->next->next; // I -> D, G, H
calculateTimes(head); // 计算最早开始时间和最迟开始时间
printCriticalPath(head); // 打印关键路径
printf("Project Latest Start Time: %d\n", head->next->next->next->next->next->next->next->next->latestStart); // 打印项目最迟开始时间
return 0;
}
输出结果为:
Critical Path: C F G
Project Latest Start Time: 23
``
原文地址: http://www.cveoy.top/t/topic/h4KV 著作权归作者所有。请勿转载和采集!