当我必须将其退还时,如何释放内存块?

I have this function for my linked list, and i have to return it as a journal (struct). when i run valgrind, i found out that i have a memory leak here, which is the malloc. I am wondering to how can i free entry? or is there a way to return the data inside the entry without creating a new object(? idk what its called)

Journal *removeStart(LinkedList *list) {
    Node *front;
    Journal *entry = malloc(sizeof(Journal));
    front = list->head;
    entry = front->data;
    if (list->head==NULL) {
        entry = NULL;
    }

    list->head = list->head->next;
    front->next = NULL;

    if (front == list->head) {
        list->head = NULL;
    }

    free(front);
    return entry;

}

Journal *removeLast(LinkedList *list) {
    Node *end;
    Journal *entry = malloc(sizeof(Journal));


    if (list->head == NULL) {
        entry = NULL;
    }

    end = list->head;
    while (end->next != NULL) {
        end = end->next;
    }
    if (list->head->next==NULL) {
        entry = list->head->data;
        list->head = NULL;
    }
    else {
        entry = list->head->data;
        end->prev->next = NULL;
    }


    free(end);
    return entry;

}

我的日记结构是

#define SIZE 102
typedef struct {
    int day;
    int month;
    int year;
    char entry[SIZE];
} Journal;
评论