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
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Rotate a Linked List
- C++ Program to implement Symbol Table
- Iterative Preorder Traversal of an N-ary Tree
- Flatten a binary tree into linked list | Set-2
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Print the DFS traversal step-wise (Backtracking also)