Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.4 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Task {
    char task[100];
    int duration;
    struct Task* next;
};

typedef struct Task Task;

// Function to create a new task node
Task* createTask(char task[], int duration) {
    Task* newTask = (Task*)malloc(sizeof(Task));
    strcpy(newTask->task, task);
    newTask->duration = duration;
    newTask->next = NULL;
    return newTask;
}

// Function to add a task to the end of the list
void addTask(Task** head, char task[], int duration) {
    Task* newTask = createTask(task, duration);

    if (*head == NULL) {
        *head = newTask;
    } else {
        Task* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newTask;
    }
}

// Function to remove a task from the list
void removeTask(Task** head, char task[]) {
    if (*head == NULL) {
        printf("Todo list is empty.\n");
    } else {
        Task* current = *head;
        Task* prev = NULL;

        while (current != NULL) {
            if (strcmp(current->task, task) == 0) {
                if (prev == NULL) {
                    // Task to be removed is the head of the list
                    *head = current->next;
                    free(current);
                    current = *head;
                } else {
                    prev->next = current->next;
                    free(current);
                    current = prev->next;
                }
            } else {
                prev = current;
                current = current->next;
            }
        }
    }
}

// Function to display all tasks in the list
void displayTasks(Task* head) {
    if (head == NULL) {
        printf("Todo list is empty.\n");
    } else {
        Task* current = head;
        printf("Tasks in the list:\n");
        while (current != NULL) {
            printf("%s (%d minutes)\n", current->task, current->duration);
            current = current->next;
        }
    }
}

// Function to free the memory of the list
void freeList(Task** head) {
    Task* current = *head;
    while (current != NULL) {
        Task* temp = current;
        current = current->next;
        free(temp);
    }
    *head = NULL;
}

// Function to ask the user which tasks have been completed and mark them accordingly
void markCompletedTasks(Task** head) {
    if (*head == NULL) {
        printf("Todo list is empty.\n");
    } else {
        printf("Mark completed tasks:\n");
        Task* current = *head;
        Task* prev = NULL;

        while (current != NULL) {
            printf("Did you complete the task: %s (%d minutes)? (y/n): ", current->task, current->duration);
            char choice;
            scanf(" %c", &choice);
            getchar();  // Clear the newline character from the input buffer

            if (choice == 'y' || choice == 'Y') {
                // Task completed, remove it from the list
                if (prev == NULL) {
                    // The task to be removed is the head of the list
                    *head = current->next;
                    free(current);
                    current = *head;
                } else {
                    prev->next = current->next;
                    free(current);
                    current = prev->next;
                }
            } else {
                // Move to the next task
                prev = current;
                current = current->next;
            }
        }

        printf("End of day. Task list updated.\n");
    }
}

// Function to push leftover tasks to the next day of the week
void pushLeftoverTasks(Task** currentDay, Task** nextDay) {
    if (*currentDay == NULL) {
        printf("No tasks to push.\n");
    } else {
        if (*nextDay != NULL) {
            Task* current = *nextDay;
            while (current->next != NULL) {
                current = current->next;
            }
            current->next = *currentDay;
        } else {
            *nextDay = *currentDay;
        }
        *currentDay = NULL;
        printf("Leftover tasks pushed to the next day.\n");
    }
}

// Main function
int main() {
    Task* monday = NULL;
    Task* tuesday = NULL;

    // Menu-driven loop
    int choice;
    char task[100];
    int duration;

    do {
        printf("\nTodo List Menu\n");
        printf("1. Add Task\n");
        printf("2. Remove Task\n");
        printf("3. Display Tasks\n");
        printf("4. Mark Completed Tasks (End of Day)\n");
        printf("5. Push Leftover Tasks to Next Day\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar();  // Clear the newline character from the input buffer

        switch (choice) {
            case 1:
                printf("Enter the task: ");
                fgets(task, sizeof(task), stdin);
                task[strcspn(task, "\n")] = '\0';  // Remove the trailing newline character

                printf("Enter the duration in minutes: ");
                scanf("%d", &duration);
                getchar();  // Clear the newline character from the input buffer

                addTask(&monday, task, duration);
                break;

            case 2:
                printf("Enter the task to remove: ");
                fgets(task, sizeof(task), stdin);
                task[strcspn(task, "\n")] = '\0';  // Remove the trailing newline character

                removeTask(&monday, task);
                break;

            case 3:
                printf("Tasks for Monday:\n");
                displayTasks(monday);
                printf("Tasks for Tuesday:\n");
                displayTasks(tuesday);
                break;

            case 4:
                markCompletedTasks(&monday);
                break;

            case 5:
                pushLeftoverTasks(&monday, &tuesday);
                break;

            case 6:
                printf("Exiting the program.\n");
                break;

            default:
                printf("Invalid choice. Please try again.\n");
        }

    } while (choice != 6);

    // Free the lists
    freeList(&monday);
    freeList(&tuesday);

    return 0;
}