Linked List in C: How to Create, Types And Advantages
Did you know that linked lists are a linear data structure and have attracted almost 21% of developers because of their reliability, efficiency, and performance? One of the most reliable features is the linked list program in C. But before we get into the details and understand what a linked list can do, have you wondered what is a linked list? The answer to this question will solve as you move further in the blog. It will walk you through the descriptive details of what a linked list is, why is it used, its functionality different types of linked lists, and how to create a linked list in C in detail.
What is a Linked List in C?
The linked list is a linear data structure and is implemented in C using pointers and structs. They are connected through different nodes. The nodes in the linked list are connected in a sequence and series, one after the other, the last node however contains the NULL value which indicates the end of the list. The sequential nodes that are connected in a linked list in a series contain, data and pointers. These are used to store the value and the address of the next node, respectively. Below is an example that shows the linked list implementation in C:
struct Node {
int data;
struct Node* next;
};
int main() {
// create three nodes
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// Assign values to each node
head->data = A;
head->next = second;
second->data = B;
second->next = third;
third->data = C;
third->next = NULL;
// traverse the linked list and print each value
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
return 0;
}
In the above-mentioned example, struct node is used to define a node in a linked list. Inside this node data is defined and struct Node* next is used to point out to the next node.
Then inside our main function, we have created three nodes head, a second and a third assigning NULL value to each node which means the list is currently empty. Then we have allocated memory for these three nodes using the malloc() function. After allocating the memory we have assigned values to each node as A, B, and C respectively, and NULL value to the next node.
Now we need to traverse the list by setting a loop and initializing a pointer to the head node. The loop will stop when the pointer reaches the NULL value. Printf() function is used to print the value of the pointer inside the loop. Each time the pointer gets updated to the next node printf() prints the value of that node.
Also Read: Fibonacci Series Program In C
Why Use the Linked List?
There are a variety of reasons why linked list in C is used by many developers. The implementation that it follows provides a very dynamic data structure that makes many operations like insertion, deletion, shrinking, and growing easier in the run time.
The utilization of memory is done efficiently and no storage is wasted while working with the linked list because of the sequentially connected nodes that store data. It is very scalable and flexible as it holds the capability to remove elements in the linked list at any position. It is also effective for larger data and hence most developers prefer using linked ist. For a better understanding of the topic, you can refer to this C course available online.
How To Create a Linked List in C?
There are six fundamental steps to creating a linked list. The following points explain those steps in detail:
- The first step is to define the structure of the linked list, the defined structure will hold the data and pointer for the corresponding node in the list.
- In this step, the malloc function is used to create a new node.
- The next involves the creation of a head pointer that will point to the first node in the linked list.
- In the fourth step, you will add a node at the start of the list.
- This step will be used to add a node at the end of the list.
- This is the final step, which is used to print the content by using the traverse and print option in each node value.
Types of Linked Lists in C
There are mainly three types of linked lists in C, which are explained below in-depth:
1. Single Linked List
The most commonly used and simple linked list is where each node in the list contains the reference for the next node in sequence and has the NULL node at the end that represents the end of the list. The example for a single linked list is as follows:
struct Node {
int data;
struct node*next;
}
An example of a three-member singly linked list is mentioned below:
/* Initialize nodes */
struct Node *head;
struct Node *one = NULL;
struct Node *two = NULL;
struct Node *three = NULL;
/* Allocate memory */
one = (struct Node*)malloc(sizeof(struct node));
two = (struct Node*)malloc(sizeof(struct node));
three = (struct Node*)malloc(sizeof(struct node));
/* Assign data values */
one->data = A;
two->data = B;
three->data = C;
/* Connect nodes */
A->next = B;
B->next = C;
C->next = NULL;
/* Save the address of the first node in the head */
head = A;
In the above-mentioned example, struct node is used to define a node in a linked list. Inside this node data is defined and struct Node* next is used to point out to the next node. Then we have defined four pointers using struct Node* that are head, one, two, and three and we have assigned each of them with a NULL value.
Next, we have allocated memory to nodes one, two, and three using the malloc() function. After allocating the memory we have assigned values to each node as A, B, and C respectively.
Finally, we have connected these nodes using pointers where the next pointer to A stores the address of B similarly next pointer to B stores the address of C, and the next pointer to C stores the address of the node with the value NULL and we have saved the address of first node in the head pointer.
2. Double Linked List
This type of linked list program in C allows you to go in either direction, that is, forward or backward. The pointer is added to the previous node in a series of sequences and it allows for a more effective traverse of the nodes. The node of a doubly linked list is written as:
struct node {
int data;
struct node*next;
struct node*prev;
}
An example:
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = (struct Node*)malloc(sizeof(struct node));
two = (struct Node*)malloc(sizeof(struct node));
three = (struct Node*)malloc(sizeof(struct node));
/* Assign data values */
one->data = A;
two->data = B;
three->data = C;
/* Connect nodes */
A->next = B;
A->prev = NULL;
B->next = C;
B->prev = A;
C->next = NULL;
C->prev = B;
/* Save the address of the first node in the head */
head = A;
In the above-mentioned example, a struct node is used to define a node in a linked list. Inside this node data is defined and struct Node* next is used to point out to the next node. Then we have defined four pointers using struct Node* that are head, one, two, and three and we have assigned each of them with a NULL value.
Next, we have allocated memory to nodes one, two, and three using the malloc() function. After allocating the memory we have assigned values to each node as A, B, and C respectively.
Finally, we have connected these nodes using pointers where the next pointer to A stores the address of B, and the previous pointer to A stores the address of the node with a NULL value. Similarly, the next pointer to B stores the address of C, the previous pointer to B stores the address of A, and the next pointer to C stores the address of the node with a NULL value and a previous pointer to C stores the address of B. We have saved the address of the first node in the head pointer.
Also Read: Identifiers In C
3. Circular Linked List
In this linked list the last element is linked to the first forming a circular loop, hence getting the name circular linked list. They can be of both types, that is, single and double.
An example:
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = (struct Node*)malloc(sizeof(struct node));
two = (struct Node*)malloc(sizeof(struct node));
three = (struct Node*)malloc(sizeof(struct node));
/* Assign data values */
one->data = A;
two->data = B;
three->data = C;
/* Connect nodes */
A->next = B;
B->next = C;
C->next = A;
/* Save the address of the first node in the head */
head = A;
In the above-mentioned example, struct node is used to define a node in a linked list. Inside this node data is defined and struct Node* next is used to point out to the next node. Then we have defined four pointers using struct Node* that are head, one, two, and three and we have assigned each of them with a NULL value.
Next, we have allocated memory to nodes one, two, and three using the malloc() function. After allocating the memory we have assigned values to each node as A, B, and C respectively.
Finally, we have connected these nodes using pointers where the next pointer to A stores the address of B. Similarly, the next pointer to B stores the address of C, the next pointer to C stores the address of node A, and we have saved the address of the first node in the head pointer.
Advantages and Limitations of Linked List
The table below explains all the benefits and limitations of the linked list:
Benefits | Limitations |
---|---|
The linked list provides a dynamic data structure. | It uses memory to store the data. |
It is free of any memory wastage. | It does not allow for random access due to certain reasons. |
It is scalable to use. | It has complex implementations. |
It is flexible as it is not stored in a specific memory location. | It sometimes gets difficult to share the data with linked lists. |
It proves to be effective to be used for larger data amounts. | It is not suitable to be used for small amounts of data. |
Conclusion
In this blog, we learned about the linked list in C and all the features that it has to offer. It is an essential function C programming language that offers help to store the data in the sequential nodes that it works on. It offers scalability with the working process that it provides and is considered reliable for storing huge amounts of data.