修改代码,使其满足根据时间的先后依次展开:void print_schedule(Node* head) { load_schedule(head); Node* move = head->next; while (move != NULL) { printf("日期:%d-%d-%d 时间:%02d:%02d 事件:%s\n", move->event.year, move->event.month, move->event.day, move->event.hour, move->event.minute, move->event.name); move = move->next; } system("pause"); system("cls"); }
时间: 2024-03-13 20:44:37 浏览: 114
gpio-beeper.rar_The Work_schedule_work
可以在加载日程时对节点进行排序,以时间为依据。以下是修改后的代码:
```
void print_schedule(Node* head) {
load_schedule(head);
Node* move = head->next;
// 将节点按照时间排序
head->next = sort_schedule(head->next);
while (move != NULL) {
printf("日期:%d-%d-%d 时间:%02d:%02d 事件:%s\n",
move->event.year, move->event.month, move->event.day,
move->event.hour, move->event.minute, move->event.name);
move = move->next;
}
system("pause");
system("cls");
}
Node* sort_schedule(Node* head) {
Node* p = head->next;
head->next = NULL;
while (p != NULL) {
Node* curr = p;
p = p->next;
Node* move = head;
while (move->next != NULL && compare_schedule(move->next->event, curr->event) < 0) {
move = move->next;
}
curr->next = move->next;
move->next = curr;
}
return head->next;
}
int compare_schedule(Event e1, Event e2) {
if (e1.year != e2.year) {
return e1.year - e2.year;
}
if (e1.month != e2.month) {
return e1.month - e2.month;
}
if (e1.day != e2.day) {
return e1.day - e2.day;
}
if (e1.hour != e2.hour) {
return e1.hour - e2.hour;
}
return e1.minute - e2.minute;
}
```
其中,`sort_schedule` 函数是一个插入排序算法,将节点按照时间从早到晚排序。`compare_schedule` 函数用于比较两个事件的时间先后。在 `print_schedule` 函数中,先通过 `load_schedule` 函数加载日程,然后调用 `sort_schedule` 函数对节点进行排序,最后依次输出日程。
阅读全文