6-4-Doubly Linked List Tutorial
6-4-Doubly Linked List Tutorial
A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages.
The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This
is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows
for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both
directions.
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)
Node Definition
Here is how a node in a Doubly Linked List is typically represented:
Try it on GfG
Practice
C++ C Java Python C# JavaScript
1
struct Node {
3
// To store the Value or data.
4
int data;
5
6
// Pointer to point the Previous Element
7
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in the list, and a pointer
to the previous node in the list. By linking these nodes together through the next and prev pointers, we can
traverse the list in both directions (forward and backward), which is a key feature of a Doubly Linked List.
a. Forward Traversal:
b. Backward Traversal:
2
using namespace std;
4
// Define the Node structure
5
struct Node {
6
int data;
7
Node* next;
8
Node* prev;
10
// Constructor to initialize Node with data
11
Node(int data) : data(data), next(nullptr),
12
prev(nullptr) {}
13
};
14
15
// Function to traverse the doubly linked list
16
// in forward direction
17
void forwardTraversal(Node* head) {
18
19
// Start traversal from the head of the list
20
Node* curr = head;
21
22
// Continue until current node is not null
23
// (end of list)
24
while (curr != nullptr) {
25
26
// Output data of the current node
27
cout << curr->data << " ";
28
29
// Move to the next node
30
curr = curr >next;
Output
Forward Traversal:
1 2 3
Backward Traversal:
3 2 1
1
#include <iostream>
2
3
using namespace std;
5
// Node structure for doubly linked list
6
struct Node {
7
int data;
8
Node * prev;
9
Node * next;
10
11
Node(int val) {
12
data = val;
13
prev = next = nullptr;
14
}
15
};
16
Output
To insert a new node at the beginning of the doubly list, we can use the following steps:
Create a new node, say new_node with the given data and set its previous pointer to null, new_node->prev
= NULL.
Set the next pointer of new_node to current head, new_node->next = head.
If the linked list is not empty, update the previous pointer of the current head to new_node, head->prev =
new_node.
Return new_node as the head of the updated linked list.
Below are the implementation of the above approach:
1
// C++ Program to insert a new node at the
2
// beginning of doubly linked list
4
#include <iostream>
5
using namespace std;
7
// Node structure for the doubly linked list
8
struct Node {
9
int data;
10
Node* prev;
11
Node* next;
12
13
Node(int d) {
14
data = d;
15
prev = next = NULL;
16
}
17
};
18
19
// Insert a node at the beginning
20
Node* insertBegin(Node* head, int data) {
21
22
// Create a new node
23
Node* new_node = new Node(data);
24
25
// Make next of it as head
26
new_node->next = head;
27
Output
To insert a new node at the end of the doubly linked list, we can use the following steps:
Allocate memory for a new node and assign the provided value to its data field.
Initialize the next pointer of the new node to nullptr.
If the list is empty:
Set the previous pointer of the new node to nullptr.
Update the head pointer to point to the new node.
If the list is not empty:
Traverse the list starting from the head to reach the last node.
Set the next pointer of the last node to point to the new node.
Set the previous pointer of the new node to point to the last node.
2
//doubly linked list
4
#include <bits/stdc++.h>
5
using namespace std;
7
struct Node {
8
int data;
9
Node *next, *prev;
10
11
Node(int new_data) {
12
data = new_data;
13
next = prev = nullptr;
14
}
15
};
16
17
// Function to insert a new node at the end of
18
//doubly linked list
19
Node *insertEnd(Node *head, int new_data) {
20
21
// Create a new node
22
Node *new_node = new Node(new_data);
23
24
// If the linked list is empty, set the new
25
//node as the head of linked list
26
if (head == NULL) {
27
head = new_node;
28
}
29
else {
30
Node *curr = head;
Output
If position = 1, create a new node and make it the head of the linked list and return it.
Otherwise, traverse the list to reach the node at position – 1, say curr.
If the position is valid, create a new node with given data, say new_node.
Update the next pointer of new node to the next of current node and prev pointer of new node to current
node, new_node->next = curr->next and new_node->prev = curr.
Similarly, update next pointer of current node to the new node, curr->next = new_node.
If the new node is not the last node, update prev pointer of new node’s next to the new node, new_node->next-
>prev = new_node.
3
#include <bits/stdc++.h>
4
using namespace std;
6
struct Node {
7
int data;
8
Node *next, *prev;
10
Node(int new_data) {
11
data = new_data;
12
next = prev = nullptr;
13
}
14
};
15
16
// Function to insert a new node at a given position
17
Node *insertAtPosition(Node *head, int pos, int new_data) {
18
19
// Create a new node
20
Node *new_node = new Node(new_data);
21
22
// Insertion at the beginning
23
if (pos == 1) {
24
new_node->next = head;
25
26
// If the linked list is not empty, set the prev
27
//of head to new node
28
if (head != NULL)
29
head->prev = new_node;
30
31
// Set the new node as the head of linked list
32
head = new_node;
33
return head;
34
}
35
36
Node *curr = head;
37
// Traverse the list to find the node before the
38
// insertion point
39
for (int i = 1; i < pos - 1 && curr != NULL; ++i) {
Output
Original Linked List: 1 2 4
Inserting Node with data 3 at position 3: 1 2 3 4
To delete a node at the beginning in doubly linked list, we can use the following steps:
1
// C++ Program to delete a node from the
2
// beginning of Doubly Linked List
4
#include <bits/stdc++.h>
5
using namespace std;
7
struct Node{
8
int data;
9
Node *prev;
10
Node *next;
11
Node(int d) {
12
data = d;
13
prev = next = nullptr;
14
}
15
};
16
17
// Deletes the first node (head) of the list
18
// and returns the second node as new head
19
Node *delHead(Node *head) {
20
21
// If empty, return
22
if (head == nullptr)
23
return nullptr;
24
25
// Store in temp for deletion later
26
Node *temp = head;
Output
To delete a node at the end in doubly linked list, we can use the following steps:
Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
If the list is not empty, then move to the last node of the doubly linked list, say curr.
Update the second-to-last node's next pointer to NULL, curr->prev->next = NULL.
Free the memory allocated for the node that was deleted.
2
//Doubly Linked List
4
#include <bits/stdc++.h>
5
using namespace std;
7
struct Node {
8
int data;
9
Node *prev;
10
Node *next;
11
Node(int d) {
12
data = d;
13
prev = NULL;
14
next = NULL;
15
}
16
};
17
18
// Function to delete the last node of the doubly
19
// linked list
20
Node *delLast(Node *head) {
21
22
// Corner cases
23
if (head == NULL)
24
return NULL;
25
if (head->next == NULL) {
26
delete head;
27
return NULL;
28
}
29
Output
To delete a node at a specific position in doubly linked list, we can use the following steps:
1
// C++ Program to delete node at a specific position
2
// in Doubly Linked List
4
#include <iostream>
6
using namespace std;
8
struct Node {
9
int data;
10
Node * prev;
11
Node * next;
12
Node(int d) {
13
data = d;
14
prev = next = NULL;
15
}
16
};
17
18
// Function to delete a node at a specific position
19
// in the doubly linked list
20
Node * delPos(Node * head, int pos) {
21
22
// If the list is empty
23
if (!head)
24
return head;
25
26
Node * curr = head;
27
28
// Traverse to the node at the given position
29
for (int i = 1; curr && i < pos; ++i) {
30
curr = curr -> next;
31
}
32