Implementation
According to the definition of a generic linked list, it is able to store any kind of data for the same definition of a node. But in the C language, we don’t have such data type which can store any type of data whereas C++ uses a template class for such generic implementation.
We will use the concept of void pointer for defining a generic node as a void pointer can store the address of any data type.
So, the structure of the generic node will be as shown below
struct node{
void* data;
struct node* next;
}
You can also try this code with Online C Compiler
Run Code
As data member’s type is not predefined, for printing a generic linked list we need to have different functions for different types.
Now we will implement a function insert_end( ) to insert a new node in the generic linked list and then print_list() to print the contents of the generic linked list.
insert_end( )
To insert a value in the generic linked list, we need to pass the size of the data as well as the address of the data as a void pointer as the type of the data member is not predefined.
Steps
- allocate memory for the new node
- allocating memory for the data
- storing the data of a new node by copying from one address to another
- inserting the new node after the end node
- updating end node pointer
node* insert_end(node** end,void* data,int size)
{
//allocate memeory for new node
node* new_node=(node*)malloc(sizeof(node));
// allocating memory for the data
new_node->data=malloc(size);
//storing the data of new node by copying from one address to another
memcpy(new_node->data,data,size);
//insering the new node after the end node
(*end)->next=new_node;
// updating end node pointer
*end=new_node;
}
You can also try this code with Online C Compiler
Run Code
print_list()
To print the contents of the generic linked list, we need the head pointer of the linked list and function pointer for printing specific types of data.
void print(node* head,void* fun(void*))
{
while(head!=NULL)
{
//function for printing specific type of data
*fun(head->data);
head=head->next;
}
}
You can also try this code with Online C Compiler
Run Code
Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
void* data;
struct node* next;
}node;
void insert_end(node** end,void* data,int size)
{
//allocate memeory for new node
node* new_node=(node*)malloc(sizeof(node));
new_node->next=NULL;
// allocating memory for the data
new_node->data=malloc(size);
//storing the data of new node by copying from one address to another
memcpy(new_node->data,data,size);
(*end)->next=new_node;
*end=new_node;
}
void print(node* head,void (*fun)(void*))
{
while(head!=NULL) {
//function for printing specific type of data
(*fun)(head->data);
head=head->next;
}
}
void print_int(void* data)
{
printf("%d ",*(int*)data);
}
int main()
{
int elements[]={1,2,3,4,5};
node* head=(node*)malloc(sizeof(node));
node* end=head;
head->data = malloc(sizeof(int));
memcpy(head->data,elements,sizeof(int));
head->next=NULL;
int i;
for( i=1;i<=4;i++)
{
insert_end(&end,&elements[i],sizeof(int));
}
print(head,print_int);
}
You can also try this code with Online C Compiler
Run Code
Output
1 2 3 4 5
You can also try this code with Online C Compiler
Run Code
Frequently Asked Questions
What is used in C++ for a generic definition?
There is a class called template which can be used to define generic structure.
What is a function pointer?
It is a pointer to a function that stores the address of the function.
What is memcpy() function in C?
It is a library function for copying a block of memory from one location to another.
Conclusion
This article covered how to implement Generic Linked List in C. A good grasp of Linked Lists can be a huge plus point in a coding interview.
Recommended Reading:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.
To study more about Linked Lists, refer to Applications Of Linked Lists.
Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.
Happy learning!