The linked List Data structure is a collection of an element where elements are not stored in contiguous memory locations.

Page Contents

A linked list Data structure is a linear type of data structure. The linked list used to store a collection of elements which are connected to each other via the pointer.

A linked list consists of nodes where each node is consist of the data field and address field.

What is Node?

A Node is an element which has data along with the address stored in it.
Node: – Node is a combination of data and the next (address of next node).

The linked list stores the set of elements where each element itself treated as a separate object. Unlike arrays liked list are not stored in the contiguous memory location. The elements in the linked list are linked using the pointer.

A linked list is a collection of a node having the following format.

### Representation of Linked List in Data structure

The linked list is a collection of a node connected by pointers. The first node in the linked list is referred to as a Head Node. If the Linked list is empty then Head Node points to Null.
The node consists of two parts
1. Data: the value of the node
2. Address: Reference to the next node

Linked list node contains two fields of a different type in a node. Therefore to implement a node we use a type of structure to form a node.
Struct node
{
int data;
Struct node * next;
}
In the linked list structure, data stores int type data in a node and the next pointer point’s next node of the same type structure.

Now we will see how exactly the linked list be implemented in C.

### Linked list program in c

In this we simply create a linked list with 3 nodes P1, P2, P3 As given below. ```#include<stdio.h>
Struct node
{
int data;
Struct node *next;
}
void main()
{
Struct node *p1,*p2,*p3;
Struct node *start,*temp;
p1=(Struct node*)malloc(sizeof(struct node));
p2=(Struct node*)malloc(sizeof(struct node));
p3=(Struct node*)malloc(sizeof(struct node));
p1->next=p2;
p2->next=p3;
p3->next=NULL;

start=p1;
temp=start;
temp->data=10;
temp=temp->next;
temp->data=20;
temp=temp->next;
temp->data=30;
temp=temp->next;
temp=start;
printf("%d",temp->data);
temp=temp->next;
printf("%d",temp->data);
temp=temp->next;
printf("%d",temp->data);
temp=temp->next;
}```

In an above Linked list, We simply create nodes in c and for creating and allocating an address to the node we used malloc() which allocates memory dynamically and after that, we linked the nodes.

Example:
p1 –> next = p2, p1 stores address of p2 which forms a link.
In this way, we formed a linked list and stored data in it with the address.

### Operations on the linked list

1. Insert Node at the beginning

This operation state that to insert the node at beginning of a linked list.
Example: Insert 10 at the beginning. Insert 7 at the beginning.
Before adding a node to beginning it starts node but after adding at beginning new node become start.

2. Insert Node at end

This operation is to add a new node at last of LL.
Add 10 at end of LL.

3. Insert Node before any Node

This operation is to add a new node before any previously given node. After adding a new node we have to modify the address of the previous node and add that to the new node added.

4. Deletion of node
This operation is to delete modes of the LL.

5. Destroy
To delete complete LL.

### Why Linked List data structure?

The array data structure is a linear data structure which is used to store the homogeneous collection of elements. But array having some drawbacks
Array Drawbacks
1. Array size is fixed: After creating array we cannot change the size of the array.
2. Inserting and deleting a random element from the array is difficult: we cannot insert or delete any middle element of the array because to do this need to modify the whole array.

So, the Linked list is used. Unlike array the Linked list is dynamic