This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
ics_cli/src/list_handling.c

59 lines
1.4 KiB
C
Raw Normal View History

2023-08-15 16:31:07 +02:00
#include "list_handling.h"
2023-08-23 20:03:26 +02:00
#include "date_time_handling.h"
2023-08-15 16:31:07 +02:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_list(struct event *head) {
while (head != NULL) {
2023-08-24 01:10:53 +02:00
printf("%s\n", head->start_date);
printf("%s\n", head->end_date);
2023-08-15 16:31:07 +02:00
printf("%s\n", head->summary);
head = head->next;
}
}
void sorted_insert(struct event** head, char start_date[], char end_date[], char summary[]) {
2023-08-15 16:31:07 +02:00
struct event *new_node = malloc(sizeof(struct event));
2023-08-24 01:10:53 +02:00
strcpy((*new_node).start_date, start_date);
strcpy((*new_node).end_date, end_date);
2023-08-15 16:31:07 +02:00
strcpy((*new_node).summary, summary);
2023-08-24 01:10:53 +02:00
if (*head == NULL || strcmp((*head)->start_date, new_node->start_date) >= 0) {
2023-08-15 16:31:07 +02:00
new_node->next = *head;
*head = new_node;
}
else {
// Locate the node before the point of insertion
struct event* current = *head;
while (current->next!=NULL && strcmp(current->next->start_date, new_node->start_date) < 0)
current = current->next;
2023-08-15 16:31:07 +02:00
new_node->next = current->next;
current->next = new_node;
}
}
void free_list(struct event *head)
{
struct event* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
2023-08-24 01:10:53 +02:00
void print_upcoming(struct event *head, char current_start_date[]) {
2023-08-15 16:31:07 +02:00
while (head != NULL) {
2023-08-24 01:10:53 +02:00
if (strcmp(head->start_date, current_start_date) >= 0) {
pretty_print_date_time(head->start_date);
print_end_date(head->end_date);
2023-08-23 20:03:26 +02:00
printf("\n%s\n", head->summary);
2023-08-15 16:31:07 +02:00
}
head = head->next;
}
}