Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
2
Indexable
Never
struct day *remove_day(struct day *day_one,
                       struct day **current_day,
                       struct date date) {  

    if (compare_date(day_one->date, date) == 0) {
        struct day *new_day_one;

        free_crepes_of_day(day_one);
        if (day_one->next == NULL) {
            struct date new_date = {2024, 1, 1};
            new_day_one = create_day(new_date);
            *current_day = new_day_one;
        }
        else {
            new_day_one = day_one->next;
            if (compare_date((*current_day)->date, date) == 0) {
                *current_day = new_day_one;
            }
        }
        free(day_one);
        return new_day_one;

    }
    else {
        struct day *current = day_one;
        struct day *deleted_day;

        while(current->next != NULL && compare_date(date, current->next->date) > 0) {
            current = current->next;
        }

        if (current->next != NULL && compare_date(date, current->next->date) == 0) {

            deleted_day = current->next;
            if(is_cycle_to_head(deleted_day, (*current_day)->date, date)) {
                *current_day = day_one;
            } else if(compare_date((*current_day)->date, date) == 0){
                *current_day = deleted_day->next;
            }
            free_crepes_of_day(deleted_day);
            current->next = deleted_day->next;
            free(deleted_day);
            return day_one;
        }

    }
    return NULL;

}
Leave a Comment