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

Linked List Data Structure

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.

Linked list Definition:

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

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

Linked List Data structure
Linked List 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.

Linked List

#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;
//Display Linked List
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.

Types of Linked List

Types of Linked List
Types of Linked List

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.

Linked List

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.

Linked ListLinked ListAfter adding a node at end null be replaced by the address of a new node.

3. Insert Node before any Node

This operation is to add a new node before any previously given node.

Linked List
Add 15 before 20 value in LL.

Linked List
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
Advantages of Linked list
1. Dynamic size: The size of the array is defined dynamic so we can change the size of the linked list.
2. Easy to insert and delete

Write A Comment