Hello,


Environement description:
Code:
OS:       Linux Ubuntu x86_64
glibc:    (Ubuntu EGLIBC 2.19-0ubuntu6.6)

Problem description:
Currently as an exercice I'm implementing double linked lists. I provide my working example code below. I would like to know whether my approach in defining the free_linked_list function is correct and this is how one should free the allocated space (via malloc) for the whole elements in a linked list.


My code:
Code:
#include <stdio.h>
#include <stdlib.h>



struct node_ty
{
    void *value;
    struct node_ty *next;
    struct node_ty *previous;
    struct node_ty *first;
    struct node_ty *last;
};
typedef struct node_ty * linked_list_ty;

// ---------------------------------------------------------------------------

linked_list_ty create_list(void * const value)
{
    linked_list_ty newlist = malloc(sizeof(struct node_ty));
    
    newlist->value = value;
    newlist->next = NULL;
    newlist->previous = NULL;
    newlist->first = newlist;
    newlist->last = newlist;
    
    return newlist;
}

// ---------------------------------------------------------------------------

int get_size(linked_list_ty *linked_list)
{
    linked_list_ty *tmp_list = linked_list;
    int size = 0;
    
    while(*tmp_list != NULL)
    {
        ++size;
        *tmp_list = (*tmp_list)->next;
    }
    
    return size;
}

// ---------------------------------------------------------------------------

void append(linked_list_ty *linked_list, void * const value)
{
    linked_list_ty new_item = create_list(value);
        
    
    if (*linked_list == NULL)
        *linked_list = new_item;
    
    else
    {
        (*linked_list)->last->next = new_item;
        new_item->previous = (*linked_list)->last;
        (*linked_list)->last = new_item;
    }
}

// ---------------------------------------------------------------------------

void print_list(linked_list_ty const linked_list)
{
    linked_list_ty tmp_list = linked_list;
    while (tmp_list != NULL)
    {
        printf("%d\n", *(int *)(tmp_list->value));
        tmp_list = tmp_list->next;
    }
}

// ---------------------------------------------------------------------------

void print_reversed_list(linked_list_ty const linked_list)
{
    linked_list_ty tmp_list = linked_list->last;
    while (tmp_list != NULL)
    {
        printf("%d\n", *(int *)(tmp_list->value));
        tmp_list = tmp_list->previous;
    }
}

// ---------------------------------------------------------------------------

void free_linked_list(linked_list_ty linked_list)
{
    linked_list_ty next_nodes = NULL;
    
    while (linked_list != NULL)
    {
        next_nodes = linked_list->next;
        free(linked_list);
        linked_list = next_nodes;
    }
}

// ---------------------------------------------------------------------------

int main(int arrgc, char *argv[])
{
    linked_list_ty my_linked_list = NULL;
    
    int  v1 =  1,
         v2 =  2,
         v3 =  3,
         v4 =  4,
         v5 =  5,
         v6 =  6,
         v7 =  7,
         v8 =  8,
         v9 =  9,
        v10 = 10;
        
    append(&my_linked_list, &v1);
    append(&my_linked_list, &v2);
    append(&my_linked_list, &v3);
    append(&my_linked_list, &v4);
    append(&my_linked_list, &v5);
    append(&my_linked_list, &v6);
    append(&my_linked_list, &v7);
    append(&my_linked_list, &v8);
    append(&my_linked_list, &v9);
    append(&my_linked_list, &v10);
    
    
//     print_list(my_linked_list);
    print_reversed_list(my_linked_list);
    
    free_linked_list(my_linked_list);
    
    return EXIT_SUCCESS;
}
Thanks in advance,