Untitled

 avatar
unknown
plain_text
2 years ago
5.5 kB
3
Indexable
#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");
    }
}

// Main function
int main() {
    Task* todoList = 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. 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(&todoList, 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(&todoList, task);
                break;

            case 3:
                displayTasks(todoList);
                break;

            case 4:
                markCompletedTasks(&todoList);
                break;

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

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

    } while (choice != 5);

    // Free the list
    freeList(&todoList);

    return 0;
}
Editor is loading...