Complementary task for topic: 11

M Nemeth · 2023-08-29 15:21:04.634218'

Linekd list: Task management

Linekd list: Task management

Our task will be to implement a simple task management system using a doubly linked list. The task management system will allow users to add new tasks, mark tasks as completed, list all tasks, and delete tasks.

Hint: If we know the data stored in the list, we just need to put the puzzle together (add element, delete element, serch for element...) For doubly linked list there is a little bit more work, but if you understand the concent it is not harder (just more lines)

Solution
#include 
#include 
#include 

// Define the structure for a task
struct Task {
    char description[100];
    int completed;
    struct Task* next;
    struct Task* prev;
};

// Function to create a new task node with the given data
struct Task* createTask(const char* description) {
    struct Task* newTask = (struct Task*)malloc(sizeof(struct Task));
    strcpy(newTask->description, description);
    newTask->completed = 0;
    newTask->next = NULL;
    newTask->prev = NULL;
    return newTask;
}

// Function to add a new task to the end of the doubly linked list
void addTask(struct Task** head, const char* description) {
    struct Task* newTask = createTask(description);
    if (*head == NULL) {
        *head = newTask;
    } else {
        struct Task* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newTask;
        newTask->prev = current;
    }
}

// Function to mark a task as completed
void completeTask(struct Task* head, const char* description) {
    struct Task* current = head;
    while (current != NULL) {
        if (strcmp(current->description, description) == 0) {
            current->completed = 1;
            return;
        }
        current = current->next;
    }
}

// Function to list all tasks
void listTasks(struct Task* head) {
    if (head == NULL) {
        printf("Task list is empty.\n");
        return;
    }

    printf("Task List:\n");
    struct Task* current = head;
    while (current != NULL) {
        printf("%s - %s\n", current->completed ? "[X]" : "[ ]", current->description);
        current = current->next;
    }
}

// Function to delete a task by description
void deleteTask(struct Task** head, const char* description) {
    struct Task* current = *head;
    while (current != NULL) {
        if (strcmp(current->description, description) == 0) {
            if (current->prev != NULL) {
                current->prev->next = current->next;
            } else {
                *head = current->next;
            }
            if (current->next != NULL) {
                current->next->prev = current->prev;
            }
            free(current);
            printf("Task '%s' deleted from the list.\n", description);
            return;
        }
        current = current->next;
    }
    printf("Task '%s' not found in the list.\n", description);
}

// Function to destroy the entire task list and free memory
void destroyTaskList(struct Task** head) {
    struct Task* current = *head;
    struct Task* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    *head = NULL; // Set head to NULL to indicate the list is now empty
}

int main() {
    struct Task* tasks = NULL;

    addTask(&tasks, "Buy groceries");
    addTask(&tasks, "Finish homework");
    addTask(&tasks, "Exercise");

    listTasks(tasks);

    completeTask(tasks, "Finish homework");
    completeTask(tasks, "Exercise");

    listTasks(tasks);

    deleteTask(&tasks, "Buy groceries");
    listTasks(tasks);

    destroyTaskList(&tasks);
    listTasks(tasks); // This will print "Task list is empty."

    return 0;
}



Explanation

< < previous    next > >