I want to create a generic linked list in c, Below is the way I created it, but I am not sure if that is the correct way to do it, I allocate a new memory in the heap for the struct uniqueOrderedList_t, and then allocate a new memory in the heap as well for the element as I want it to be generic, is that the right way to do it? and what about the "next pointer", do I need to allocate memory for it as well?
#the .h file contain:
typedef void* Element;
typedef struct uniqueOrderedList_t* UniqueOrderedList;
UniqueOrderedList uniqueOrderedListCreate(/*some parameters*/);
#the .c file:
struct uniqueOrderedList_t{
Element element;
struct uniqueOrderedList_t* next;
};
uniqueOrderedList uniqueOrderedListCreate(/*some arguments*/){
UniqueOrderedList newList = malloc(sizeof(*newList));
if(!newList){
return NULL;
}
newLust->element = malloc(sizeof(Element));
if(!element){
return NULL;
}
newList->next = NULL;
}