In C language, we can also create a generic linked list using the void pointer. Generic linked list means that it can store any data type as per the requirements.




Most important thing about the void pointer, it can store the address of any data type. Means that programmer can store the address of any data type as per there the user requirements.




In below example, I am creating a Node which contains the void pointer to store the address of any data type and Node pointer to create a link with another node.

Generic Node in C

Using the void pointer, we can create a generic Node. In below source code I am creating a structure which contains the void pointer and structure pointer.




Function to add node at the end of the Generic Linked list

It creates an extra node and adds this node in the last of the generic linked list.




Free the all allocated memory

We know that when we allocate memory in heap then this memory alive till the life of the program. So after use of this memory, we have to free all the allocated memory either we will get memory leak issues.




Driver program to create a generic linked list

In below program user can create a linked-list as per their requirements. If the user selects 1 then program create a list of a character and if select 2 then create a list of an integer and if user select 3 then create a list of the float.

 

OutPut:

When user Enter: 1

When user Enter: 2

When user Enter: 3

When user Enter: 4