Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Unlike C++ and Java, C does not support generics. How do I create a linked list in C that can be used with any data type? In C, we can use void Pointers and function Pointers to achieve the same functionality. The great thing about the void pointer is that it can be used to point to any data type. Also, all types of Pointers are always the same size, so we can always assign a linked list node. A function pointer is needed to handle the actual content stored at the address pointed to by a void pointer.

Here is a sample C code to demonstrate the work of a generic linked list.

// General list C program
#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;
};

/* Add node functions at the beginning of the list. This function requires a pointer to the data to be added and the size of the data type */
void push(struct Node** head_ref, void *new_data, size_t data_size)
{
	// Allocate memory for the node
	struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

	new_node->data = malloc(data_size);
	new_node->next = (*head_ref);

	// Copy the contents of new_data into the newly allocated memory. Assume: char occupies 1 byte.
	int i;
	for (i=0; i<data_size; i++)
		*(char *)(new_node->data + i) = *(char *)(new_data + i);

	// Change the header pointer when you start adding new nodes
	(*head_ref) = new_node;
}

/* A function that prints nodes in a given list. Fpitr is used to access the function used to print data for the current node. Note that different data types require different specifiers */ in printf()
void printList(struct Node *node, void (*fptr)(void *))
{
	while(node ! =NULL) { (*fptr)(node->data); node = node->next; }}// A function that prints integers
void printInt(void *n)
{
printf(" %d", * (int *)n);
}

// A function that prints floating-point numbers
void printFloat(void *f)
{
printf(" %f", * (float *)f);
}

/* Test the driver for the above function */
int main(a)
{
	struct Node *start = NULL;

	// Create and print an int 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 floating 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;
}
Copy the code

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
Copy the code

🥇 past quality articles

Singly linked list data structure of the chain table from the introduction of | first set of singly linked list data structure linked list and array | second singly linked list data structure of chain table insert | | delete nodes of the third set of singly linked list data structure of the fourth set of singly linked list data structure to delete a given location linked list node | the fifth set Singly linked list data structure of the check method of array and list | singly linked list data structure of the set of 6-1 check method of array and list | set 6-2 singly linked list data structure of the length of the chain table lookup (iteration and recursion) | 7 sets of singly linked list data structure of switching nodes in the list without exchanging data | 8 sets Singly linked list data structure of the inversion list | 9 sets of singly linked list data structure of merging two sorted lists | 10 sets of singly linked list data structure of the list of merge sort | 11 sets of singly linked list data structure with the size of a given set of inversion list | detection of 12 sets of singly linked list data structure and delete cycle | 13 set in the list Singly linked list data structure will represent the list of the two Numbers together | 14 sets of singly linked list data structure of spin chain table | 15 sets

📣 Endnote: For more information on data structures, you can follow me: Haiyong, I hope you found this article helpful.

If you see this, thank you for reading 🙂

💌 welcomes your comments and suggestions in the comments section! 💌

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.