Link copied to clipboard. Share away!

Dismiss

Published on 06 Mar 2020

Insertion in doubly linked list can happen at various places in the list. A few cases are:

- At the beginning of the doubly linked list.
- At the end of the doubly linked list.
- At a given position in the doubly linked list.

- Let us assume a newNode as shown above. The newNode with data = 25 has to be inserted at the beginning of the list.
- The
**next**pointer of the**newNode**is referenced to the head node and its**previous**pointer is referenced to**NULL**. - The
**previous**pointer of the head node is referenced to the**newNode**. - The
**newNode**is then made as the head node.

**C program to insert insert a node at the beginning of a doubly linked list**

C

Output

Enter the total number of nodes: 4

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of first node: 25

THE DOUBLY LINKED LIST IS:

25 5 10 15 20

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of first node: 25

THE DOUBLY LINKED LIST IS:

25 5 10 15 20

- Now, let us assume a newNode as shown above. The
**newNode**with data = 25 has to be inserted at the end of the linked list. - Make the
**next**pointer of the**last**node to point to the**newNode**. - The
**next**pointer of the**newNode**is referenced to NULL and its**prev**pointer is made to point to the last node. - Then, the
**newNode**is made as the**last node**.

**C program to insert a node at the end of a doubly linked list**

****

C

Output

Enter the total number of nodes: 4

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of first node: 25

THE DOUBLY LINKED LIST IS:

5 10 15 20 25

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of first node: 25

THE DOUBLY LINKED LIST IS:

5 10 15 20 25

- Now let us assume that a
**newNode**with data = 25 has to be inserted at position 2 in the linked list. - Start traversing the list from head and move upto the
**position - 1**. In this case, since we want to insert at second position, you need to traverse till (2 - 1) = 1 st node. - Once you reach the position - 1 node, the
**next**pointer of the newNode is set to the address contained by the**next**pointer of the (position - 1) th node. - Make the
**next**pointer of the (position - 1) th node to point to the newNode and the prev pointer of the newNode is pointed to the (position - 1) th node - Finally the newNode next 's
**prev**pointer must be made to point to the newNode.

C

Output

Enter the total number of nodes: 4

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of Last node: 50

Enter the postion: 2

THE DOUBLY LINKED LIST IS:

5 10 50 15 20

Enter the data of node 1: 5

Enter the data of node 2: 10

Enter the data of node 3: 15

Enter the data of node 4: 20

THE DOUBLY LINKED LIST IS:

5 10 15 20

Enter data of Last node: 50

Enter the postion: 2

THE DOUBLY LINKED LIST IS:

5 10 50 15 20

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×