Back to: Data Structures and Algorithms Tutorials

**Inserting a new node in a Linked List in C Language:**

In this article, I am going to discuss Inserting a new node in a linked list using C Language with Examples. Please read our previous article, where we discussed How to **Improve Searching in Linked List **using C Language with Examples.

**How to Insert a new node in a linked list:**

Here, we will see how to insert a new node in an existing linked list.

In the above Linked List, we want to insert a new node. For insertion, we have to mention at what position we want to insert or at what index we want to insert. Let us give indices to the nodes of the above linked list.

So we have given indices. Usually in array, we start index from â€˜0â€™ onwards so that is given by the compiler itself. But here is we can the indices from â€˜1â€™ also or if you want you can change it to â€˜0â€™ onwards. Now let us see what are the places that are available where we can insert a new node.

Here green arrow represents the places where we can insert a new node.

- We can insert a new node before the first node.
- We can insert in between two nodes.
- We can insert a new node after the last node.

Let us give indices for these positions.

So let us say the position before first node as â€˜0â€™ and rest of the positions are â€˜0â€™ onwards. If we say we want to insert at index â€˜1â€™ then it means we want to insert after the first node. If we say at index â€˜3â€™ means after the third node. Here we have to cases:

**Insert before first.****Inserting after given position.**

Let us look at the first case inserting before the first.

**Insertion before first in a Linked List:**

Now if we are inserting a new node before first node than we have to move â€˜firstâ€™ point also because that new node becomes first node otherwise inserting after any node there is no change in â€˜firstâ€™, simply we can insert a new node at any given index.

First step is to create a new node help of a pointer. Let us say it as â€˜tâ€™. Then after creating a new node initialized that node whatever the data, you want to insert. So, let’s say we want to insert â€˜4â€™ Next, we have to modify â€˜t->nextâ€™ to point where the â€˜firstâ€™ pointer is pointing. Then next step is to modify pointer â€˜firstâ€™ to point on this node â€˜tâ€™ because it’s getting inserted before the first node. So â€˜firstâ€™ should point on this.

So, there are 4 steps involved:

- Create a node:
**Node *t = new Node;** - Insert the data:
**t->data = x;** - Make tâ€™s next to point on first:
**t->next = first;** - Make the node as first node:
**first = t;**

These are the constant steps so time complex to inserting before first node is **O (1)**. It will take constant time. Now next let us learn how to insert a new node at a given position.

**Inserting after given position in a Linked List:**

Let’s say we want to insert at position â€˜2â€™. First, we have to create a new node, so we will take a temporary pointer and create a new node, and initialized it with some data whatever you want to insert.

Next, we have to make â€˜t->nextâ€™ to point on the next node and make previous nodes next to the point on the tâ€™s node. In this case, tâ€™s next will point on the â€˜7â€™ node and â€˜3â€™ next will point on tâ€™s node. So, these are the two changes we have to make.

So, for modifying â€˜7â€™ we should know the address of that node and to modify â€˜2â€™ next we should have some pointer for accessing this node then only we can access that one or modify that one. For having on previous node, we should bring a pointer from the first node onwards, we cannot directly get a pointer on the previous node.

So, we will create a pointer let say â€˜pâ€™ and bring this to that node after which we want to insert a new node. In this case we want â€˜pâ€™ at â€˜3â€™.

For inserting a new node let say at index â€˜2â€™ then we have to move â€˜pâ€™ for only 1 time and let we want to insert after index â€˜3â€™ then â€˜pâ€™ will move 2 times from the beginning of the linked list. So â€˜pâ€™ will move position â€“ 1 time.

The next step is we want the address of the next node to modify the links. Let say here we have inserted a new node after index â€˜2â€™ and â€˜pâ€™ is pointing the index â€˜1â€™ then we want the address of next node which is â€˜7â€™ node. So, we will get that by â€˜p->nextâ€™. Now the last step is â€˜p->nextâ€™ have to point on â€˜tâ€™.

**Pseudo Code for the above steps:**

**Node *t = new Node;**

**t->data = x;**

**p = first;**

**for (i = 0; i < pos – 1; i++)**

**{**

**p = p->next;**

**}**

**t->next = p->next;**

**p->next = t;**

The same procedure or same steps will work on insertion after the last node. Now let us write a function to insert at any position in the linked list.

**Function to insert a node at any position in a linked list:**

void Insert(int pos, int x) { Node *t, *p; if (pos == 0) { t = new Node; t->data = x; t->next = first; first = t; } else if (pos > 0) { p = first; for (int i = 0; i < pos - 1 && p; i++) p = p->next; if (p) { t = new Node; t->data = x; t->next = p->next; p->next = t; } } }

Now let us write a full program for insertion a new node in a linked list using C Language.

**Program for insertion of a new node in a linked list using C Language:**

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; } *first = NULL; void Create(int A[], int n) { int i; struct Node *t, *last; first = (struct Node *) malloc(sizeof(struct Node)); first->data = A[0]; first->next = NULL; last = first; for (i = 1; i < n; i++) { t = (struct Node *) malloc(sizeof (struct Node)); t->data = A[i]; t->next = NULL; last->next = t; last = t; } } int Count(struct Node *p) { int l = 0; while (p) { l++; p = p->next; } return l; } void Display(struct Node *p) { while (p != NULL) { printf ("%d ", p->data); p = p->next; } } void Insert(struct Node *p, int index, int x) { struct Node *t; int i; if (index < 0 || index > Count(p)) return; t = (struct Node *) malloc (sizeof (struct Node)); t->data = x; if (index == 0) { t->next = first; first = t; } else { for (i = 0; i < index - 1; i++) p = p->next; t->next = p->next; p->next = t; } } int main() { int A[] = { 8, 3, 7, 12 }; Create (A, 4); Insert (first, 2, 2); Display (first); return 0; }

**Output:**

In this article, I am going to discuss **How to Create a Linked list using Insert** using C Language with Examples. Here, in this article, I try to explain How to Insert a new node in a linked list using C Language with Examples and I hope you enjoy this Inserting a new node in a linked list using C Language with Examples article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.