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 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.
// C program for generic linked list #include<stdio.h> #include<stdlib.h> /* A linked list node */struct Node { // Any data type can be stored in this node void *data; struct Node *next; }; /* Function to add a node at the beginning of Linked List. This function expects a pointer to the data to be added and size of the data type */void push(struct Node** head_ref, void *new_data, size_t data_size) { // Allocate memory for node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = malloc(data_size); new_node->next = (*head_ref); // Copy contents of new_data to newly allocated memory. // Assumption: char takes 1 byte. int i; for (i=0; i<data_size; i++) *(char *)(new_node->data + i) = *(char *)(new_data + i); // Change head pointer as new node is added at the beginning (*head_ref) = new_node; } /* Function to print nodes in a given linked list. fpitr is used to access the function to be used for printing current node data. Note that different data types need different specifier in printf() */void printList(struct Node *node, void (*fptr)(void *)) { while (node != NULL) { (*fptr)(node->data); node = node->next; } } // Function to print an integer void printInt(void *n) { printf(" %d", *(int *)n); } // Function to print a float void printFloat(void *f) { printf(" %f", *(float *)f); } /* Driver program to test above function */int main() { struct Node *start = NULL; // Create and print an int linked list unsigned int_size = sizeof(int); int arr[] = {10, 20, 30, 40, 50}, i; for (i=4; i>=0; i--) push(&start;, &arr;[i], int_size); printf("Created integer linked list is \n"); printList(start, printInt); // Create and print a float linked list unsigned float_size = sizeof(float); start = NULL; float arr2[] = {10.1, 20.2, 30.3, 40.4, 50.5}; for (i=4; i>=0; i--) push(&start;, &arr2;[i], float_size); printf("\n\nCreated float linked list is \n"); printList(start, printFloat); return 0; } |
chevron_right
filter_none
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
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.
Recommended Posts:
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Length of longest palindrome list in a linked list using O(1) extra space
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Generic Implementation of QuickSort Algorithm in C
- Sort a linked list of 0s, 1s and 2s



