Introduction To Linked List
The Linked List is a type of data structure like an array in C programming. The linked list in c was designed to overcome the demerits of the Array type Data structure. Because the ar
ray is a sequential element of a similar data type with the contiguous memory location. Only one memory block is allocated for the array to hold elements of the array. But in the case of a Linked list, it can be stored its every element in any memory address. The Linked list is a method used in different programming languages like C, C++, C#, JAVA, Python, etc.Why Linked List In C?
We can use the arrays also but it can only store linear data of similar types. Even though it has below disadvantages for which we use linked list instead of arrays. The size of the array is static or fixed: So we must know the maximum number of elements in advance.
As we know that the array is limited in size. we can not change size at run time as it is static in nature. when the memory is taken into consideration then the array is not efficient as compared to other data types. Let me clarify your confusion, suppose we declared an array of size 5, but we inserted only 3 elements to the array. But the remaining 2 blocks reserved in memory are left with no value.it simply wasted as it is not being useful anywhere in the program.
Another situation is if you want to insert more data into the array of size 5. it does not store in the array rather it will send the message saying the array overflowing or exceeding the limit of the array. these are the major problems that had been faced by the programmer till the linked list concept appeared in the programming language. Being dynamic in nature, the Linked List concept solved the above problem.
Advantage of Linked List In C
Dynamic In Nature: The linked list is dynamic in nature as it can add and shrink at run time. The memory is allocated and deallocated as per the operation. so it is not required to give the initial size of a linked list as we do in array initialization.
Reduce Memory Wastage: As we know that the Linked List increases and decreases at run time, so memory utilization can be achieved in a fully optimized way. In Linked List, we do not need to pre-allocate memory for it like we in array initialization. Memory is allocated as per the requirement dynamically.
Insertion and Deletion Operations: Doing any kind of operations like insertion and deletion in Linked List is quite convenient as compared to an array. we do not need to shift the elements while doing insertion and deletion operations like an array. only the field pointer of the node needs to be updated.
Structure Of Linked List In C
The linked list in c is a sequence of linear data structures called elements. Each element is called a node. The linked list is a collection of nodes or elements that are connected via links.
In Linked List, Header is the pointer variable that is used to store the address of the first node of the linked list. As per the concept , if we want to visit any particular node present in the list. we have to traverse(visit) righrt from first node till the destination node. If we loose the address of the first node then we will loose other nodes connected to it. Because the nodes are not stored in consecutive order in memory like array .
so when we do any kind of operration to the linked list, it is a better to keep a header pointer as reference to the first node of Linked list. so that next time if we want to use the list again, we can enter to the list through the header reference pointer.
In simple, I can say that each node consists of its own data and the address of the next node and forms a chain. But here the main advantage is that no matter each element is storing in which area of memory.
Data field: This is the real data assigned by the user with any data type as per the user like int, char, float, etc.
Address Field: It is a pointer that stores the address of another node.
Let us take an example of a linked list program in c or how to do the linked list implementation in c using a function.
struct node
{
int data;
struct node *next;
};
#include <stdio.h>
#include <stdlib.h>
struct node *createNode(int nodeValue);
struct node
{
int data;
struct node *next;
};
int main(void)
{
int data,LinkedListCount=0;
struct node *head=NULL,*NewNodeAddress,*CurrentNodeAddress;
printf("Hi PiEmbSysTech Plz insert value for Number of Nodes creation \n");
scanf("%d",&LinkedListCount);
/*Creation of Number of nodes as per the user entry*/
while(LinkedListCount!= 0)
{
printf("Hi PiEmbSysTech Plz Insert a Node.......");
scanf("%d",&data);
NewNodeAddress=createNode(data);
/*Linking of a new node to linked list*/
if(head==NULL)
{
head=NewNodeAddress;
CurrentNodeAddress=head;
}
else
{
CurrentNodeAddress->next=NewNodeAddress;
CurrentNodeAddress=NewNodeAddress;
}
LinkedListCount--;
}
printf("\n Original list.......");
displayNewlyCreatedLinkedList(head);
}
struct node *createNode(int nodeValue)
{
struct node *tempNode;
tempNode=(struct node *)malloc(sizeof(struct node));
tempNode->data=nodeValue;
tempNode->next=NULL;
return tempNode;
};
void displayNewlyCreatedLinkedList(struct node *LinkedListHeader)
{
while(LinkedListHeader!=NULL)
{
if(LinkedListHeader->next!=NULL)
printf("%x->%d-->",LinkedListHeader, LinkedListHeader->data);
else
printf("%x->%d",LinkedListHeader, LinkedListHeader->data);
LinkedListHeader=LinkedListHeader->next;
}
}
Each element or node is nothing but the memory only.
Now you can compile the above program to see the results and how it is working.
Types Of Linked List in C or C++
There 3 types of Linked List in C programming language. Each Linked List will have its own method and process of storing the data with different application features. So each Type will have different article to go through it.
- Singly Linked List.
- Doubly Linked List.
- Circular Linked List.
Similar Articles Related To Linked List
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.
Best example for Linked list.