Generic Linked List in C
Unlike C++ and Java, C doesn’t support generics. How to create a linked list in C that can be used for any data type? In C, we can use a void pointer and a function pointer to implement the same functionality. The great thing about void pointer is it can be used to point to any data type. Also, the size of all types of pointers is always is same, so we can always allocate a linked list node. Function pointer is needed to process actual content stored at the address pointed by the void pointer.
Following is a sample C code to demonstrate the working of a generic linked list.
Created integer linked list is 10 20 30 40 50 Created float linked list is 10.100000 20.200001 30.299999 40.400002 50.500000