# Operations on Linked Lists in C/C++

**Introduction**

A linked list is a type of linear data structure that uses nodes to store the data.

Each node in a linked list is a structure-defined data type that consists of data and a pointer referencing the address of the next node.

Source: studytonight

**Advantages - **

- Linked Lists are dynamic in nature, which means they can allocate memory when required.

- Insertion and deletion in Linked Lists can be on both sides, i.e., either from the head part or from the tail part.

- Linked Lists reduce access time.

**Applications -**

- Linked Lists can be used to implement queues, stacks and graphs.

- Forward and backward button in the toolbar for navigation uses doubly linked lists.

Recommended Topic, __Floyds Algorithm__

**Types of Linked Lists**

There are three types of linked lists which have their own unique ways of implementation. They are namely:

- Singly Linked Lists.

- Doubly Linked Lists.

- Circular Linked Lists.

**Singly Linked Lists - **Singly Linked List is the collection of nodes which consist of data and the pointer part which will be pointing to the address of the next node.

The structure of the node of Singly Linked List be like:

```
Struct Node
{
int data;
Node* next;
};
```

The nodes are connected with the help of the next pointer which is pointing towards the address of the next node. If the next node is not present or the current node is the last node of the Linked List then the next pointer will be pointing to NULL.

**Doubly Linked Lists - **Doubly Linked List is the collection of nodes which consists of data and two pointers one of which will be pointing to the previous node’s address and another will be pointing to the next node’s address.

The structure of the node of Singly Linked List be like:

```
Struct Node
{
int data;
Node* next;
Node* prev;
};
```

The nodes are connected with the help of the next pointer which is pointing towards the address of the next node and the prev pointer which is pointing towards the address of the previous node. If the next node is not present or the current node is the last node of the Linked List then the next pointer will be pointing to NULL. And in the other case if the previous node is not present or the current node is the first node of the linked list then the prev pointer will be pointing to NULL.

**Circular Linked Lists - **Circular Linked List is the collection of nodes which consists of data and a pointer which will be pointing to the next node’s address.

The Last node’s next pointer will be pointing to the first node of the Circular Linked List.

Source: AlphaCodingSkills

Now let us jump to the basic operations performed on a linked list.

You can also read __Difference Between Array List and Linked List__ here.

**Operations on Linked Lists in C/C++**

** **There are several operations which were performed on the Linked Lists

- Traversal - To traverse throughout the linked list.

- Insertion - Insertion of a node at any position.

- Deletion - Deletion of a node from any position.

- Updation - Updation of data of a node.

We will cover each one of these operations on linked lists in C/C++ one by one in detail. So, fasten your seat belts.

**1. Traversal -**

Traversal is one of the basic operations on linked lists in C/C++ in which we traverse throughout the linked list from beginning to the end or from head to tail.

Traversal can be used to print the nodes of the linked list, to reach out for a required node in a list.

Let us take the example of printing the nodes of the linked list through traversal operation.

**Algorithm :**

- First, initialize a Node variable, say ‘temp’ pointing to the head of the linked list.

- Iterate till the ‘temp’ will not become NULL.

- Print the node’s data.

- Increment ‘temp’ to temp’s next.

```
void traversal(Node* head) {
Node* temp = head;
while(temp != NULL)
{
cout<<(temp->data);
temp = temp->next;
}
}
```

**2. Insertion - **Of the operations on linked lists in C/C++, insertion is used to insert a node at a particular position. The positions can be:

- At the beginning of the linked list.

- At the end of the linked list.

- After after a particular node.

**Insertion at the Beginning **- In this case we need not to traverse the linked list. We will check if the list is empty then we will make the new node as head. Otherwise, we have to connect the head of the list with the new node by making the next pointer of the node pointing to the head and then making the new node as head of the list.

Source : codeforwin

```
Node* insertAtBegin(Node* head, int x)
{
// creation of a new node of linked list.
Node* newNode = new Node(x)
// checking if the linked list is empty.
if(head == NULL)
return newNode;
// insertion of the node at the beginning.
else
{
newNode->next = head;
return newNode;
}
}
```

**Insertion at the end - **In this case we have to traverse throughout the linked list until we will find the last node and then we will insert the required node in the list.

For this, we have to consider some special cases. For eg. if the list is empty then we will just create the new node and make it as head. In this case the new node will be the first and the last node of the list.

Source: AfterAcademy

```
Node* insertAtEnd(Node* head, int x)
{
// If the list is empty.
if( head == NULL )
{
Node* newNode = new Node(x);
head = newNode;
return head;
}
Node* temp = head;
// Traversing the list till the last node
while(temp->next != NULL)
{
temp = temp->next;
}
Node* newNode = new Node(x);
temp->next = newNode;
return head;
}
```

**Insertion after a particular node -**

In this case a reference to a particular node will be given. Then a new node will be inserted after that.

If the reference of the node is not given instead data of the node is given, then we have to traverse to the given node matching the given data and then we will insert the new node after that node.

Source: AfterAcademy

```
void insertAfterNode(Node* givenNode, int x)
{
Node* newNode = new Node(x);
newNode->next = givenNode->next;
givenNode->next = newNode;
}
```

**3. Deletion - **This is one type of the operations on linked lists in C/C++, in which we just need to follow the following steps:

- Traversing to the previous node of the node to be deleted.

- Changing the next pointer of that previous node to point to the address of the next node of the node to be deleted.

- Freeing up the memory which is occupied by the node to be deleted.

Source : AfterAcademy

```
Node deleteNode(Node* head, Node* toBeDeleted)
{
// If the node to be deleted is the head node.
if(head == toBeDeleted)
{
return head.next;
}
// temp is the traversing node.
Node* temp = head;
while( temp->next != NULL )
{
// Searching for the previous node of the node to be deleted.
if(temp->next == toBeDeleted)
{
temp->next = temp->next->next;
// Freeing up the memory of the node to be deleted.
return head;
}
temp = temp->next;
}
// If no node matches in the Linked List.
return head;
}
```

**4. Updation - **This kind is one of the operations on linked lists in C/C++, in which we have to replace the data part of the required node with the given value.

For this, we will be traversing throughout the linked list until we find a node that will be matching to the required node to be updated.

```
void updateNode(Node* head, int value, int newValue)
{
// temp is the traversing node
Node* temp = head;
while(temp != NULL)
{
// If the node matches the given node to be updated.
if( temp->data == val)
{
// Updating the data part of the node with the new value.
temp->data = newVal;
return;
}
temp = temp->next;
}
}
```

## Frequently Asked Questions

**What is a linked list?**

Linked List is a type of linear data structure which stores the values at non - contiguous memory locations unlike arrays. Linked Lists are made up of nodes which consist of a data field and a pointer field to reference the address of the next node in the linked list.

**How to create a node of a linked list?**

Linked Lists are made up of nodes which consist of a data field and a pointer field to reference the address of the next node in the linked list.

So, to achieve this we need to create a node with the help of a structured data type which will have a data part and a pointer.

**Struct Node****{**** int data;**** Node* next;****};**

**How Linked Lists differ from Arrays?**

Linked Lists and Arrays both are linear data structures but there are some differences between them due to which both have some advantages and disadvantages over each other.**Arrays**

1. Data is stored in contiguous locations of memory

2. Due to fixed and continuous memory locations, the deletion and insertion operations are much costlier as we have to shift all the elements with respect to the operated element.

3. Size of the array must be specified at the time of array declaration.

4. Array elements can be accessed randomly with the help of index.**Linked Lists**

1. Data is stored in non contiguous locations of memory.

2. Operations like deletion and insertion are easier in linked lists as compared to arrays.

3. Size of the linked lists can be changed by insertion or deletion operations.

4. We cannot access the element of the linked lists randomly rather we have to traverse to that element for access.

## Conclusion

This article covered the types of linked lists and various operations on linked lists in C/C++. If you want to learn more about linked lists and want to practice some questions which require you to take your basic knowledge on operations on linked lists in C/C++, a notch higher, then you can visit our __Guided Path for Linked List__.

Recommended Reading:

__Dynamic Binding in C++____Advantages and Disadvantages of Linked List____Linked List in Java____Dynamic Memory Allocation in C____Binary search on Linked List____Reversing a Linked List____Non-alphanumeric Characters__

To study more about Linked Lists, refer to __Applications Of Linked Lists__.

Until then, All the best for your future endeavors, and Keep Coding.