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 void pointer and 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, size of all types of pointers is always is same, so we can always allocate a linked list node. Function pointer is needed process actual content stored at address pointed by void pointer.

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20generic%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20%2F%2F%20Any%20data%20type%20can%20be%20stored%20in%20this%20node%0A%20%20%20%20void%20%20*data%3B%0A%20%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20add%20a%20node%20at%20the%20beginning%20of%20Linked%20List.%0A%20%20%20This%20function%20expects%20a%20pointer%20to%20the%20data%20to%20be%20added%0A%20%20%20and%20size%20of%20the%20data%20type%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20void%20*new_data%2C%20size_t%20data_size)%0A%7B%0A%20%20%20%20%2F%2F%20Allocate%20memory%20for%20node%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20new_node-%3Edata%20%20%3D%20malloc(data_size)%3B%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F%2F%20Copy%20contents%20of%20new_data%20to%20newly%20allocated%20memory.%0A%20%20%20%20%2F%2F%20Assumption%3A%20char%20takes%201%20byte.%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cdata_size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20*(char%20*)(new_node-%3Edata%20%2B%20i)%20%3D%20*(char%20*)(new_data%20%2B%20i)%3B%0A%20%0A%20%20%20%20%2F%2F%20Change%20head%20pointer%20as%20new%20node%20is%20added%20at%20the%20beginning%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list.%20fpitr%20is%20used%0A%20%20%20to%20access%20the%20function%20to%20be%20used%20for%20printing%20current%20node%20data.%0A%20%20%20Note%20that%20different%20data%20types%20need%20different%20specifier%20in%20printf()%20*%2F%0Avoid%20printList(struct%20node%20*node%2C%20void%20(*fptr)(void%20*))%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20(*fptr)(node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Function%20to%20print%20an%20integer%0Avoid%20printInt(void%20*n)%0A%7B%0A%20%20%20printf(%22%20%25d%22%2C%20*(int%20*)n)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20print%20a%20float%0Avoid%20printFloat(void%20*f)%0A%7B%0A%20%20%20printf(%22%20%25f%22%2C%20*(float%20*)f)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20and%20print%20an%20int%20linked%20list%0A%20%20%20%20unsigned%20int_size%20%3D%20sizeof(int)%3B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B10%2C%2020%2C%2030%2C%2040%2C%2050%7D%2C%20i%3B%0A%20%20%20%20for%20(i%3D4%3B%20i%3E%3D0%3B%20i–)%0A%20%20%20%20%20%20%20push(%26start%2C%20%26arr%5Bi%5D%2C%20int_size)%3B%0A%20%20%20%20printf(%22Created%20integer%20linked%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList(start%2C%20printInt)%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20and%20print%20a%20float%20linked%20list%0A%20%20%20%20unsigned%20float_size%20%3D%20sizeof(float)%3B%0A%20%20%20%20start%20%3D%20NULL%3B%0A%20%20%20%20float%20arr2%5B%5D%20%3D%20%7B10.1%2C%2020.2%2C%2030.3%2C%2040.4%2C%2050.5%7D%3B%0A%20%20%20%20for%20(i%3D4%3B%20i%3E%3D0%3B%20i–)%0A%20%20%20%20%20%20%20push(%26start%2C%20%26arr2%5Bi%5D%2C%20float_size)%3B%0A%20%20%20%20printf(%22%5Cn%5CnCreated%20float%20linked%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList(start%2C%20printFloat)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output:

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
[ad type=”banner”]