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.
Following is a sample C code to demonstrate working of 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
This article is contributed by Himanshu Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Given a linked list which is sorted, how will you insert in sorted way
- Function to check if a singly linked list is palindrome
- Merge two sorted linked lists
- Circular Linked List | Set 1 (Introduction and Applications)
- Rotate a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- restrict keyword in C
- Swap nodes in a linked list without swapping data
- Enumeration (or enum) in C
- Memory efficient doubly linked list
- void pointer in C
- Reverse a linked list
- Linked List | Set 1 (Introduction)
- Detect loop in a linked list
- Add two numbers represented by linked lists | Set 1